공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE
LOOPS WHILE AND FOR while syntax while (expression) statement The expression is evaluated. If it is true (non-zero), statement is executed and expression is re-evaluated. This cycle continues until expression becomes zero, at which point execution resumes after statement. 1
LOOPS WHILE AND FOR for syntax 1 4 for (expr1; expr2; expr3) statement 2 3 Grammatically, the three components of a for loop are expressions. Most commonly, expr1 and expr3 are assignments or function calls and expr2 is a relational expression. Any of the three parts can be omitted, although the semicolons must remain. If the test, expr2, is not present, it is taken as permanently true, so is an infinite loop, presumably to be broken by other means, such as a break or return. 2
LOOPS WHILE AND FOR for (expr1; expr2; expr3) statement equivalent int i, sum = 0; const int MAX = 10; for(i = 1; i <= MAX; i++) sum += i; printf("sum from 1 to %d is : %d\n", MAX, sum); expr1; while (expr2) statement expr3; int i = 1, sum = 0; const int MAX = 10; while (i <= MAX) sum += i; i++; printf("sum from 1 to %d is : %d\n", MAX, sum); 3
LOOPS WHILE AND FOR Whether to use while or for is largely a matter of personal preference. For example, in while ((c = getchar()) == ' ' c == '\n' c = '\t')... there is no initialization or re-initialization, so the while is most natural. The for is preferable when there is a simple initialization and increment since it keeps the loop control statements close together and visible at the top of the loop. This is most obvious in for (i = 0; i < n; i++)... 4
BREAK The break statement provides an early exit from for, while, and do, just as from switch. A break causes the innermost enclosing loop or switch to be exited immediately. 5
BREAK int input, i, sum; while(1) sum = 0; printf("enter a positive integer or 0 for END :"); scanf("%d", &input); if(input <= 0) break; for(i=1;i <= input; i++) sum += i; printf("sum from 1 to %d : %d\n", input, sum); printf("end the Program.\n"); 6
CONTINUE The continue statement causes the next iteration of the enclosing for, while, or do loop to begin. In the while and do, this means that the test part is executed immediately. In the for loop, control passes to the increment step. The continue statement applies only to loops, not to switch. 7
CONTINUE int input, i; printf("enter a positive integer greater than 3: \n"); scanf("%d", &input); printf("multiples of 3 from 1 to %d are : ", input); for(i = 1; i<=input; i++) if(i % 3!= 0) continue; printf("%d%s", i, (input-i)<3? \n :, ); 8
INFINITE LOOP An infinite loop (endless loop) is a sequence of instructions in a computer program which loops endlessly. If it is not intentional, the loop can be terminated by ctrl+c If you want to make an infinite loop, use non-zero as the conditional expression while and do while without conditional expression causes an error infinite loop error 9
NESTED LOOP Nested loop a loop within a loop outer loop inner loop for (m = 1; m <= 4; m++) for (n = 1; n <= 3; n++) statements outer loop control variable m; iterates 1 to 4 inner loop control variable n; iterates 1 to 3 Loop start Loop end 10
NESTED LOOP int i, j; for (i = 1; i <= 5; i++) for (j = 1; j <= i; j++) printf("*"); printf("\n"); Loop start Loop end 11