Lecture-9

Programming Fundamental
Instructor Name:
Lecture-9
Today’s Lecture
 Repetition Structure (Loops) continued
 For loop
 Behavior of For Loop
 For Loop Structure & Syntax
 Execution of For Loop with example
 Nested loops & their execution strategy
 Continue Statement
 Use of Continue & Break in Loops (Difference)
2
For Loop
Behaviour of Loops
 Three mandotry elements of loops are:
 In a loop, a variable is initialize at start
 A condition is set for loop termination
 To meet the terminating condition we do something in the body of
the loop to affect condition
 For example in all our previous lecture when we use num=num-1 this
change the terminating condition i.e infinite loop
 We have seen that in the loops we discussed so far the initialization and
way to change the value of variable were not actually the part of loop
structure.
3
For Loop
For Loop Structure
 We conclude from discussion that there are three things in a loop
1. Initialization
2. A continuation/termination condition
3. Changing the value of the condition variable (increment/decrement)
 C++ provides us a loop known as for loop that implements these three
things in its structure
 For loop is used to perform repetition tasks for a known number of
repetitions
 That’s why for loop is also known as counter loop.
 A counter loop is one where you already know about the number of
iteration s that a loop will perform
4
For Loop
For Loop Structure
 The syntax of for loop is
for ( initialization ; continuation condition ; incrementing condition )
{
statement(s) ;
}
 Initialization is used to initialize the value of variable. It define the
starting point of the loop.
 Initialization occurs once in the life of the loop and it is the very first
condition or statement that executes when a loop starts
 The overall execution depends on the initialization value that a loop have
 For example if we initialize the variable counter to 0 the loop will start
with value 0 as starting value.
5
For Loop
For Loop Structure
 Continuation / termination condition is used to define the number of
iteration that a loop will perform
 A continuation condition allows the body of the loop to execute statements
till the condition is true
 Once the continuation condition becomes false the loop will terminate
 For example if the continuation condition for the variable counter is
counter <10
 The body of for loop will execute till the value of counter remains less
than 10
 Once it becomes 10 or greater then 10 the loop will terminate
6
For Loop
For Loop Structure
 Incrementing condition increments the value of the variable that was
initialized at the start of the loop to make continuation condition false
 We can write incrementing condition on one of the following form
counter=co unter+1 or counter ++
 This statement adds 1 to the existing value of variable counter
 We will write for loop structure in c++ as
for ( counter = 0 ; counter < 10 ; counter = counter +1 )
{
cout << counter << endl;
}
7
For Loop
For Loop Structure
 Lets discuss how this loop structure will execute
 When the control goes to for statement first time, it sets the value of
variable counter to 0
 tests the condition (i.e. counter < 10). If it is true, then executes the body of
the loop.
 it displays the value of counter which is 0 for the first execution.
 Then runs the incrementing statement and the value of counter becomes 1
 Then again test the condition and executes body if condition is true
 And process repeats in a cycle till the condition becomes false
8
For Loop
For Loop Structure
 Is there be any situation where the body of for loop will never execute as
in the case of while loop?
 In what cases the for loop may become an infinite loop?
 Among while, do-while, and for loop which loop will be your preference?
OR
 How you will select a loop (among while, do while and for) for you
program?
9
For Loop
For Loop Program
Write a program that prints the table of number 2
using namespace std;
#include<iostream>
main ( )
{
int counter;
for ( counter = 1 ; counter <= 10 ; counter ++) {
cout << “2 x “ << counter << “ = “ << 2 * counter << “\n” ;
}
}
10
For Loop
For Loop Program
The output of the program will be:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
11
For Loop
For Loop Program
 What you need to do if you want to make this program a general program
to print table of any number given by the user?
Class activity.
 Modify the program to make it general to print table of any number
 Further modify to make it more general to print table of any number upto
a number (multiplier) entered by the user
12
Loop
Order of movement of control in for loop:
First time:
Expression 1-> Expression 2->Loop body -> Expression 3
Second time and onward:
Expression 2->Loop body -> Expression 3
Properties of Expression-1
Expression1 can initialize the more than one variable. For example:.
main(){
int i,j,k;
for(i=0,j=2,k=1;i<=4;i++)
{
cout<<i+j+k;
}
Getch();
}
Output: 3 4 5 6 7
Properties of Expression-1
Expression1 is optional. For example:
main(){
int i=1;
for(;i<=4;i++){
cout<<i;
}
getch();
}
Output: 1 2 3 4
Properties of Expression-2
Expression2 is also optional. For example:
main(){
int j;
for(j=0; ;j++){
cout<<j;
if(j>2)
break;
}
Getch();
}
Output: 0 1 2
Properties of Expression-2
If expression2 is zero means condition is false and any non zero number
means condition is true. For example
main(){
int i;
for(i=0;-5 ;i++){
cout<<i;
if(i==3)
break;
}
Getch();
}
Output: 0 1 2 3
Properties of Expression-2
If expression2 is zero means condition is false and any non zero number
means condition is true. For example
main(){
int i;
for(i=5;0 ;i++){
cout<<i;
}
Getch();
}
Output:
Properties of Expression-3
It is called as instrumentation expression. Task of this expression is to increment the
variable. Properties:
We can increment more than one variable at the same time in the expression3. For
example
main(){
int i,j,k;
for(i=0,j=0,k=0;k<=3;i++,++j,k+=2))
{
cout<<i+j+k;
}
Getch();
}
Output: 0 4
Properties of Expression-3
It is called as instrumentation expression. Task of this expression is to increment the
variable. Properties:
We can increment more than one variable at the same time in the expression3. For
example
main(){
int i,j=0;
for(i=0;i<=3;++i,i++,++j ){
cout<<i<<”,”<<j;;
}
Getch();
}
Output: 0 0 2 1
Properties of Expression-3
Expression 3 is also optional. For example
main(){
int i;
for(i=0;i<=3; ){
cout<<i++;
}
Getch();
}
Output: 0 1 2 3
Loop body:
Loop body contains the part of code which we have to execute multiple numbers of
times.
Properties of loop body:
If loop body contain only one statement then brace is optional. For example:
main(){
int i,j=0;
for(i=0;i<=3;++i,i++,++j )
cout<<i<<j;
Getch();
}
Output: 0 0 2 1
Loop body:
Loop without body is possible. For example
main(){
int i;
for(i=0;i<=10;i++);
cout<<i;
Getch();
}
Output: 11
Sample Program (Assignment Operator)
find the sum of the squares of the integers
from 1 to n. Where n is a positive value entered by the user (i.e.
Sum = 1 2 + 2 2 + 3 2 + n2
Nested Loop
What is a Nested Loop?
 A loop within a loop is called nested loop.
 C++ allows at least 256 levels of nesting
 The syntax of nested loop is as under
for ( initialization; condition; increment )
{
for ( initialization; condition; increment )
{
statement(s);
}
statement(s);
}
You can nest while and do while loop in the same fashion
25
Nested LOOP
A loop with a loop is called Nested loop
•
Inner loop will be executed completely for each iteration of outer
loop
•
Inner loop will executed multiple times of outer loop
Nested Loop
Nested Loop Execution
27
Nested Loop
Nested Loop Example
Write a program that prints all the combinations of the digits from 0 to 5 i.e.
00,01,12,13,50,55 and etc
using namespace std;
#include <iostream>
main () {
int i, j;
for(i=0; i<=5; i++) {
for(j=0; j <= 5; j++) {
cout << i << j <<" \t";
}
cout <<"\n";
}
}
28
Nested Loop
Nested Loop Example – Output
00 01 02 03 04 05
10 11 12 13 14 15
20 21 22 23 24 25
30 31 32 33 34 35
40 41 42 43 44 45
50 51 52 53 54 55
29
Nested LOOP
For(int i=1; i<=3; i++)
For (int j=1; j<=3; j++)
cout<< j;
Exercise Program
for ( int row = 1; row <= 10; ++row )
{
for ( int col = 1; col <= row; ++col )
cout << '*';
cout << '\n';
}
for (int row = 10; row >= 1; --row )
{
for ( int col = 1; col <= row; ++col )
cout << '*';
cout << '\n';
}
break;
continue;
Break statement
 Break statement interrupt the flow of control.
 In some case, we want only true case to be executed and
remain to be skipped
 For this purpose we write the break statement right after the
true case.
 Break statement interrupt the flow of control and control
jumps out of the repetitive structure
Break statement
#include<iostream.h>
#include<conio.h>
main()
{
int num, j;
cin>>num;
j=2;
while(j<num)
{
if(num%j==0)
{
cout<<"number is not prime";
break;
}
j++;
}
if(num==j)
cout<< "number is prime";
getch();
}
Continue statement
 There is another statement relating to loops that is continue
statement.
 We may have lot of code in the body of loop
 Early part of code is executed every time
 Remaining portion is executed in certain cases and may not
executed in other cases. But loop should be continue.
 Continue forces the immediate next iteration of loop.
 Statement of the loop body will not executed after continue
statement
Continue statement-example
int j;
J=2;
For (int i=0; i<=3; i++)
{
if
If(i==j)
Continue;
Else
Cout<<i;
}
Nested LOOP
Write a program that print different combination of digits between 1----3
(excluding equal combination)
Nested LOOP
#include<iostream.h>
#include<conio.h>
main()
{
for(int i=1; i<=3; i++)
for (int j=1; j<=3; j++)
{
if (i==j)
continue;
cout<<i<<"------"<<j<<"\n";
}
getch();
}
Nested LOOP
main()
{
int i,j;
i=1;
j=1;
while(i++<=100)
{
while(j++<=200)
{
if(j==150)
break;
else
cout<<i<<"----"<<j<<"\n";
}
}
getch();
}
A while loop nested in for loop
A while loop nested in for loop
long sum;
int i, j, count=10;
for(i=1; i<=count; i++)
{
sum=0;
j=1;
while(j<=i)
{
//++j;
sum=sum+j;
cout<<j<<"+";
++j;
}
cout<<"="<<sum;
cout<<endl;
}
44