c++ lecture 3
TRANSCRIPT
![Page 1: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/1.jpg)
![Page 2: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/2.jpg)
INSTRUCTOR: ENGR. AFSHAN ASIM
CHAPTER # 3LOOPS & DECISIONS
![Page 3: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/3.jpg)
OBJECTIVES
• Relational operators.• For Loop• While Loop• do while Loop• Nested Loop
![Page 4: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/4.jpg)
RELATIONAL OPERATORS
Operator
Meaning
> Greater than
< Less than
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
![Page 5: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/5.jpg)
RELATIONAL OPERATORS (CONTD…)
int a=12; //assignment statementint 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
![Page 6: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/6.jpg)
LOOPS
• Three kinds of loops• For •While•Do-while
![Page 7: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/7.jpg)
FOR LOOP
Syntax
•Single Statement Loopfor(variable initialization, condition, variable update)statement; //executed if condition true
•Multi Statement Loopfor(variable initialization, condition, variable update){ //executed if condition truestatement1; statement2;}
![Page 8: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/8.jpg)
FOR LOOP FLOW CHART
Initialization Expression
Body of Loop
Increment Expression
Test Expressio
nExit
false
True
![Page 9: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/9.jpg)
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”);}
![Page 10: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/10.jpg)
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 startscout<<setw(4)<<i;int j=i*i*i;cout<<setw(6)<<j<<endl;} //loop body endssystem(“pause”);}
![Page 11: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/11.jpg)
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 startscout<<setw(4)<<I;int j=i*i*i;cout<<setw(6)<<j<<endl;} //loop body endscout<<j; //ERRORsystem(“pause”);}
![Page 12: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/12.jpg)
FOR LOOP VARIATIONS
//increment expression variations#include<iostream>using namespace std;int main(){int i;for(i=10;i>0;i--) { //loop body startscout<<i;cout<<endl;} //loop body endssystem(“pause”);}
![Page 13: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/13.jpg)
FOR LOOP VARIATIONS
//variables defined in for statement#include<iostream>using namespace std;int main(){for(int i=0;i<10;i++) { //loop body startscout<<i;cout<<endl;} //loop body endssystem(“pause”);}
![Page 14: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/14.jpg)
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 endssystem(“pause”);}
![Page 15: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/15.jpg)
TASK
What happens if you use for loop in the following manner
•for(;;)•for(;;);
(Submit your answers in the next class)
![Page 16: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/16.jpg)
WHILE LOOP
Syntax
•Single Statement while Loopwhile(test expression)statement;
•Multi Statement while Loopwhile(test expression){Body of loop}
![Page 17: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/17.jpg)
WHILE LOOP FLOW CHART
Body of Loop
Test Expressio
nExit
false
True
![Page 18: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/18.jpg)
WHILE LOOP EXAMPLE
#include<iostream>using namespace std;int main(){int i=0;while(i<10){cout<<i<<endl;i++;}system(“pause”);}
![Page 19: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/19.jpg)
DO WHILE LOOP
Syntaxdo{Body of loop}while(test expression);
![Page 20: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/20.jpg)
DO WHILE LOOP FLOW CHART
Body of Loop
Test Expressio
nExit
false
True
![Page 21: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/21.jpg)
DO WHILE LOOP EXAMPLE
#include<iostream>using namespace std;int main(){int i=0;do{cout<<i<<endl;i++;} while(i<10);system(“pause”);}
![Page 22: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/22.jpg)
NESTED LOOPS
• Loops inside another loop• Examplefor(int i=0;i<3;i++){for(int j=0;j<3;j++){cout<<“Loop2”;}cout<<“\nLoop1 “;}
Inner Outer
![Page 23: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/23.jpg)
NESTED LOOPS EXAMPLE
• Program to print the following pattern
* * * * * * * * * *
![Page 24: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/24.jpg)
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”);}
![Page 25: c++ Lecture 3](https://reader035.vdocuments.us/reader035/viewer/2022062303/557d4663d8b42ae71a8b4743/html5/thumbnails/25.jpg)