Está en la página 1de 20

1

Algoritmos

“ Todo puede ser resuelto ejecutando una serie de


acciones en un orden especifico”

 2000 Prentice Hall, Inc. All rights reserved.


2

Pseudocode
• Pseudocódigo
– Lenguaje informal que permite desarrollar algoritmos
– Similar a un Ingles o español estructurado
– Actualmente se puede ejecutar en computadores: PSInt

 2000 Prentice Hall, Inc. All rights reserved.


3

Estructuras de Control

• Ejecución Secuencial

• Transferencia de control

• Todo programa se escriben en términos de 3


estructuras de control:
• Estructura secuencial
• Estructura de selección: if, if/else, y switch
• Estructura de repetición: while, do/while y for

 2000 Prentice Hall, Inc. All rights reserved.


4

La estructura de selección: if
• Estructura de selección:
– Usada para escoger entre una alternativa de acciones.
– Pseudocodigo:
If student’s grade is greater than or equal to 60
Print “Passed”
– Si condición es true (verdadera)
• Imprime el mensaje y pasa a la siguiente instrucción
– Si condición es false (falsa),
• No imprime el mensaje y pasa a la siguiente instrucción

 2000 Prentice Hall, Inc. All rights reserved.


5

Pseudocódigo

Si (nota >= 40) entonces


Escribir “Asignatura aprobada”;
Fin-si

if ( nota >= 40 )
printf( "Passed\n" );

 2000 Prentice Hall, Inc. All rights reserved.


6

• if single

true
nota >= 60 print “aprobado”

false

 2000 Prentice Hall, Inc. All rights reserved.


7

Estructura de control: if/else


• if
– Solamente ejecuta una acción si la condición es true
• if/else
– Especifíca una acción a ser ejecutada cuando la condición es
true y cuando esta es false

• Psuedocódigo:
If student’s grade is greater than or equal to 60
Print “Passed”
else
Print “Failed”

 2000 Prentice Hall, Inc. All rights reserved.


8

• Pseudolenguaje:
si ( nota >= 40 ) entonces
Escribir “Asignatura aprobada";
sino
Escribir “Asignatura reprobada";
Fin-si

• Código en C code:
if ( nota >= 40 )
printf( “Asignatura aprobada\n");
else
printf( “Asignatura reprobada\n");

 2000 Prentice Hall, Inc. All rights reserved.


9

3.6 The if/else Selection Structure


• Flow chart of the if/else selection structure

false true
nota >= 40

print “Failed” print “Passed”

 2000 Prentice Hall, Inc. All rights reserved.


10

If anidados
– Pseudocodigo para estructura if/else anidado

If student’s grade is greater than or equal to 90


Print “A”
else
If student’s grade is greater than or equal to 80
Print “B”
else
If student’s grade is greater than or equal to 70
Print “C”
else
If student’s grade is greater than or equal to 60
Print “D”
else
Print “F”

 2000 Prentice Hall, Inc. All rights reserved.


11

La estructura de selección: if/else


• Varias instrucciones en el cuerpo if/else:
Pseudlenguaje:
C
if ( nota >= 60 )
si ( nota >= 60 )
printf( “Asignatura aprobada.\n" );
escribir “Asignatura aprobada”
else {
sino
printf( “Asignatura
escribir “Asignatura reprobada” reprobada.\n" );
escribir “Debe cursar nuevamente la printf( “Debe cursar
asignatura” nuevamente la
Fin-si asignatura.\n" );
}

 2000 Prentice Hall, Inc. All rights reserved.


12

La estructura de control repetitiva: while


• Estructura de repetición:
– Programador especifíca una acción a ser repetida mientras se
cumpla con condición true

– Psuedocódigo:
While there are more items on my shopping list
Purchase next item and cross it off my list

 2000 Prentice Hall, Inc. All rights reserved.


13

• Ejemplo Pseudolenguaje

numero <- 0;
Mientras ( numero <= 10 ) haga
numero = numero + 1;

true
numero <= 10 numero = numero + 1

false

 2000 Prentice Hall, Inc. All rights reserved.


14

• Ejemplo C :
int numero = 0;
while ( numero <= 10 )
numero = numero + 1;

true
numero <= 10 numero = numero + 1

false

 2000 Prentice Hall, Inc. All rights reserved.


1 /* Fig. 3.6: fig03_06.c 15
2 Class average program with Outline
3 counter-controlled repetition */
4 #include <stdio.h>
5
6 int main()
7 {
8 int counter, grade, total, average;
9
10 /* initialization phase */
11 total = 0;
12 counter = 1;
13
14 /* processing phase */
15 while ( counter <= 10 ) {
16 printf( "Enter grade: " );
17 scanf( "%d", &grade );
18 total = total + grade;
19 counter = counter + 1;
20 }
21
22 /* termination phase */
23 average = total / 10;
24 printf( "Class average is %d\n", average );
25
26 return 0; /* indicate program ended successfully */
27 }
 2000 Prentice Hall, Inc. All rights reserved.
1 /* Fig. 3.8: fig03_08.c 16
2 Class average program with Outline
3 sentinel-controlled repetition */
4 #include <stdio.h>
5
6 int main()
7 {
8 float average; /* new data type */
9 int counter, grade, total;
10
11 /* initialization phase */
12 total = 0;
13 counter = 0;
14
15 /* processing phase */
16 printf( "Enter grade, -1 to end: " );
17 scanf( "%d", &grade );
18
19 while ( grade != -1 ) {
20 total = total + grade;
21 counter = counter + 1;
22 printf( "Enter grade, -1 to end: " );
23 scanf( "%d", &grade );
24 }
 2000 Prentice Hall, Inc. All rights reserved.
25
Outline
26 /* termination phase */

27 if ( counter != 0 ) {

28 average = ( float ) total / counter;

29 printf( "Class average is %.2f", average );

30 }

31 else

32 printf( "No grades were entered\n" );

33

34 return 0; /* indicate program ended successfully */

35 }

 2000 Prentice Hall, Inc. All rights reserved.


1 /* Fig. 3.10: fig03_10.c 18
2 Analysis of examination results */ Outline
3 #include <stdio.h>
4
5 int main()
6 {
7 /* initializing variables in declarations */
8 int passes = 0, failures = 0, student = 1, result;
9
10 /* process 10 students; counter-controlled loop */
11 while ( student <= 10 ) {
12 printf( "Enter result ( 1=pass,2=fail ): " );
13 scanf( "%d", &result );
14
15 if ( result == 1 ) /* if/else nested in while */
16 passes = passes + 1;
17 else
18 failures = failures + 1;
19
20 student = student + 1;
21 }
22
23 printf( "Passed %d\n", passes );
24 printf( "Failed %d\n", failures );
25
26 if ( passes > 8 )
27 printf( "Raise tuition\n" );
28
29 return 0; /* successful termination */
30 }
 2000 Prentice Hall, Inc. All rights reserved.
19

Operadores de asignación:
• Asignación de opeardores puede ser abreviada:
c = c + 3;
c += 3;

• Otros ejemplos:

d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)

 2000 Prentice Hall, Inc. All rights reserved.


20

Operador incremental/decremental
• Operador incremental (++)
Puede ser usado en vez de: c+=1 o c=c+1

• Operador decremental (--)


Puede ser usado en vez de: c-=1 o c=c-1

• Preincrement: ++c

• Postincrement: c++

 2000 Prentice Hall, Inc. All rights reserved.

También podría gustarte