SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

Planet of the AOPs
Planet of the AOPsPlanet of the AOPs
Planet of the AOPs
James Ward
 
3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)
Hardik gupta
 
Udp scriptstart
Udp scriptstartUdp scriptstart
Udp scriptstart
dina_retiz
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
bsdeol28
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
ErumShammim
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
aprilyyy
 

La actualidad más candente (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 a 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 2
rohassanie
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
ChaAstillas
 
Computer programming
Computer programmingComputer programming
Computer programming
Xhyna Delfin
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
AqeelAbbas94
 
Arduino sectionprogramming slides
Arduino sectionprogramming slidesArduino sectionprogramming slides
Arduino sectionprogramming slides
Jorge Joens
 
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_constants
TAlha MAlik
 

Similar a 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
 

Más de sajidpk92

Cauchy riemann equations
Cauchy riemann equationsCauchy riemann equations
Cauchy riemann equations
sajidpk92
 
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 4
sajidpk92
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
sajidpk92
 
c++ Lecture 1
c++ Lecture 1c++ Lecture 1
c++ Lecture 1
sajidpk92
 
basic c++(1)
basic c++(1)basic c++(1)
basic c++(1)
sajidpk92
 

Más de 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)
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
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
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
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
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
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...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

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