SlideShare a Scribd company logo
1 of 25
INSTRUCTOR:
ENGR. AFSHAN ASIM
CHAPTER # 3
LOOPS & DECISIONS
OBJECTIVES
• Relational operators.
• For Loop
• While Loop
• do while Loop
• Nested Loop
RELATIONAL OPERATORS
Operator Meaning
> Greater than
< Less than
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
RELATIONAL OPERATORS (CONTD…)
int a=12; //assignment statement
int b=34; //assignment statement
(b<34) //false or 0
(b<=34) //true or 1
(a==12) //true or 1
(b!=35) //true or 1
(a>14) //false or 0
(a>=10) //true or 1
LOOPS
• Three kinds of loops
• For
• While
• Do-while
FOR LOOP
Syntax
•Single Statement Loop
for(variable initialization, condition, variable update)
statement; //executed if condition true
•Multi Statement Loop
for(variable initialization, condition, variable update)
{ //executed if condition true
statement1;
statement2;
}
FOR LOOP FLOW CHART
Initialization
Expression
Body of Loop
Increment
Expression
Test
Expression
Exit
false
True
FOR LOOP EXAMPLE
//single statement loop
#include<iostream>
using namespace std;
void main()
{
int i;
for(i=0;i<10;i++)
cout<<i<<endl;
system(“pause”);
}
FOR LOOP EXAMPLE
//multi statement loop
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i;
for(i=0;i<10;i+=2)
{ //loop body starts
cout<<setw(4)<<i;
int j=i*i*i;
cout<<setw(6)<<j<<endl;
} //loop body ends
system(“pause”);
}
BLOCK & VARIABLE VISIBILITY
//multi statement loop
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i;
for(i=0;i<10;i++)
{ //loop body starts
cout<<setw(4)<<I;
int j=i*i*i;
cout<<setw(6)<<j<<endl;
} //loop body ends
cout<<j; //ERROR
system(“pause”);
}
FOR LOOP VARIATIONS
//increment expression variations
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=10;i>0;i--)
{ //loop body starts
cout<<i;
cout<<endl;
} //loop body ends
system(“pause”);
}
FOR LOOP VARIATIONS
//variables defined in for statement
#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<10;i++)
{ //loop body starts
cout<<i;
cout<<endl;
} //loop body ends
system(“pause”);
}
FOR LOOP VARIATIONS
//multiple initialization and increment
expressions
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=0,alpha=100;i<10;i++,alpha--)
{ //loop body starts
……..
cout<<i;
cout<<endl;
} //loop body ends
system(“pause”);
}
TASK
What happens if you use for loop in the
following manner
•for(;;)
•for(;;);
(Submit your answers in the next class)
WHILE LOOP
Syntax
•Single Statement while Loop
while(test expression)
statement;
•Multi Statement while Loop
while(test expression)
{
Body of loop
}
WHILE LOOP FLOW CHART
Body of Loop
Test
Expression
Exit
false
True
WHILE LOOP EXAMPLE
#include<iostream>
using namespace std;
int main()
{
int i=0;
while(i<10)
{
cout<<i<<endl;
i++;
}
system(“pause”);
}
DO WHILE LOOP
Syntax
do
{
Body of loop
}
while(test expression);
DO WHILE LOOP FLOW CHART
Body of Loop
Test
Expression
Exit
false
True
DO WHILE LOOP EXAMPLE
#include<iostream>
using namespace std;
int main()
{
int i=0;
do
{
cout<<i<<endl;
i++;
} while(i<10);
system(“pause”);
}
NESTED LOOPS
• Loops inside another loop
• Example
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<“Loop2”;
}
cout<<“nLoop1 “;
}
Inner Outer
NESTED LOOPS EXAMPLE
• Program to print the following
pattern
*
* *
* * *
* * * *
CONTD…
#include<iostream>
using namespace std;
int main()
{
int num=1;
for(int i=0;i<4;i++)
{
for(int j=0;j<num;j++)
{
cout<<“*”;
}
num++;
cout<<endl;
}
system(“pause”);
}
c++ Lecture 3

More Related Content

What's hot

One definition rule - что это такое, и как с этим жить
One definition rule - что это такое, и как с этим житьOne definition rule - что это такое, и как с этим жить
One definition rule - что это такое, и как с этим житьPlatonov Sergey
 
Planet of the AOPs
Planet of the AOPsPlanet of the AOPs
Planet of the AOPsJames Ward
 
Let's meet your expectations!
Let's meet your expectations!Let's meet your expectations!
Let's meet your expectations!Bartosz Polaczyk
 
3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)Hardik gupta
 
Udp scriptstart
Udp scriptstartUdp scriptstart
Udp scriptstartdina_retiz
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loopsbsdeol28
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C languageErumShammim
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetJose Perez
 
Perl 5.16 new features
Perl 5.16 new featuresPerl 5.16 new features
Perl 5.16 new featuresPavel Vlasov
 
Operating Systems - A Primer
Operating Systems - A PrimerOperating Systems - A Primer
Operating Systems - A PrimerSaumil Shah
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and loopingaprilyyy
 

What's hot (20)

One definition rule - что это такое, и как с этим жить
One definition rule - что это такое, и как с этим житьOne definition rule - что это такое, и как с этим жить
One definition rule - что это такое, и как с этим жить
 
Planet of the AOPs
Planet of the AOPsPlanet of the AOPs
Planet of the AOPs
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
 
Rcpp11
Rcpp11Rcpp11
Rcpp11
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Let's meet your expectations!
Let's meet your expectations!Let's meet your expectations!
Let's meet your expectations!
 
3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)
 
Udp scriptstart
Udp scriptstartUdp scriptstart
Udp scriptstart
 
Advance ROP Attacks
Advance ROP AttacksAdvance ROP Attacks
Advance ROP Attacks
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheet
 
Loops in JavaScript
Loops in JavaScriptLoops in JavaScript
Loops in JavaScript
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
 
Perl 5.16 new features
Perl 5.16 new featuresPerl 5.16 new features
Perl 5.16 new features
 
Operating Systems - A Primer
Operating Systems - A PrimerOperating Systems - A Primer
Operating Systems - A Primer
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 

Similar to c++ Lecture 3

FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
Arduino section programming slides
Arduino section programming slidesArduino section programming slides
Arduino section programming slidesvivek k
 
Arduino Section Programming - from Sparkfun
Arduino Section Programming - from SparkfunArduino Section Programming - from Sparkfun
Arduino Section Programming - from SparkfunJhaeZaSangcapGarrido
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and loopingChaAstillas
 
Computer programming
Computer programmingComputer programming
Computer programmingXhyna Delfin
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptxAqeelAbbas94
 
Arduino sectionprogramming slides
Arduino sectionprogramming slidesArduino sectionprogramming slides
Arduino sectionprogramming slidesJorge Joens
 
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statementsVladislav Hadzhiyski
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)SURBHI SAROHA
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11Uilian Ries
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)jewelyngrace
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constantsTAlha MAlik
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 

Similar to c++ Lecture 3 (20)

MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 
Loops c++
Loops c++Loops c++
Loops c++
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Arduino section programming slides
Arduino section programming slidesArduino section programming slides
Arduino section programming slides
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
Arduino Section Programming - from Sparkfun
Arduino Section Programming - from SparkfunArduino Section Programming - from Sparkfun
Arduino Section Programming - from Sparkfun
 
Loops
LoopsLoops
Loops
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Computer programming
Computer programmingComputer programming
Computer programming
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
 
Arduino sectionprogramming slides
Arduino sectionprogramming slidesArduino sectionprogramming slides
Arduino sectionprogramming slides
 
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statements
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
3 rd animation
3 rd animation3 rd animation
3 rd animation
 
PLSQL (1).ppt
PLSQL (1).pptPLSQL (1).ppt
PLSQL (1).ppt
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constants
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 

More from sajidpk92

Cauchy riemann equations
Cauchy riemann equationsCauchy riemann equations
Cauchy riemann equationssajidpk92
 
STEP(Solar Technology for Energy Production)
STEP(Solar Technology for Energy Production)STEP(Solar Technology for Energy Production)
STEP(Solar Technology for Energy Production)sajidpk92
 
c++ Lecture 4
c++ Lecture 4c++ Lecture 4
c++ Lecture 4sajidpk92
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2sajidpk92
 
c++ Lecture 1
c++ Lecture 1c++ Lecture 1
c++ Lecture 1sajidpk92
 
basic c++(1)
basic c++(1)basic c++(1)
basic c++(1)sajidpk92
 

More from sajidpk92 (8)

Cauchy riemann equations
Cauchy riemann equationsCauchy riemann equations
Cauchy riemann equations
 
STEP(Solar Technology for Energy Production)
STEP(Solar Technology for Energy Production)STEP(Solar Technology for Energy Production)
STEP(Solar Technology for Energy Production)
 
c++ Lecture 4
c++ Lecture 4c++ Lecture 4
c++ Lecture 4
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
 
c++ Lecture 1
c++ Lecture 1c++ Lecture 1
c++ Lecture 1
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
basic c++(1)
basic c++(1)basic c++(1)
basic c++(1)
 

Recently uploaded

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Recently uploaded (20)

fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

c++ Lecture 3

  • 1.
  • 3. OBJECTIVES • Relational operators. • For Loop • While Loop • do while Loop • Nested Loop
  • 4. RELATIONAL OPERATORS Operator Meaning > Greater than < Less than == Equal to != Not equal to >= Greater than or equal to <= Less than or equal to
  • 5. RELATIONAL OPERATORS (CONTD…) int a=12; //assignment statement int b=34; //assignment statement (b<34) //false or 0 (b<=34) //true or 1 (a==12) //true or 1 (b!=35) //true or 1 (a>14) //false or 0 (a>=10) //true or 1
  • 6. LOOPS • Three kinds of loops • For • While • Do-while
  • 7. FOR LOOP Syntax •Single Statement Loop for(variable initialization, condition, variable update) statement; //executed if condition true •Multi Statement Loop for(variable initialization, condition, variable update) { //executed if condition true statement1; statement2; }
  • 8. FOR LOOP FLOW CHART Initialization Expression Body of Loop Increment Expression Test Expression Exit false True
  • 9. FOR LOOP EXAMPLE //single statement loop #include<iostream> using namespace std; void main() { int i; for(i=0;i<10;i++) cout<<i<<endl; system(“pause”); }
  • 10. FOR LOOP EXAMPLE //multi statement loop #include<iostream> #include<iomanip> using namespace std; int main() { int i; for(i=0;i<10;i+=2) { //loop body starts cout<<setw(4)<<i; int j=i*i*i; cout<<setw(6)<<j<<endl; } //loop body ends system(“pause”); }
  • 11. BLOCK & VARIABLE VISIBILITY //multi statement loop #include<iostream> #include<iomanip> using namespace std; int main() { int i; for(i=0;i<10;i++) { //loop body starts cout<<setw(4)<<I; int j=i*i*i; cout<<setw(6)<<j<<endl; } //loop body ends cout<<j; //ERROR system(“pause”); }
  • 12. FOR LOOP VARIATIONS //increment expression variations #include<iostream> using namespace std; int main() { int i; for(i=10;i>0;i--) { //loop body starts cout<<i; cout<<endl; } //loop body ends system(“pause”); }
  • 13. FOR LOOP VARIATIONS //variables defined in for statement #include<iostream> using namespace std; int main() { for(int i=0;i<10;i++) { //loop body starts cout<<i; cout<<endl; } //loop body ends system(“pause”); }
  • 14. FOR LOOP VARIATIONS //multiple initialization and increment expressions #include<iostream> using namespace std; int main() { int i; for(i=0,alpha=100;i<10;i++,alpha--) { //loop body starts …….. cout<<i; cout<<endl; } //loop body ends system(“pause”); }
  • 15. TASK What happens if you use for loop in the following manner •for(;;) •for(;;); (Submit your answers in the next class)
  • 16. WHILE LOOP Syntax •Single Statement while Loop while(test expression) statement; •Multi Statement while Loop while(test expression) { Body of loop }
  • 17. WHILE LOOP FLOW CHART Body of Loop Test Expression Exit false True
  • 18. WHILE LOOP EXAMPLE #include<iostream> using namespace std; int main() { int i=0; while(i<10) { cout<<i<<endl; i++; } system(“pause”); }
  • 19. DO WHILE LOOP Syntax do { Body of loop } while(test expression);
  • 20. DO WHILE LOOP FLOW CHART Body of Loop Test Expression Exit false True
  • 21. DO WHILE LOOP EXAMPLE #include<iostream> using namespace std; int main() { int i=0; do { cout<<i<<endl; i++; } while(i<10); system(“pause”); }
  • 22. NESTED LOOPS • Loops inside another loop • Example for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { cout<<“Loop2”; } cout<<“nLoop1 “; } Inner Outer
  • 23. NESTED LOOPS EXAMPLE • Program to print the following pattern * * * * * * * * * *
  • 24. CONTD… #include<iostream> using namespace std; int main() { int num=1; for(int i=0;i<4;i++) { for(int j=0;j<num;j++) { cout<<“*”; } num++; cout<<endl; } system(“pause”); }