1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

32
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    6

Transcript of 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

Page 1: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

1

Engineering Problem Solving With C++

An Object Based ApproachChapter 3

Control Structures

Page 2: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

2

Structured Programming

• Sequence

• Selection

• Repetition

yesno

yes

no

Page 3: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

3

Boolean Expressions

Page 4: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

4

Relational Operators

== equality != non equality < less than > greater than <= less than equal to >= greater than equal to

Page 5: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

5

Logical Operators

! not && and || or

Page 6: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

6

Operator Precedence

1. < <= > >= (relational operators)

2. == != (logical operators)

3. && (…)

4. || (…)

Page 7: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

7

(-6<0)&&(12>=10)

(3.0 >= 2.0) || (3.0 >= 4.0)

(3.0 >= 2.0) && (3.0 >= 4.0)

true && true results in true

Practice! - evaluate

true || false results in true

true && false results in false

Page 8: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

8

Selection Statements

Page 9: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

9

Selection Statements

• if

• if else

• switch

Page 10: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

10

The if statement

if(expression)statement; //single statement executed

//if expression is true

if(expression){ //statements inside {} are

//executed if expression is truestatement1;statement2;…statement n;

}

Page 11: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

11

The if statement - examples

if (x>0)k++;

if(x>0) {

x=sqrt(x);k++;

}

Page 12: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

12

The if - else statement

if(expression)statement;

elsestatement;

if(expression) {

statement block }else {

statement block }

Page 13: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

13

The nested if-else

if(x > y)if(y < z)

k++;else

m++;else

j++;

Page 14: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

14

Practice!

int x=9, y=7, z=2, k=0, m=0, j=0;if(x > y)

if(y >z && y>k)k++;

elsem++;

elsej++;

What are the values of j, k and m?

j is 0, k is 1, m is 0

Page 15: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

15

The switch statement

switch(expression){

case constant:statement(s);break;

case constant:statement(s);break;

/* default is optional*/default:

statement(s);}

Page 16: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

16

The switch statement

• Expression must be of type integer or character

• The keyword case must be followed by a constant

• break statement is required unless you want all subsequent statements to be executed.

Page 17: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

17

switch statement examplechar ch;int ecount=0, vowels=0, other=0;cin.get(ch);while(!cin.eof()){ switch(ch) { case ‘e’:

ecount++; case ‘a’: case ‘i’:case ‘o’: case ‘u’:

vowels++;break;

default:other++;

}//end switch cin.get(ch);}//end whilecout << ecount << “ e’s, ” << vowels << “ vowels, and ” << other << “ other characters. “ << endl;

Page 18: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

18

Practice!

Convert these nested if/else statements to a switch statement:if (rank==1 || rank==2) cout << "Lower division \n";else{ if (rank==3 || rank==4) cout << "Upper division \n"; else    { if (rank==5) cout << "Graduate student \n"; else cout << "Invalid rank \n"; }}

Page 19: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

19

Practice Solution!

switch(rank)

{ case 1: case 2: cout << "Lower division \n"; break; case 3: case 4: cout << "Upper division \n"; break; case 5: cout << "Graduate student \n"; break; default: cout << "Invalid rank \n"; }//end switch

Page 20: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

20

Repetition Statements

Page 21: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

21

The while statement

while (condition)

statement;

while (condition)

{

statement block

}

yes

no

Page 22: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

22

The do/while statement

do

statement;

while (condition)

do

{

statement block

} while (condition)yes

no

Page 23: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

23

Practice!

#include <iostream>using namespace std;int main(){

int n=4;while(n>0){

cout << n << endl;n--;

}cout << “value of n outside while is “ << n << endl;return 0;

}

Output?Program Trace

Page 24: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

24

The for statement

initalizetest

increment/decrement

true

statement(s)statement(s)

Page 25: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

25

The for statement

for(initialization; test; increment/decrement)

statement;

for(initialization; test; increment/decrement)

{

statement;

statement;

}

Page 26: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

26

The for statement - examples

//sum integers from 1 to 10int sum =0;for(int I=1;I<=10;I++)

sum = sum + I;

//sum odd integers from 1 to 10int sum =0;for(int I=1;I<=10;I+=2)

sum = sum + I;

Page 27: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

27

Practice!

Determine the number of times that each of the following for loops is executed.

for (k=3; k<=10; k++){ statements;}

for (k=3; k<=10; ++k){ statements;}

for (count=-2; count<=10; count++){ statements;}

Page 28: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

28

The break statement

• break;– terminates loop– execution continues with the first statement

following the loop

Page 29: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

29

The continue statement

• continue;– forces next iteration of the loop, skipping any

remaining statements in the loop

Page 30: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

30

Practice! What is the output?#include<iostream>using namespace std;int main(){ for(int i=0; i<10; i++) { if(i%2) { continue; }//end if cout << i << endl; }//end for return 0;}//end main

02468

Page 31: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

31

Practice! What is the output?#include<iostream>using namespace std;int main(){ for(int i=0; i<10; i++) { if(i%2) { break; }//end if cout << i << endl; }//end for return 0;}//end main

0

Page 32: 1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

32

Practice!

//This while loop calculates n!int nfact=1, n;cout << “enter positive integer “;cin >> n;while(n > 0){

nfact = nfact*n;n--;

}cout << n “! = “ << nfact;..

Write a for loop to replace the while loop