0% found this document useful (0 votes)
6 views47 pages

Lecture-13 (17-10-2023)

The document discusses loops in computer programming, specifically focusing on the 'for', 'while', and 'do-while' loops, along with the 'break' and 'continue' statements. It includes example programs that demonstrate how to print patterns using loops and explains the syntax and behavior of these control structures. Additionally, it highlights the differences between 'break' and 'continue' and provides insights into infinite loops and their implications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views47 pages

Lecture-13 (17-10-2023)

The document discusses loops in computer programming, specifically focusing on the 'for', 'while', and 'do-while' loops, along with the 'break' and 'continue' statements. It includes example programs that demonstrate how to print patterns using loops and explains the syntax and behavior of these control structures. Additionally, it highlights the differences between 'break' and 'continue' and provides insights into infinite loops and their implications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Problem Solving and

MA144:
Computer Programming

Lecture-13
Loops, break, continue
Three important pyramids
Write a program to print the following pattern.
#include<iostream>
using namespace std;
int main()
{ int rows,space,i,j;
cout<<"enter no. of rows: ";
cin>>rows;
for(i=1;i<=rows;i++)
{
for(space=1;space<=rows-i;space++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<"*";

cout<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{ int rows,space,i,j;
cout<<"enter no. of rows: ";
cin>>rows;
for(i=1;i<=rows;i++) two spaces
{
for(space=1;space<=rows-i;space++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
One space
cout<<"* ";

cout<<endl;
}
return 0;
}
Write a program to print the following pattern.
Exercise
Write a program to print the following pattern.
#include<iostream>
using namespace std;
int main()
{ int rows,space,i,j;
cout<<"enter no. of rows: ";
cin>>rows;
for(i=0;i<=rows-1;i++)
{
for(space=0;space<i;space++)
cout<<" ";
for(j=space;j<rows;j++)
cout<<"* ";

cout<<endl;
}
return 0;
}
Loops
 Group of statements that are executed
repeatedly while some condition
remains true
 Each execution of the group of
statements is called an iteration of the
loop
 for for()
{ statement-1;
 while statement-2;
 do-while }
statement-3;
for loop
for(expr1; expr2; expr3)
{ statement-1;
statement-2;
statement-3;
}
 expr1 (initialization) : initialize parameters
 expr2 (loop-continuation test): loop continues
if expression is non-zero
 expr3 (update): used to alter the value of the
parameters after each iteration
 Initialization, loop-continuation test, and
update can contain arithmetic expressions

for(i=a+b; i!=2*c%d; i+=a/d)

 Update may be negative (decrement)


for (i = 10; i >= 0; --i)
 We can give several expressions separated by
commas in place of expr1 and expr3 in a for
loop to do multiple assignments
What is the output?
 We can give several expressions separated by
commas in place of expr1 and expr3 in a for
loop to do multiple assignments
; ; is
necessary
No output
Semicolon ;
is same as
{

} empty block
Guess the output of the following program.
Guess the output of the following program.
Infinite Loops
for(; ;) while(1)
{ statement1; { statement1;
statement2; statement2;
statement3; statement3;
} }

do
Condition is necessary in
{ statement1;
while and do-while
statement2;
statement3;
}while(1)
Infinite for Loop
Another
Infinite for Loop
Find out the output of the following program.
Relation between for and while loops.
for(expr1; expr2; expr3)
{ statement-1;
statement-2;
statement-3;
}

expr1;
same as while(expr2)
{ statement-1;
statement-2;
statement-3;
expr3;
}
The break statement
• Break out of the loop body { }
∎ can use with while, do-while, for, switch
∎ does not work with if, if-else

[Error] break statement not


within loop or switch
• ends only the innermost loop/switch
that contains it.
• ends only the innermost loop/switch
that contains it.
The continue statement
Syntax
continue;
• opposite to that of break statement,
• instead of terminating the loop, it forces to
execute the next iteration of the loop
• when the continue statement is executed
in the loop,
(i) the code inside the loop following
the continue statement will be skipped
(ii) next iteration of the loop will begin.
for(i=1; i<=10; i++)
{ statement-1;
statement-2;
continue;
statement-3;
statement-4;
}
Difference between continue and break

break continue
• exits loop • exits the current
iteration of the loop
What would be the output ?
Usage of both continue and break

What would be the output ?


The continue & break statements
[Error] continue statement
not within a loop

You might also like