Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of...
Transcript of Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of...
![Page 1: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/1.jpg)
ControlStructure(2)
ISNE261102FacultyofEngineeringChiangMaiUniversity
![Page 2: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/2.jpg)
AssignmentOperators
• C++providesseveralassignmentoperators forabbreviatingassignmentexpressions.Forexample,thestatement
c=c+3;canbeabbreviatedwiththeadditionassignmentoperator+=as
c+=3;
![Page 3: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/3.jpg)
AssignmentOperators
![Page 4: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/4.jpg)
IncrementandDecrementOperators
• C++alsoprovidesincrementanddecrementoperatorsforadding1toorsubtracting1fromthevalueofanumericvariable.
c=c+1è ++c orc++
Incre/Dec
Beforeexpression
Afterexpression
Beforeexpression
Afterexpression
![Page 5: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/5.jpg)
IncrementandDecrementOperators
556
566
5c 6
c 56
![Page 6: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/6.jpg)
IncrementandDecrementOperators
• Try!– UsingWHILEloopandincrementoperatortoprintnumbers1through5.
int main(){int count=1; //1.initializationwhile(count<=5){ //2.loopcondition
cout <<count<<endl;count=count+1; //3.update
}return0;
}
![Page 7: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/7.jpg)
IncrementandDecrementOperators
• Try!– UsingWHILEloopandincrementoperatortoprintnumbers1through5.
int main(){int count=1; //1.initializationwhile(count<=5){ //2.loopcondition
cout <<count<<endl;count++; //3.update
}return0;
}
12345
Output:
![Page 8: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/8.jpg)
for loop
• InadditiontoWHILEloop,C++providestheFORrepetitionstatement,whichspecifiesthecounter-controlledrepetition detailsinasinglelineofcode.
for (initialization;loopcondition;update){
//dosomething…}
![Page 9: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/9.jpg)
for loop
• InadditiontoWHILEloop,C++providestheFORrepetitionstatement,whichspecifiesthecounter-controlledrepetition detailsinasinglelineofcode.int main(){
for(int counter=1; counter<=5; counter++){cout <<counter<<endl;
}return0;
}
initialization loopcondition update
![Page 10: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/10.jpg)
for loop
• InadditiontoWHILEloop,C++providestheFORrepetitionstatement,whichspecifiesthecounter-controlledrepetition detailsinasinglelineofcode.int main(){for(int counter;counter<=10;counter++){cout <<counter<<endl;
}return0;
}
![Page 11: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/11.jpg)
for loop
• ExamplesofusingFOR loop,
![Page 12: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/12.jpg)
for loop• Exercises
RedoLabAssignment#2
2. Writeaprogramthataskstheusertotype10integersanddisplaysthesmallestvalue.[10points]
3. Writeaprogramthataskstheusertotype10integersanddisplaythenumberofoccurrenceofthebiggestvalue.[15points]
![Page 13: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/13.jpg)
for loop
2. Writeaprogramthataskstheusertotype10integersanddisplaysthesmallestvalue.
![Page 14: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/14.jpg)
for loop
2. Writeaprogramthataskstheusertotype10integersanddisplaysthesmallestvalue.
int num,smallest;
for(int num_counter=0;num_counter<10;num_counter++){
//sameasWHILEloop//exceptforcounterincrement
}
![Page 15: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/15.jpg)
for loop
3. Writeaprogramthataskstheusertotype10integersanddisplaythenumberofoccurrenceofthebiggestvalue.
![Page 16: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/16.jpg)
for loop
3. Writeaprogramthataskstheusertotype10integersanddisplaythenumberofoccurrenceofthebiggestvalue.
int num;int biggest;int occur=0;
for(int num_counter=0;num_counter<10;num_counter++){
//sameasWHILEloop//exceptforcounterincrement
}
![Page 17: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/17.jpg)
for loop
Nestedforloop
for(int i=0; i<3; i++){
for(int j=0; j<4; j++){cout << “*”;
}cout << endl;
}
Output:
************
j=
0 1 2 3
i=
(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)
(2,0) (2,1) (2,2) (2,3)
0
1
2
(i,j)
Row
Column
Whatistheoutputifthefollowingconditionisused?
if(i<j){ cout<<“*”; }
![Page 18: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/18.jpg)
do/while statement• Thedo/while statementissimilartothewhilestatement.
• Thedo/while statementteststheloop-continuationconditionafter theloopbodyexecutes;therefore,theloopbodyalwaysexecutesatleastonce.
do{//dosomething…
}while (condition);
Action(s)
Condition
false
true
![Page 19: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/19.jpg)
do/while statement
• Example, Output:
![Page 20: Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment](https://reader033.fdocuments.us/reader033/viewer/2022051910/5fff4103baedfd5c05393c79/html5/thumbnails/20.jpg)
LabAssignment#3