6
: .
: : . . . . .
. , . : while, for do.
while
while. while () :
while (expression)program statement;expression, . expression (TRUE 0), program statement, . expression. TRUE, program statement. , expression ( while) FALSE () 0, , , program statement. , () :
while (expression){program statement;program2 statement2;program3 statement3;...}while. , , , expression.
, expression ( ).
/* */#include <stdio.h>main(){ int i = 1; char res; printf(" 1 100. . \n"); printf (" y, "); printf("\n n, \n"); printf(", %d?\n",i); /* */ while((res = getchar())!='y') if(res!='\n') /* */ printf(" %d\n",++i); printf(" !\n");}! | . , . , - , . ! |
, , . " " , . ( ), , while, , . , , , ( ). . if. , if () , while , , .
|
|
! | while - , , . . |
while , , .. . , , . , .
! | . , , . , - , , , . . , . , , . , ! |
:
while , , , . while - . , , . , . , , , . :
while (), , .
for
for :
for (init_expression; loop_condition; loop_expression)program statement;, for, .
init_expression ( )
loop_condition , . , ( ) . , .
loop_expression , , .. program statement ( loop_expression , init_expression).
|
|
init_expression loop_expression , loop_condition .
, . init_expression loop_expression, . loop_condition, , , ( )?, .. loop_condition, TRUE.
, , .. :
for (init_expression; loop_condition; loop_expression){program1 statement1;program2 statement2;program3 statement3;...}.
, for, while :
init_expression;while (loop_condition){program statement;loop_expression;}continue.
for(;loop_condition;) program statement;init_expression, loop_expression
while(loop_condition) program statement; for(;;)program statement;while(TRUE) program statement;
.. . break, goto, return, program statement.
. 6.1.
6.1. | ||
/ | ||
1) | == | |
2) | != | |
3) | < | |
4) | <= | |
5) | > | |
6) | >= |
, ( for). . () ().
for do /1, for . for , for do . , for .
for while , , (Ritchie, D.M. 1980. The Programming Language - Reference Manual/. AT&T Bell Laboratories, Murray Hill, N.J. 07974), . , , program statement continue , continue. continue , for while. for loop_expression loop_condition, while loop_expression .
for - . for, .
- :
- :
- , ..:
- , :
z ASCII. , , .
|
|
- , , . :
for(num=1; num<=6; num++) for(num=1; num<6; num++)
, , .
- , , , .. :
x 1.1 , 10%. :
100.00. 110.00. 121.00. 133.10. 146.41.- . , :
, y, x. , for, .
! | , . , . |
- , " ".
, .
- . , , printf(). , , .
, , 6.
- , , , . , , :
, t , if . .
|
|
. for , . " ". - . , , . , . for - . , , . , . , , , .
"" for, .
dowhile
while for . , FALSE (). (.. ) dowhile.
dowhile :
doprogram statement;while (loop_condition);dowhile : program statement, loop_condition while. TRUE (), , program statement . , loop_condition TRUE. FALSE (), ( ), .
! | , dowhile . |
:
do {program1 statement1;program2 statement2;program3 statement3;... } while (loop_condition);while , for , dowhile c .
! | do while - . |
:
do scanf("%d", &number);while(number!=50);. do while , , , . do while - . , . . , , , .
. ? -, , . . , 5% . , . , , , , . , , . , , , .
. , .
:
/* - , 1 . 2, 3, 5, 7 11. *//* */ main(){ int number, divisor, limit; int count=0; printf(", , . \n"); printf("B 2 .\n"); scanf("%d",&limit); while (limit<2) { /* , */ printf("B ! \n"); scanf("%d",&limit); } printf("Ceac !\n"); for (number=2;number<=limit; number++) /* */ { for(divisor=2; number%divisor!=0; divisor++); if(divisor==number) { printf("%5d",number); if(++count%10==0) printf("\n");/* 10 */ } } printf("Pa6o !\n");}, 2 limit, . . count . .
|
|
, , , if, if-else switch, . . , , , , . , goto.
break
break while, do, for, switch, . , , . break
break;:
while((ch=getchar())!= EOF) /* ch=getchar(). EOF, while */{ if(ch=='\n') break; putchar(ch);}, " ".
continue
continue , .
, , . continue :
continue;:
while((ch=getchar())!= EOF) /* ch=getchar(). EOF, while */{ if(ch=='\n') continue; putchar(ch);}continue " ", , EOF.
goto
goto . :
goto ;goto " " . :
goto part1;, , part1. , :
part1: printf(" \n");! | goto! " GOTO", . , : goto, break, continue, return. |
goto, break, continue, return , goto , .
break , switch.
continue .
! | return. , . return , , return . |
while, for dowhile.
1. while. .
:
#include <stdio.h>#include <conio.h> int main(void) {int i, j = 0, n;printf("\n\t Enter the primary natural number: ");scanf_s("%d", &i);printf("\t Enter the last natural number: ");scanf_s("%d", &n); printf("\n\t The numbers are:");while (i <= n) {printf("\n\t %3d", i);++i;++j; }printf("\n\t Total numbers: %d\n", j); printf("\n Press any key: "); _getch(); return 0; }
. 6.1.
. 6.1.
scanf_s(), MS Visual Studio. , , ++i ++j , . "++" . , ++i , i++ , . .
, i n (, n i), ( j), . , , i n. , () i, j.
2. "Hello, world" ( ).
:
#include <stdio.h>#include <conio.h> int main(void){ int c, i = 0; printf("\n Enter symbol-by-symbol the offer \"Hello, world\",\n press twice Ctrl+Z and press Enter:\n");printf("\t"); while ((c = getchar())!= EOF){printf("\t");c = getchar();++i;} printf("\n\t The number of characters: %d\n", i); printf("\n Press any key: "); _getch(); return 0;}Ctrl+Z, (), .. EOF. while , EOF. \"Hello, world\" .
printf("\t") . () i, .
#include <conio.h> - _getch(). , .
. 6.2.
. 6.2.
. getchar() :
#include <stdio.h>int getchar(void);3. for.
, 26 . . , char , .
:
#include <stdio.h>#include <conio.h>int main(void){ int j = 1; char a = 'a'; printf("\n Table code characters:\n");for (; a <= 'z'; ++a) printf("\n %4d) %2c: code%4d", j++, a, a); printf("\n\n Press any key: "); _getch(); return 0;}. 6.3.
. 6.3.
, %4d %2c printf().
4. 4- = 2.3 0 5 ( ) 0.2:
:
#include <stdio.h>#include <conio.h>#include <math.h> int main(void){ int j, f, k = 4; double F, Lt, s, t = 0.0, Tend = 5.0; float L = 2.3F; printf("\n Erlang function of order %d, Lambda = %1.2f:\n\n", k, L); for (; t <= Tend; t += 0.2) { s = 0.0; f = 1; Lt = 1.0; for (j = 1; j <= k; ++j) { f *= j; Lt *= (L*t); s += (Lt/f)*exp(-L*t); } F = 1 - exp(-L*t) - s; printf("\t %lg\n", F); } printf("\n\n Press any key: "); _getch(); return 0;}for. ( ):
t += 0.2; /* t = t + 0.2; */f *= j; /* f = f * j; */t *= (L*t); /* Lt = Lt * (L*t); */, 0!
t. #include <math.h>.
. 6.4
. 6.4. 4-
5. " " :
:
#include <stdio.h>#include <conio.h> int main(void) { double denom; double sum1 = 0.0, sum2 = 0.0; int k = 1; denom = k * (k + 1) * (k + 2); // do { sum1 = sum2; sum2 += 1.0 / denom; denom = denom / k * (k + 3); ++k; } while (sum1 < sum2); printf("\n\t The amount of numerical series: %lg\n", sum2); printf("\n Press any key: "); _getch(); return 0;}sum2. sum1. , sum1 < sum2 ( denom). ( ), sum1 < sum2 . , " ".
. "l".
. 6.5.
. 6.5.
6. , .
, , , 123 321.
:
#include <stdio.h>#include <conio.h> int main(void) { long int x, r=0; printf("\n Enter an integer: "); scanf_s("%ld", &x); printf("\n Reverse-digit number %ld:\n\n\t", x); do { r =r*10+ x % 10; x /= 10; } while (x!= 0); printf(" %ld", r); printf("\n\n Press any key: "); _getch(); return 0;}, , , long int ( long). int long int .
. 6.6.
. 6.6.
7. for "" , , . for .
:
#include <stdio.h>#include <conio.h> int main (void) { int p = 0; char ch = 'A'; char i, j, k, ch2, kk, chA; chA = ch; printf("\n Enter a capital letter between \"A\" and \"S\": ");scanf_s("%c", &ch2, sizeof(char)); for(chA -= 1; chA >= ch2; chA--) { printf("\n Error! Press any key: "); _getch(); return -1; } for (kk = 'S'+1; kk <= ch2; kk++) {printf("\n Error! Press any key: "); _getch(); return -1;} k = ch2;for (kk = ch; kk <= k; kk++) { printf("\n "); for (ch2 = ch; ch2 <= k-p; ch2++) printf(" "); for (j = ch; j <= kk; j++) printf(" %c", j); for (i = kk; i > ch; i--) printf(" %c", i-1); p++; } printf("\n\n Press any key: "); _getch(); return 0;}. 6.7.
. 6.7.
- ?
- ?
- ?
- ?
- ?
- ?
- ?
- ? .
- while for?
.
, : while for . .
1.
1.
for , :
2.
, if .
3.
4.
5.
2.
1.
for , :
2.
3.
4.
5.
3.
1.
for , :
2.
3.
4.
5.
4.
1.
for , :
2.
3.
4.
5.
5.
1.
for , :
2.
3.
4.
5.
6.
1.
for , :
2.
3.
4.
5.
7.
1.
for , :
2.
3.
4.
5.
8.
1.
for , :
2.
3.
4.
5.
9.
1.
for , :
2.
3.
4.
: 1. (1, 1, 2, 3, 5, 8, 13, ). , m (m>1).
5.
10.
1.
for , :
2.
3.
4.
, n 5.
5.
11.
1.
for , :
2.
3.
4.
k .
5.
12.
1.
for , :
2.
. .
3.
4.
5.
13.
1.
for , :
2.
3.
4.
5.
14.
1.
for , :
2.
3.
4.
5.
15.
1.
for , :
2.
, if .
3.
4.
5.
16.
1.
for , :
2.
3.
4.
5.
17.
1.
for , :
2.
3.
4.
5.
18.
1.
for , :
2.
3.
4.
: 1. (1, 1, 2, 3, 5, 8, 13, ). S , n (n>1).
5.
19.
1.
for , :
2.
3.
4.
5.
. n . n .
20.
1.
for , :
2.
3.
4.
5.
. . .
21.
1.
for , :
2.
3.
4.
5.
22.
1.
for , :
2.
, if .
3.
4.
5.
23.
1.
for , :
2.
3.
4.
5.
24.
1.
2.
3.
4.
5.
25.
1.
for , :
2.
, if .
3.
4.
5.