SlideShare a Scribd company logo
c++ Lecture 3
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 (20)

PDF
One definition rule - что это такое, и как с этим жить
Platonov Sergey
 
PPTX
Planet of the AOPs
James Ward
 
PPTX
Shell Script Tutorial
Quang Minh Đoàn
 
PDF
Rcpp11
Romain Francois
 
PPTX
Cs1123 6 loops
TAlha MAlik
 
PPT
C++ programming
viancagerone
 
PDF
Let's meet your expectations!
Bartosz Polaczyk
 
PPTX
3.looping(iteration statements)
Hardik gupta
 
TXT
Udp scriptstart
dina_retiz
 
PPT
Advance ROP Attacks
n|u - The Open Security Community
 
PPTX
Presentation on nesting of loops
bsdeol28
 
PPTX
Oop object oriented programing topics
(•̮̮̃•̃) Prince Do Not Work
 
PPTX
Nested loop in C language
ErumShammim
 
PDF
GeoGebra JavaScript CheatSheet
Jose Perez
 
PDF
Loops in JavaScript
Florence Davis
 
PPTX
Loop control in c++
Debre Tabor University
 
PDF
Perl 5.16 new features
Pavel Vlasov
 
PPTX
Operating Systems - A Primer
Saumil Shah
 
PPT
C++ programming
viancagerone
 
PPTX
Switch case and looping
aprilyyy
 
One definition rule - что это такое, и как с этим жить
Platonov Sergey
 
Planet of the AOPs
James Ward
 
Shell Script Tutorial
Quang Minh Đoàn
 
Cs1123 6 loops
TAlha MAlik
 
C++ programming
viancagerone
 
Let's meet your expectations!
Bartosz Polaczyk
 
3.looping(iteration statements)
Hardik gupta
 
Udp scriptstart
dina_retiz
 
Presentation on nesting of loops
bsdeol28
 
Oop object oriented programing topics
(•̮̮̃•̃) Prince Do Not Work
 
Nested loop in C language
ErumShammim
 
GeoGebra JavaScript CheatSheet
Jose Perez
 
Loops in JavaScript
Florence Davis
 
Loop control in c++
Debre Tabor University
 
Perl 5.16 new features
Pavel Vlasov
 
Operating Systems - A Primer
Saumil Shah
 
C++ programming
viancagerone
 
Switch case and looping
aprilyyy
 

Similar to c++ Lecture 3 (20)

PPTX
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
PDF
4th_Ed_Ch03.pdf
ShifatiRabbi
 
PPTX
operating system introduction to programming
farhannisar578
 
PDF
ICP - Lecture 9
hassaanciit
 
PPT
Loops
abdulmanan366
 
PPTX
Loops c++
Shivani Singh
 
PPT
Looping in c++
deekshagopaliya
 
PPT
Looping in c++
deekshagopaliya
 
PPTX
C++ decision making
Zohaib Ahmed
 
PDF
Loops and it's types Presentation -2.pdf
muhammadjamalics
 
PPT
Chapter 5 Looping
GhulamHussain142878
 
PPTX
12-Lec - Repetition For Loop.pptx
AqeelAbbas94
 
PPT
Deeksha gopaliya
deekshagopaliya
 
PDF
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
 
PPTX
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
PDF
Chapter 3 - Flow of Control Part II.pdf
KirubelWondwoson1
 
PPT
Mesics lecture 7 iteration and repetitive executions
eShikshak
 
PPTX
MUST CS101 Lab11
Ayman Hassan
 
PPTX
Lecture 5
Mohammed Khan
 
DOCX
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
 
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
4th_Ed_Ch03.pdf
ShifatiRabbi
 
operating system introduction to programming
farhannisar578
 
ICP - Lecture 9
hassaanciit
 
Loops c++
Shivani Singh
 
Looping in c++
deekshagopaliya
 
Looping in c++
deekshagopaliya
 
C++ decision making
Zohaib Ahmed
 
Loops and it's types Presentation -2.pdf
muhammadjamalics
 
Chapter 5 Looping
GhulamHussain142878
 
12-Lec - Repetition For Loop.pptx
AqeelAbbas94
 
Deeksha gopaliya
deekshagopaliya
 
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
 
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
Chapter 3 - Flow of Control Part II.pdf
KirubelWondwoson1
 
Mesics lecture 7 iteration and repetitive executions
eShikshak
 
MUST CS101 Lab11
Ayman Hassan
 
Lecture 5
Mohammed Khan
 
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
 
Ad

More from sajidpk92 (8)

PPTX
Cauchy riemann equations
sajidpk92
 
PPTX
STEP(Solar Technology for Energy Production)
sajidpk92
 
PDF
c++ Lecture 4
sajidpk92
 
PPT
c++ Lecture 2
sajidpk92
 
PPT
c++ Lecture 1
sajidpk92
 
PPT
Lecture 1
sajidpk92
 
PDF
Lecture 4
sajidpk92
 
PPT
basic c++(1)
sajidpk92
 
Cauchy riemann equations
sajidpk92
 
STEP(Solar Technology for Energy Production)
sajidpk92
 
c++ Lecture 4
sajidpk92
 
c++ Lecture 2
sajidpk92
 
c++ Lecture 1
sajidpk92
 
Lecture 1
sajidpk92
 
Lecture 4
sajidpk92
 
basic c++(1)
sajidpk92
 
Ad

Recently uploaded (20)

PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPTX
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
PDF
Comprehensive Guide to Writing Effective Literature Reviews for Academic Publ...
AJAYI SAMUEL
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PPTX
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
PPTX
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
PPTX
Modern analytical techniques used to characterize organic compounds. Birbhum ...
AyanHossain
 
PPTX
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
PPTX
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PPTX
nutriquiz grade 4.pptx...............................................
ferdinandsanbuenaven
 
PPTX
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
SAMPLING: DEFINITION,PROCESS,TYPES,SAMPLE SIZE, SAMPLING ERROR.pptx
PRADEEP ABOTHU
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
digestive system for Pharm d I year HAP
rekhapositivity
 
Comprehensive Guide to Writing Effective Literature Reviews for Academic Publ...
AJAYI SAMUEL
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
Presentation: Climate Citizenship Digital Education
Karl Donert
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
Modern analytical techniques used to characterize organic compounds. Birbhum ...
AyanHossain
 
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
nutriquiz grade 4.pptx...............................................
ferdinandsanbuenaven
 
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
PPT on the Development of Education in the Victorian England
Beena E S
 
SAMPLING: DEFINITION,PROCESS,TYPES,SAMPLE SIZE, SAMPLING ERROR.pptx
PRADEEP ABOTHU
 

c++ Lecture 3

  • 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”); }