CMPT 128: Introduction to Computing Science for Engineering Students

12
© Janice Regan, CMPT 128, Jan 2007 1 CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements

description

CMPT 128: Introduction to Computing Science for Engineering Students. Control Structures Nested ifs, switch statements. Multiple Selections (Nested if). if (condition) { // Series of actions to be taken when the condition is true action 1A; action nA; } else { - PowerPoint PPT Presentation

Transcript of CMPT 128: Introduction to Computing Science for Engineering Students

Page 1: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 1

CMPT 128: Introduction to Computing Science for Engineering Students

Control Structures

Nested ifs, switch statements

Page 2: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 2

Multiple Selections (Nested if)

if (condition){ // Series of actions to be taken when the condition is true action 1A; action nA;}else{ if (condition 2) { // Series of actions to be taken when condition is false and condition2 true

action 1B;action nB;

} else { // Series of actions to be taken when condition and condition 2 are false

action 1C;action nC;

} }

Page 3: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 3

Flowchart for multiple selection

condition2

Statement 1A;

Statement nA;T

F

Statement 1C;

Statement nC;

condition

F

Statement 1B;

Statement nB;

T

Page 4: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 4

Multiple Selections (elseif) if (condition)

{ // Series of actions to be taken when condition is true action 1A; action nA;} else if (condition 2){

// Series of actions to be taken when condition is false and condition 2 is true action 1B; action nB;}

else { // Series of actions to be taken when the condition and condition2 are false

action 1C; action nC;

}

Page 5: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 5

Multiple Selections (Nested if)

if (condition){ if (condition 2) { // Series of actions to be taken when condition is true and condition2 true

action 1A;action nA;

} else { // Series of actions to be taken when condition is true and condition 2 is false

action 1B;action nB;

} } else { // Series of actions to be taken when condition is false, condition2 either

action 1C; action nC;

}

Page 6: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 6

Flowchart for multiple selection

condition2

Statement 1;

Statement n;

T

F

Statement 1C;

Statement nC;

condition

F

Statement 1A;

Statement nA;T

F

Page 7: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 7

Flowchart for multiple selection

condition

Statement 1A;

Statement nA;T

F

Statement 1C;

Statement nC;

condition&&condition2

F

Statement 1B;

Statement nB;

T

Page 8: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 8

Multiple Selections (elseif) if (condition && condition2)

{ // Series of actions to be taken when condition is true and condition2 true action 1A; action nA;} else if (condition){ // Series of actions to be taken when condition is true and condition2 is false action 1; action n;}

else { // Series of actions to be taken when the condition and condition2 are false

action 1; action n;

}

Page 9: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 9

The switch statement An alternative method to if statements with

multiple else clauses The controlling expression (selector) must

have an integral type Characters also work because they can be

converted to integers Case labels are particular values of the

controlling expression Break statements exit from the switch structure

Page 10: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 10

switch Structuresswitch(controling expression){case value1:

statements1;break;

case value2: statements2;break;

...case valuen:

statementsn; break;

default: statements;

}

Page 11: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 11

switch Statement

expression = value1

expression = value2

expression = valuen

Page 12: CMPT 128:  Introduction to Computing Science for Engineering Students

© Janice Regan, CMPT 128, Jan 2007 12

Sample case statement /* This is in a loop that reads */ /* nextInt each time */ switch (nextInt) { case 0: count0++; break; case 1: count1++; break; case 2: count2++; break;

case 4: count4++; break; case 5: count5++; break; case 6: count6++;

break; case 7: count7++; break; default:

countOther++;}