Programación básica en C para ambientes Unix

Anuncio
Curso-Taller
Programación básica en C
para ambientes Unix
Antonio Marín-Hernández
Departamento de Inteligencia Artificial
Facultad de Física e Inteligencia Artificial
Universidad Veracruzana
Sesión 2. Ciclos
•
•
•
•
•
for
while
do… while
break
continue
for
for (exp1; exp2; exp3)
statement;
or {block of statements}
exp1 inicialización
exp2 condición
exp3 modificador
for
for (x=3;x>0;x--) {
printf("x=%d \n",x);
}
>x=3
x=2
x=1
for
for (x=0;((x>3) && (x<9)); x++)
for (x=0,y=4;((x>3) && (y<9));
x++,y+=2)
for (x=0,y=4,z=4000;z; z/=10)
while
while (expression)
statement;
or {block of statements}
Ej.
while (x>0) {
printf("x=%d \n",x);
x--;
}
while
while (x>0) {
printf("x=%d \n",x);
x--;
}
>x=3
x=2
x=1
while
while (x--);
while (x=x+1);
while (x+=5);
while
while (i++ < 10);
while ( (ch = getchar()) != `q')
putchar(ch);
do … while
do
statement;
or {block of statements}
while (expression);
do … while
do {
printf("x=%d \n",x--);
} while (x>0);
>x=3
x=2
x=1
break and continue
• break –
– exit form loop or switch.
• continue –
– skip 1 iteration of loop.
break and continue
while((scanf(“%d”,&value)==1) && (value != 0)) {
if (value < 0) {
printf(“Illegal value n”);
break; /* Abandon the loop */
}
if (value > 100) {
printf(“Invalid value n”);
continue; /* Skip to start loop again */
}
/* Process the value read */
/* guaranteed between 1 and 100 */ ....;
....;
} /* end while value != 0 */
Descargar