Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
ControlStructure(2)
ISNE261102FacultyofEngineeringChiangMaiUniversity
AssignmentOperators
• C++providesseveralassignmentoperators forabbreviatingassignmentexpressions.Forexample,thestatement
c=c+3;canbeabbreviatedwiththeadditionassignmentoperator+=as
c+=3;
AssignmentOperators
IncrementandDecrementOperators
• C++alsoprovidesincrementanddecrementoperatorsforadding1toorsubtracting1fromthevalueofanumericvariable.
c=c+1è ++c orc++
Incre/Dec
Beforeexpression
Afterexpression
Beforeexpression
Afterexpression
IncrementandDecrementOperators
556
566
5c 6
c 56
IncrementandDecrementOperators
• Try!– UsingWHILEloopandincrementoperatortoprintnumbers1through5.
int main(){int count=1; //1.initializationwhile(count<=5){ //2.loopcondition
cout <<count<<endl;count=count+1; //3.update
}return0;
}
IncrementandDecrementOperators
• Try!– UsingWHILEloopandincrementoperatortoprintnumbers1through5.
int main(){int count=1; //1.initializationwhile(count<=5){ //2.loopcondition
cout <<count<<endl;count++; //3.update
}return0;
}
12345
Output:
for loop
• InadditiontoWHILEloop,C++providestheFORrepetitionstatement,whichspecifiesthecounter-controlledrepetition detailsinasinglelineofcode.
for (initialization;loopcondition;update){
//dosomething…}
for loop
• InadditiontoWHILEloop,C++providestheFORrepetitionstatement,whichspecifiesthecounter-controlledrepetition detailsinasinglelineofcode.int main(){
for(int counter=1; counter<=5; counter++){cout <<counter<<endl;
}return0;
}
initialization loopcondition update
for loop
• InadditiontoWHILEloop,C++providestheFORrepetitionstatement,whichspecifiesthecounter-controlledrepetition detailsinasinglelineofcode.int main(){for(int counter;counter<=10;counter++){cout <<counter<<endl;
}return0;
}
for loop
• ExamplesofusingFOR loop,
for loop• Exercises
RedoLabAssignment#2
2. Writeaprogramthataskstheusertotype10integersanddisplaysthesmallestvalue.[10points]
3. Writeaprogramthataskstheusertotype10integersanddisplaythenumberofoccurrenceofthebiggestvalue.[15points]
for loop
2. Writeaprogramthataskstheusertotype10integersanddisplaysthesmallestvalue.
for loop
2. Writeaprogramthataskstheusertotype10integersanddisplaysthesmallestvalue.
int num,smallest;
for(int num_counter=0;num_counter<10;num_counter++){
//sameasWHILEloop//exceptforcounterincrement
}
for loop
3. Writeaprogramthataskstheusertotype10integersanddisplaythenumberofoccurrenceofthebiggestvalue.
for loop
3. Writeaprogramthataskstheusertotype10integersanddisplaythenumberofoccurrenceofthebiggestvalue.
int num;int biggest;int occur=0;
for(int num_counter=0;num_counter<10;num_counter++){
//sameasWHILEloop//exceptforcounterincrement
}
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<<“*”; }
do/while statement• Thedo/while statementissimilartothewhilestatement.
• Thedo/while statementteststheloop-continuationconditionafter theloopbodyexecutes;therefore,theloopbodyalwaysexecutesatleastonce.
do{//dosomething…
}while (condition);
Action(s)
Condition
false
true
do/while statement
• Example, Output:
LabAssignment#3