Práctica no 11

10
PRÁCTICA 11 INSTRUCCIONES DE REPETICIÓN

Transcript of Práctica no 11

Page 1: Práctica no 11

PRÁCTICA 11

INSTRUCCIONES DE

REPETICIÓN

Page 2: Práctica no 11

While

• El propósito del ciclo while es ejecutar muchas

instrucciones varias veces. Se escribe como una

instrucción if solo que cambiando la palabra

clave while.

• Forma general

while (condición) { instrucciones a repetir

mientras la condición sea true }

Page 3: Práctica no 11

CICLO WHILE

1. /*PROGRAMA QUE ILUSTRA EL CICLO WHILE*/ 2. #include <stdio.h> 3. #include <conio.h> 4. main() 5. { 6. int cont=0; 7. cont =0; 8. while (cont<11) 9. { 10. printf("EL VALOR DEL CONTADOR ES =%d \n", cont); 11. cont=cont+1; 12. } 13. getch(); 14. return 0; 15. }

Page 4: Práctica no 11

CICLO WHILE 1. #include <stdio.h> 2. #include<stdlib.h>

3. main() 4. { 5. int contador=0, numero1=0,numero2=1,numero3; 6. printf("\nImprime los primeros 10 numeros de la serie fibonacci\n\n\n"); 7. printf ("\t %i \n\t %i\n", numero1, numero2); 8. while (contador<8) 9. { 10. numero3=numero1+numero2; 11. printf("\t %i \n",numero3); 12. numero1=numero2; 13. numero2=numero3; 14. contador++; 15. } 16. system ("pause"); 17. }

Page 5: Práctica no 11

Su diferencia básica con el ciclo while es que la prueba de condición es hecha al finalizar el ciclo, es decir las instrucciones se ejecutan cuando menos una vez, porque primero ejecuta las instrucciones y al final evalúa la condición.

También se le conoce por esta razón como ciclo de condición de salida.

Forma general cargar o inicializar variable de condición; do{ grupo cierto de instrucción(es); instrucción(es) de

rompimiento de ciclo; } while(condición);

Do while

Page 6: Práctica no 11

CICLO DO WHILE 1. /*PROGRAMA QUE ILUSTRA EL CICLO DO WHILE*/ 2. #include <stdio.h> 3. #include <conio.h> 4. main() 5. { 6. int cont; 7. cont=0; 8. do 9. { 10. printf("EL VALOR DEL CONTADOR ES =%d \n", cont); 11. cont=cont+1; 12. } 13. while 14. (cont<=10); 15. getch(); 16.return 0; 17.}

Page 7: Práctica no 11

CICLO DO WHILE 1. #include <stdio.h> 2. #include<stdlib.h>

3. main() 4. { 5. int contador=0, numero1=0,numero2=1,numero3; 6. printf("\nImprime los primeros 10 numeros de la serie fibonacci\n"); 7. printf ("\t %i \n\t %i\n", numero1, numero2); 8. do{ 9. numero3=numero1+numero2; 10. printf("\t %i \n",numero3); 11. numero1=numero2; 12. numero2=numero3; 13. contador++; 14. } while (contador<8); 15. system("pause");

16.}

Page 8: Práctica no 11

For

• Esta orden es similar al while, pero es mas fácil

de usar si se están contando o indexando

variables, este ciclo combina tres elementos :

inicialización, prueba e incremento.

• Forma general

for (inicio; condicion; paso-siguiente)

{ cuerpo }

Page 9: Práctica no 11

CICLO FOR

1. /*PROGRAMA QUE ILUSTRA EL CICLO FOR*/ 2. #include <stdio.h>

3. #include <conio.h>

4. main()

5. {

6. int indice; 7. for (indice=0;indice<11;indice=indice+1)

8. {

9. printf("EL VALOR DEL INDICE ES =%d \n", indice);

10. }

11. getch(); 12. return 0;

13. }

Page 10: Práctica no 11

CICLO FOR 1. #include <stdio.h> 2. #include<stdlib.h>

3. main() 4. { 5. int contador, numero1,numero2,numero3,n; 6. numero1=0; 7. numero2=1; 8. printf("\nHasta que numero de la serie de Fibonacci deseas?"); 9. scanf ("%d",&n); 10. printf ("\t %d) %i \n\t %d) %i\n", contador=1,numero1,contador=2, numero2); 11. for (contador=3;contador<=n;contador++) 12. { 13. 14. numero3=numero1+numero2; 15. printf("\t %d) %i\n",contador,numero3); 16. numero1=numero2; 17. numero2=numero3; 18. } 19. system("pause"); 20. }