SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Plus de 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
 

Plus 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)
 

Dernier

Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 

Dernier (20)

Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 

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