20
Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University

Control Structure (2)santi/cpe102_files/... · Control Structure (2) ISNE 261102 Faculty of Engineering Chiang Mai University. Assignment Operators • C++ provides several assignment

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

LabAssignment#3