27
•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 LOOPS http://vustudents.ning.com

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

Embed Size (px)

Citation preview

Page 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

•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.

LOOPSLOOPS

http://vustudents.ning.com

Page 2: 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

THE THE forfor LOOP 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 { }.

Page 3: 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

THE THE forfor LOOP 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.

Page 4: 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

#include <iostream. h>#include <conio. h>

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 LoopExample of a for Loop

Page 5: 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

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 VARIATIONS OF forfor LOOP LOOP

Page 6: 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

VARIATIONS OF VARIATIONS OF forfor LOOP LOOP4. 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;

Page 7: 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

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 VARIATIONS OF forfor LOOP LOOP

Page 8: 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

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 VARIATIONS OF forfor LOOP LOOP

Page 9: 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

VARIATIONS OF VARIATIONS OF forfor LOOP LOOP

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.

Page 10: 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

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 <isotream. h>

void main ( ){

int n,m; for (n=0,m=0; n<=5; ++n, m+=2)

cout « n « "\t" « m « '\n";

}

THE COMMA OPERATORTHE COMMA OPERATOR

Page 11: 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

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 LOOPTHE while LOOP

Page 12: 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

#include <iostream. h>void main ( ) {

int no=10; while (no > 0)

{cout « no « '\t"

« no*no « '\t" « no*no*no « '\n"; no --; }

}

THE while LOOPTHE while LOOP

Page 13: 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

• 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 LOOPTHE while LOOP

Page 14: 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

The general form of the loop isdo {

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 LOOPTHE do - while LOOP

Page 15: 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

• 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 LOOPTHE do - while LOOP

Page 16: 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

# include <iostream.h>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’);

}

Page 17: 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

• 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 LOOPSNESTED LOOPS

Page 18: 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

for (i=1; i<=2; i++)

for (j=1; j<=2; j++)cout « i « '\t" « j « '\n";

What is the output?

NESTED LOOPSNESTED LOOPS

Page 19: 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
Page 20: 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

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

Page 21: 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

– 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

Page 22: 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

• 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

Page 23: 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

• The assignment Expressiona=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

Page 24: 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

• 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

Page 25: 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

• 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 :

Page 26: 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

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

Page 27: 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

Unary !, ++, --

Arithmetic *, /, %

+, -

Relational !=, <,>,>=,<=

= =,

Logical &&

| |

Conditional ? :

Assignment =, +=, -=, *=, /=, %=

OPERATOR PRECEDENCE SUMMARY