, , switch. :
switch (<>)
{
case <1>: < 1>;
case <2>: < 2>;
case <N>: < N>;
default: < >;
}
- . , switch, , case, , , . , . default:, , . , switch.
, switch, (, ) . . . , (break, continue, goto, return). , break.
89
#include < stdio.h >
main ()
{
int c;
printf(" 1 7:");
c=getchar();
printf("\n :");
switch ()
{
case '1': (printf("\n!");break;}
case ' 2': {printf("\n!");break;}
case '3': (printf("\n!");break;}
case '4': {printf("\n!");break;}
case '5': (printf("\n!");break;)
case '6': {printf("\n!");break;}
default:printf("\nBocpecee!");
}
}
, .
for
for (<1>;<>;<2>) <3>;
. , 1 2 , 1 - .
for , , , . 1 .
1 , 2 .
,
|
|
for (i=l; i<n; i++) <>;
. for : i, , i .
for , . 1 2, . 1, , .
,
for (i=l;;i++) {
}
.
for:
1)
for(n=10;n>0;n-)
printf("%d \n",n);
2) , ..
for (n=2;n<60;n+=2)
printf("%d\n",n);
3) ,
for(ch='a';ch<='z';ch++)
printf("Bea ASCII % %d.\n",ch,ch);
4) , ,
for(num=l;num*num*num<216;num++)
5) , ,
for(n=l;n<1500;n*=3)
printf("-%d \n",n);
6) ; ,
for(x= I;<=75;=5*(++)+10)
printf("%d, %d\n",x,y);
7) ( ";"); ,
ans=2;
for(n=3;ans<=25;)
ans=ans*n;
for(;;)
printfC"* \ n");
, ;
8) ; , , printf() ; , ,
for(printf("3 !\n");num = 6;)
scanf('"%d",&num);
printf("TO , !\n");
, , 6;
9) , , , ; , , :
for(n=l;n< l000;n+=delta)
, dalta , if ( ).
|
|
. .
for . , :
1. for(i=l;i<10;i++)
for(j=l;j<10;j++)
<>
2. for(i=l,j=l;i<10,j<10;i++,j++)
<>
, , z .
90
# include<stdio.h>
main()
{
int x,y,z,v,u,zero();
for(x=l,y=l,z=l;x<10;x++,y++,z++)
printf("x=%d y=%d z=%d\n", , y, z);
}
:
x=l y=l z=l
x=2 y=2 z=2
x=3 y=3 z=3
x=4 y=4 z=4
x=5 y=5 z=5
x=6 y=6 z=6
x=7 y=7 z=7
x=8 y=8 z=8
x=9 y=9 z=9
while
while :
while (<>) <>;
while , (.. ). , , ..
( ), , while, , . , , , ( ). , while.
while - , , . .
1. .
91
#include<stdio.h>
main()
{
int x,y;
scanf("\n%d",&x); scanf("\n%d",&y);
while (x!=y)
{
if (x>y) x=x-y; else y=y-x;
)
printf("\n%d",x);
)
2. , n 3.
92
#include<stdio.h>
main()
{
int i,n,a;
scanf("%d",&n); n=n*n; i=10000;
while(i>=l)
{
a=n/i; /* n/i
1, =0 */
n=n-a*i;
if(a==3) goto abc; else i=i/10;
)
printf(" 3 n^2 ");
goto cd;
abc: printf(" 3 n^2 ");
cd:;
}
do/while
( ), . do/while.
do/while , .
:
do <>
while (<>);
break
break for, while, do/while, switch . break .
break , .
93
# include<stdio.h>
main()
{
int x=l,y,z;
printf(" y=2*x+z\n");
printf(" z:"); scant("%d",&z);
while(x<1000)
(
y=2*x+z;x++; if y=100 (printf(= 100\n"); break; }
} if y=100
printf(" !!!");
else printf (" !!! y<>lOO.");
}
break .
|
|
for(;;),
break.