Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.

Similar presentations


Presentation on theme: "Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition."— Presentation transcript:

1 Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition becomes false, the loop ends and control passes to the statements following the loop. There are three kinds of loops in C++: the for loop, the while loop, and the do loop. LOOPS http://vustudents.ning.com

2 THE for LOOP Syntax : for (init expr; test expr; incr expr ) { statement(s); } –init expr sets the initial value of the loop control variable which acts as a counter. –test expr is a conditional expression that is used to terminate the loop. –Incr expr is some modification of the loop control variable. –The body of the loop may have one statement or a block of statements enclosed in { }.

3 THE for LOOP Execution of the loop is as follows: –First, init expr is evaluated. –Then test expr is tested if it is true then body of the loop is executed. –After its execution, incr expr is evaluated and test expr is tested again. –The loop terminates when test expr becomes false.

4 #include void main( ) { clrscr ( ); int n; for (n =l; n<=10; n++) cout << n <<"\t" << n*n << “\t" << n*n*n « “\n”; } Example of a for Loop

5 1. We can increase as well as decrease the value of loop control variable by any amount. For example for (n=1;i<=15; n=n+3) 2. We can test a condition other than the number of iterations to terminate the loop. for (n=1; n*n<=10; n++) 3. The increment or decrement need not be an integer. for (n=50; n<=100; n=n*1.5) cout « n; VARIATIONS OF for LOOP

6 4. The termination condition controlling the loop may be any valid C++ expression. m=5; for (n=1; m<=35; m=++n* 5+5) cout « n « m; 5. The pieces of the loop definition need not be there. (don't omit semicolons). n=2; for (i=3; n<=20;) n = n*i;

7 6. The value of the control variable can also be input from the keyboard. for (n=5; n!=0 ;) { cout << "n= " << n; cout << "Enter a number "; cin >> n; } 7. We can change the value of the loop control variable inside the body of the loop. for (n=0; n<=10 ;) ++n; VARIATIONS OF for LOOP

8 8. Another variation of the for loop is that we can move the initialization section outside the loop. n=5; for ( ; n<=10 ;) ++n; 9. The expression1 need not be an assignment statement. It could, for example, be a cout statement. for (cout « "Enter a number “ ; n!=0 ;) cin » n; VARIATIONS OF for LOOP

9 10. We can create an infinite loop using the following general form of for loop for (; ;) statement; 11. The body of the loop may be empty. This type of loop is used to introduce a time delay. for (n=1; n<=100; n++) ; Notice the semicolon that terminates the for statement, for statement expects a statement in the body of the loop, which, in this case, is empty.

10 The comma operator extends the flexibility of for loop by allowing to include multiple expressions for any or all the expressions in the loop. #include void main ( ) { int n,m; for (n=0,m=0; n<=5; ++n, m+=2) cout « n « "\t" « m « '\n"; } THE COMMA OPERATOR

11 The general form of while loop is while (expression) { statement(s); } The while loop first evaluates the expression if it is true then statement(s) is executed and expression is evaluated again. If the expression is false then while loop terminates and the control passes to the statement immediately following while loop. The statement may be a single statement or a block of statements. THE while LOOP

12 #include void main ( ) { int no=10; while (no > 0) { cout« no « '\t" « no*no « '\t" « no*no*no « '\n"; no --; } THE while LOOP

13 We can use a while loop to check the validity of an input. For instance, the following while loop keeps on executing until a 'Y' or 'N' is entered. The function getchar( ) used in the loop, reads a single character from the keyboard. This function is defined in stdio.h header file while ((ch =getchar ( )) != ‘Y’ | | ch != ‘N’) THE while LOOP

14 The general form of the loop is do { statement(s) } while (expression); First, the statement(s) is executed and then the expression is evaluated. If the expression is true, the statement is executed again, otherwise the control passes to the statement immediately following the loop. THE do - while LOOP

15 Unlike for and while loops in which the condition is checked at the top of the loop, the do-while loop checks the condition at the end of the loop. A do-while loop is executed at least once. Although the braces are optional if a single statement is present, it is a good programming practice to use them to improve the readability of the program. THE do - while LOOP

16 # include void main ( ) { int n1, n2, sum, product; char response; do { cout << “\n Enter two numbers : “; cin >> n1 >> n2; sum = n1 + n2; product = n1 * n1; cout << sum << product << “\n”; cout << “ Press N for No and any other key to continue “; cin >>response; } while (response != ‘N’); }

17 A nested loop is a loop that is inside another loop. The inner loop must finish inside the outer loop that is, the body of the inner loop is fully contained in the outer loop. for(exp1; exp2; exp3) { statement; do { statement; } // end of for loop } while (exp4); // end of do loop is invalid. Why? NESTED LOOPS

18 for (i=1; i<=2; i++) for (j=1; j<=2; j++) cout « i « '\t" « j « '\n"; What is the output? NESTED LOOPS

19

20 Loops & Decisions Relational Operators >, =, <=, != Program statement which alter the flow of exec of program are called “Control Statements” types are Loops & Decision Loops –The for Loop Initail exp, test exp, incre exp –The while Loop Test exp

21 –The do while Loop The body of loop exec at least once, test exp is checked at the end Decisions –The if Statement Multiple statement in the if body –Nesting ifs Inside Loop if can be nested inside loop body

22 Library Function exit() –Causes the program to terminate no matter where ever it is in the listing, value 0 mean successful termination if else statement –If statement let you do one thing if exp is true if exp is false it let you do another getche() Library Function –Get character echo, declared in the conio.h, get the character from keyboard one at a time and display it on the screen

23 The assignment Expression a=b=c=d=10; is valid assignment expression, has lower precedence Nested if else or else if statement –This depict a decision ladder Matching the else –else is matched to the nearest if which does not have it own else The break statement: can occur in the switch and loop, causes to exit the switch and loop, control goes the statement following loop and switch

24 Switch vs if else: switch case variable are either character or int constant. In case of if else constructs let you use unrelated variables as complex as you like –If(SteamPressure*Factor > 56) //statement –else if (day == Thursday) // statement

25 Conditional Operator –Ternary operator op on three operands, test exp, exp2, exp 3, can be used whereever if else can be used –Result=(test exp)?(exp2):(exp3); –?: are conditional operators, if test exp is true, variable Result is assigned value with ?, otherwise with :

26 http://vustudents.ning.com Logical Operators: logically combine the boolean variable if(x>y && y>z) operators are && logical AND, logical OR||, ! Logical Not The continue statement: when ever executed in the loop causes the con to go in the start of the loop, due to result of some unwarranted situations The goto statement : causes the control to go to the label, it is unconditional Jump

27 Unary!, ++, -- Arithmetic*, /, % +, - Relational!=,,>=,<= = =, Logical&& | Conditional? : Assignment=, +=, -=, *=, /=, %= OPERATOR PRECEDENCE SUMMARY


Download ppt "Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition."

Similar presentations


Ads by Google