# Programación básica en C para ambientes Unix

Anuncio
```Curso-Taller
Programaci&oacute;n b&aacute;sica en C
para ambientes Unix
Antonio Mar&iacute;n-Hern&aacute;ndez
Departamento de Inteligencia Artificial
Facultad de F&iacute;sica e Inteligencia Artificial
Sesi&oacute;n 2. Ciclos
•
•
•
•
•
for
while
do… while
break
continue
for
for (exp1; exp2; exp3)
statement;
or {block of statements}
exp1 inicializaci&oacute;n
exp2 condici&oacute;n
for
for (x=3;x&gt;0;x--) {
printf(&quot;x=%d \n&quot;,x);
}
&gt;x=3
x=2
x=1
for
for (x=0;((x&gt;3) &amp;&amp; (x&lt;9)); x++)
for (x=0,y=4;((x&gt;3) &amp;&amp; (y&lt;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&gt;0) {
printf(&quot;x=%d \n&quot;,x);
x--;
}
while
while (x&gt;0) {
printf(&quot;x=%d \n&quot;,x);
x--;
}
&gt;x=3
x=2
x=1
while
while (x--);
while (x=x+1);
while (x+=5);
while
while (i++ &lt; 10);
while ( (ch = getchar()) != `q')
putchar(ch);
do … while
do
statement;
or {block of statements}
while (expression);
do … while
do {
printf(&quot;x=%d \n&quot;,x--);
} while (x&gt;0);
&gt;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”,&amp;value)==1) &amp;&amp; (value != 0)) {
if (value &lt; 0) {
printf(“Illegal value n”);
break; /* Abandon the loop */
}
if (value &gt; 100) {
printf(“Invalid value n”);