Chapter 3
-
Upload
amrit-kaur -
Category
Education
-
view
41 -
download
0
Transcript of Chapter 3
Amrit Kaur Looping Constructs in C++
1
Chapter 3: Loop Constructs
A loop construct allow set of statements of a program to be executed certain number of
times. The statement will continue to execute until loop condition remains true. When the
condition becomes false, loop ends and control is passes to the statement following loop.
3.1 The while Loop
While loop continues until the evaluating condition becomes false. The condition
should be a logical expression.
Syntax initial value; while(logical_expression) { statement(s); increment; }
Example 1: A program to print even numbers beween 0 to n where value of n is specified by user. #include<iostream.h> #include<conio.h> void main() { int n, i; clrscr(); cout<<"\n *** Program to print EVEN series upto limit ***"; cout<<"\nEnter Limit "; cin>>n; cout<<"\nEven Number between 0 to "<< n <<endl; for(i=0;i<=n;i=i+2) //here i is loop variable { cout<<i<<" "; } }
3.2 The do...while Loop
The do..while loop is similar to while loop as both executes until the logical expression
becomes false.
Syntax initial value; do { statement(s); increment;
Amrit Kaur Looping Constructs in C++
2
} while(logical_expression);
Example 2 : A program to print even numbers beween 0 to n where value of n is specified by user.
#include<iostream.h> #include<conio.h> void main() { int n, i;
clrscr(); cout<<"\n *** Program to print EVEN series upto limit ***"; cout<<"\nEnter Limit "; cin>>n; cout<<"\nEven Number between 0 to "<< n <<endl; //initalise loop variable i i=0; do //loop execute atleast onec { cout<<i<<" "; i=i+2; // increment } while(i<=n); // loop execute second time if condition is true
}
3.3 Difference between while and do...while loop
1. In while loop, logical expression is evaluated first. If it evaluates to true the body
of while loop will execute otherwise not. On the other hand, in do...while loop,
the body of loop executes first and then the logical expression is evaluated. If
the expression is true, next time the
loop will execute.
2. In while loop, the body will execute
only when the logical expression is
true. On the other hand, in
do...while loop body of loop will
execute at least one time
irrespective of logical expression.
Amrit Kaur Looping Constructs in C++
3
3.4 The for Loop
The for loop is used to repeat the statement or set of statements known or specified
number of times. The for statement consists of the keyword for followed by
parenthesis containing three expressions, each
separated by semicolon. The expressions are
i. Initialisation expression: Used to initialise the
loop variable. It is executed only once.
ii. Test expression / condition: Used to evaluate the
loop condition. It is executed each time control
passes to beginning of loop. The loop will
execute only if condition is true.
iii. Increment or decrement expression: It is always
executed when control return to beginning of the
loop.
Syntax
for(initialisation; test_expression; increment) {
statement(s); }
Example 3: A program to print even numbers beween 0 to n where value of n is specified by user. #include<iostream.h> #include<conio.h> void main() { int n, i; clrscr(); cout<<"\n *** Program to print EVEN series upto limit ***"; cout<<"\nEnter Limit "; cin>>n; cout<<"\nEven Number between 0 to "<< n <<endl; for(i=0;i<=n;i=i+2) //here i is loop variable { cout<<i<<" ";
Amrit Kaur Looping Constructs in C++
4
} }
NOTE:
You can omit the Initialization Expression, but then the looping variable is
initialized before the for loop
You can omit the test_expression / condition, but then the condition is specified
inside the body of the for loop, generally using an if statement.
You can omit the increment, but then the increment expression is written inside
the body of the for loop.
You can omit all three, but then the loop is called Infinite loop
3.5 The break Statements
The break statement is used to exit the loop before the loop condition is re evaluated.
Example 4: A program to print even numbers between 0 to n where value of n is specified by
user and program terminates when i is greater than 10.
#include<iostream.h> #include<conio.h> void main() { int n, i; clrscr(); cout<<"\n *** Program to print EVEN series upto limit ***"; cout<<"\nEnter Limit "; cin>>n; cout<<"\nEven Number between 0 to "<< n <<endl; //initalise loop variable i i=0; do //loop execute atleast onec { cout<<i<<" "; i=i+2; // increment if (i==10) { break; } } while(i<=n); // loop execute second time if condition is true }
3.6 The continue statement
Amrit Kaur Looping Constructs in C++
5
The continue statements is used to skip all the subsequent / remaining instructions
and take the control back to the loop (next increment/decrement value).
Example 4: A program to print even numbers between 0 to n where value of n is specified by
user and increments value of n by 10.
#include<iostream.h> #include<conio.h> void main() { int n, i; clrscr(); cout<<"\n *** Program to print EVEN series upto limit ***"; cout<<"\nEnter Limit "; cin>>n; cout<<"\nEven Number between 0 to "<< n <<endl; //initalise loop variable i i=0; do //loop execute atleast onec {
cout<<i<<"\n"; i=i+2; // increment if(i>20) { n=n+10; cout<<"\nProgram continue to print even nos upto limit n="<<n<<endl; continue; } } while(i<=n); // loop execute second time if condition is true }