Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Control Structures Repetition (Loops) Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1 to 100. The answer will be 1 + 2 + 3 + 4 + 5 + 6 + … + 99 + 100. Here’s some sample C code: int sum=0; sum = sum+1; sum = sum +2; sum = sum +3; sum = sum +4; … sum = sum +99; sum = sum +100; printf(“%d”,sum); This solution has problems: It would take a long time to type in. There is a high risk of making an error while typing it in. It doesn’t easily scale. This may work for 100 numbers but how would you handle having to add from 1 to a 1000? Or to 1000000?Or to 10000000? The Algorithm 1. 2. 3. 4. 5. 6. 7. 8. 9. Create a variable to hold the sum. Initialize the sum to zero. Create a variable to hold a counter from 1 to 100. Initialize the counter to 1. While the counter is less-than-or-equal to 100 add the counter to the sum add one to the counter Now repeat Print the sum We can use pseudo-code: sum = 0 count = 1 loop while count <= 100 sum = sum + count count++ endloop print sum Syntax: while(condition) <body> endloop before executing the statements in the body, evaluate the condition. If the condition is true then execute the body once. Once you have executed the body statements once, go back to the loop condition and re-evaluate it. If it is true, execute the body code again. If the condition is not true then the body will not be executed! The while Looping (Repetition) Structure Infinite loop: is a loop that continues to execute endlessly. • So, expression is always true in an infinite loop. • Statements must change value of expression to false. • 8 Example i = 0 loop while (i <= 20) print(i) i = i + 5 end loop 0 Output 5 0 5 10 15 20 15 20 25 i = 0 i <= 20 i 10 start Yes Print i i = i + 5 end What will happen if you omit (i= i + 5) ? No Example Write a program to allow the user to enter a set of integer numbers, then print the sum of these numbers. Start Program Read setSize counter = 0 sum = 0 Loop while (counter < setSize) Read number sum = sum + number counter = counter + 1 End loop Print sum End Program start Start Program Read setSize counter = 0 sum = 0 Loop while (counter < setSize) Read number sum = sum + number counter = counter + 1 End loop Print Sum End Program setSize counter sum number 3 0 0 1 Output 1 1 10 2 11 4 3 1 10 4 15 3 15 Read setSize counter = 0 sum = 0 counter < setSize Yes Read number sum = sum + number counter = counter + 1 Print sum end No Sentinel-Controlled Loop Used when exact number of entry pieces is unknown, but last entry (special/sentinel value) is known. The idea of a sentinel controlled loop is that there is a special value (the "sentinel") that is used to say when the loop is done. General form: Input the first data item into variable Loop while (variable != sentinel) . . . input a data item into variable; . . . End loop Example Write a program that adds up a list of positive numbers entered by the user from the keyboard, then prints the result. What is the number of iterations ?? ◦ Unknown Since the input are positive integers, we can use (-1) as the sentinel. Example of user input: 1 3 6 4 9 12 3 5 -1 Solution Start Program sum = 0 Read number loop while (number != -1) sum = sum + number Read number End loop Print sum End Program sum number 0 5 5 2 7 3 10 -1 start sum = 0 Read number number != -1 Yes Input 5 2 3 -1 sum= sum+ number Read number Print sum Print sum 10 end No While loop It is pre tested loop. Syntax: while (Expression) { Loop body } Properties of while loop: Task of the expression is to check the condition. Loop will execute until condition is true otherwise loop will terminate. If any expression returns zero then condition will false and if it returns any non- zero number then condition will true. For example: #include<stdio.h> int main(){ int x=3,y=2; while(x+y-1){ printf("%d ",x--+y); } return 0; } output:5 4 3 2 In while loop condition expression is compulsory. For example: #include<stdio.h> int main(){ while( ) { printf("Hello world"); } return 0; } Compilation error While loop without any body is possible. For example: #include<stdio.h> int main(){ int i=0; while(i=i+1,i<=8); printf("%d ",i); return 0; } Output: 9 In while loop there can be more than one conditional expression. For example #include<stdio.h> int main(){ int x=2,y=2; while(x<=5,y<=3) printf("%d %d ",++x, ++y); return 0; } Output: 3 3 4 4 If loop body contain only one statement the brace is optional. do while loop in c It is also called as post tested loop. It is used when it is necessary to execute the loop at least one time. Syntax: do { Loop body } while (Expression); void main(){ int i=0; clrscr(); do { printf(“hi”); i=i+1; } while(i<=10); getch(); } Output: hi hi hi…. If there is only one statement in the loop body then braces is optional. For example: void main(){ int i=5; clrscr(); do printf("hi"); while(!i); getch(); } void main(){ int x=25,y=1; do { if(x>5) printf(" ONE"); else if(x>10) printf(" TWO"); else if(x==25) printf(" THREE"); else printf(" FOUR"); } while(y--); getch(); } Output: ONE ONE for loop: Syntax: for (Expression 1; Expression 2; Expression 3) { Loop body } Order of movement of control in for loop: for (Expression 1; Expression 2; Expression 3) First time: Expression 1 Expression 2 Loop body Expression 3 Second time and onward: Expression 2 Loop body Expression 3 That is expression 1 only executes in the first iteration. From second iteration onwards control doesn’t go to the expression 1. #include<stdio.h> int main(){ int i; for(i=0;i<=4;i++){ printf("%d ",i); } return 0; } Output: 0 1 2 3 4 Explanation of each term of syntax: Expression 1: It is called initialization expression. Task of this expression is to initialize the looping variables. Properties of expression 1: Expression1 can initialize more than one variable. For example #include<stdio.h> int main(){ int i,j,k; for(i=0,j=2,k=1;i<=4;i++){ printf("%d ",i+j+k); } return 0; } Output: 3 4 5 6 7 Expression1 is optional. For example: #include<stdio.h> void main(){ int i=1; for( ; i<=4 ; i++){ printf("%d ",i); } return 0; } Output: 1 2 3 4 Expression 2: It is called as conditional expression. Task of expression is to check the condition and if it is false then it terminates the loop. For example: include<stdio.h> int main(){ int i; for(i=1;i<=3;i++){ printf("hi "); } printf("%d",i); return 0; } Output: hi hi hi 4 Expression2 can have more than one checking condition and if any condition is false loop will terminate. For example: #include<stdio.h> void main(){ int i,j=2; for(i=0;i<=5,j>=0;i=i+1){ printf("%d ",i+j); j=j-1; } return 0; } Output: 2 2 2 Expression2 is also optional. For example: #include<stdio.h> int main(){ int j; for(j=0; ;j++){ printf("%d ",j); if(j>=2) break; } return 0; } #include<stdio.h> int main(){ int i; for(;i=0,i<=3 ;i++){ printf("%d ",i); } return 0; } Output: Infinite loop #include<stdio.h> int main(){ int i=0; for(; i+=2,i<5 ; i++){ printf("%d ",i); } return 0; } #include<stdio.h> int main(){ int i; for(i=0; -5 ; i++){ printf("%d ",i); if(i==3) break; } return 0; } It is called as instrumentation expression. Task of this expression is to increment the variable. We can increment more than one variable at the same time in the expression3. For example #include<stdio.h> int main(){ int i,j,k; for(i=0,j=0,k=0;i<=5,j<=4,k<=3;i++,++j,k+=2){ printf("%d ",i+j+k); } return 0; } Expression 3 is also optional. For example #include<stdio.h> int main(){ int i; for(i=0;i<=3; ){ printf("%d ",i); i=i+1; } return 0; } continue: It is keyword of c and task of this keyword is to transfer the control of program at the beginning of loop. For example: #include<stdio.h> int main(){ int i=5; do{ printf("%d",i); continue; i++; } while(i<=10); return 0; } Except looping, we cannot use continue keyword. #include<stdio.h> int main(){ int x; scanf("%d",&x); switch(x){ case 1:printf("1");break; case 2:printf("2");continue; default:printf("3"); } return 0; } Nested loop A loop inside another loop is known as nested loop. We can write any loop inside any loop in c i.e. we can write for loop inside the loop or while loop or do while loop etc. Difference while Loop do…while Loop for Loop