Control structures Algorithm Development Conditional Expressions Selection Statements 1.

29
Control structures • Algorithm Development • Conditional Expressions • Selection Statements 1

Transcript of Control structures Algorithm Development Conditional Expressions Selection Statements 1.

Page 1: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

Control structures

• Algorithm Development

• Conditional Expressions

• Selection Statements

1

Page 2: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

Algorithm

• Algorithm: a sequence of steps/actions needed to solve a problem. It is a set of sequential steps/actions that a C++ program should perform in order to solve the problem. These steps will eventually correspond to your C++ statements.

– Top-down design of your Algorithm:– Start with a “big picture”, i.e., broad description of a

problem solution in sequential step (i.e. order is important)

– Then, refine the sequential steps until they are simple enough to translate to C++ language statements.

• The solution steps of your Top-Down design define your algorithm. 2

Page 3: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

Algorithm• Algorithm corresponds to Step 4 (Solution development)

of the 5-Step Problem solving methodology

• How to describe your TOP-Down design (or algorithm)?

• Two techniques:– Pseudo Code: describes algorithm steps using English-like

statements.

– Flowchart: describes algorithm steps using a diagram/graphical notation

Page 4: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

Top-Down Design – Example

Problem: Increase salaries lower then 500 by 30 Omani Rials and display all salaries!

4

True

Falsesalary<50

0

Start main

read salary

salary=salary+30

Stop main

print salary

Program start/end

symbol

Input/output symbol

Comparison symbol

Computation symbol

Program start/end symbol

Input/output symbol

Or NO

Or Yes

Pseudo Code Flow Chart

Broad Pseudo Code:Read salary value

Increase salary values <500 by 30

Print salary

Step 2 Needs Refinement:

Refined Pseudo Code:Read salary value

If ( salary <500)

salary=salary+30

Print salary

Page 5: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

Structured Programming– Sequence:

• Statements executed in order sequentially, one after another.• This is the default structure in C++ programs

– Selection: if, if-else, if-else if, switch

• One set of statements is executed if a given condition is true, a different set of statements is executed if the condition is false.

– Repetition: while, do/while, for

• A set of statements is executed repeatedly as long as a given condition is true.

5

Page 6: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

6

Structured Programming

• Sequence

• Selection

• Repetition

?truefalse

?true

false

? => conditional expression Or Comparison

Page 7: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

Conditional Expressions

• A conditional expression is a Boolean expression that evaluates to true or false.– Example: if (salary <500) salary=salary+30;

• Relational operators and logical operators are used to build conditional expressions.

• Selection structures and repetition structures use conditional expressions.

7

Page 8: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

8

Relational Operators

== equal to

!= not equal to

< less than

> greater than

<= less than or equal

>= greater than or equal

Page 9: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

9

Relational Expression Value

1 == 6 false

5 != 9 true

5<9 true

5>10 false

2<=0 false

2>=0 true

Practice – evaluate

Page 10: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

10

Logical Operators

! not && and || or - can link relational expressions - create complex conditional expressions

ExampleAlgebra: 90≤ mark ≤ 100C++ Statement: mark >= 90 && mark <=100

Page 11: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

Logical Operators

A B A&&B A||B !A !B

0 0 0 0 1 1

0 1 0 1 1 0

1 0 0 1 0 1

1 1 1 1 0 0

11

Truth table for conditional expressions

0 = false1= true

Page 12: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

12

Operator Precedence

1. ( )

2. !

3. < <= > >=

4. == !=

5. &&

6. ||

Page 13: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

13

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

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

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

! (3.0 >= 4.0)

true && true results in true

Evaluate Expressions

true || false results in true

true && false results in false

! false results in true

Page 14: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

14

Selection Statements

C++ selection statements:– if statements

•if statement•if-else statement•if-else-if statement

– switch statements– ? the conditional operator

Page 15: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

15

The if statement one-way branch of action

if(expression)statement; /*single statement executed

if expression is true */

// statement block is executed if expression is true. if(expression){

statement1;statement2;

…statement n;

}

Page 16: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

16

The if statement - examples

if (salary<500)salary=salary+30;

if(x>0) {

x=sqrt(x);++k;

}

Page 17: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

17

Nested if statement

• If statement (s) within an if statement block if(x>0) {

if (y<100)x=sqrt(y);

x+=4; }

Page 18: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

18

The if - else statement2-way branch of action

if(expression)statement;

else statement;

if(expression) {

statement block }else {

statement block }

Page 19: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

The if-else Statement - Example

//determine grade result

#include <iostream>using namespace std;

int main(){ double grade; cout<<“Enter grade: “; cin>>grade; if ( grade >= 60 )

cout << "Passed.\n"; else {

cout << "Failed.\n"; cout << "take this course again.\n"; }

return 0;}

Page 20: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

20

The nested if-else

int x=9, y=7, z=2, k=0, m=0, j=0;

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

k++;else

m++;else

j++;

Page 21: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

21

The if – else if statementmulti-way branch of action

Form of nested if-else

if(expression1)

{statement;}

else if(expression2)

{statement;}

else if(expression3)

{statement;}

……

else{statement;}

if(expression1){ statement;}else{ if(expression2) { statement;} else { if(expression3)

{ statement;} else

{ …….. } }}

Each branch may contain a single statement or a block of statements encloses within braces:{ statement; statement; statement;}

Page 22: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

22

The if – else if statement (Example1)

If(day==1)cout<<“Saturday\n”;

else if(day==2)cout<<“Sunday\n”;

else if(day==3)cout<<“Monday\n”;

else if(day==4)cout<<“Tuesday\n”;

else if(day==5)cout<<“Wednesday\n”;

else if(day==6)cout<<“Thursday\n”;

else if(day==7)cout<<“Friday\n”;

elsecout<<“Sorry! Invalid day number\n.”;

Page 23: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

23

The if – else if statement (Example2)

If(grade>=90)cout<<“A”;

else if(grade>=80)cout<<“B”;

else if(grade>=70)cout<<“C”;

else if(grade>=60)cout<<“D”;

elsecout<<“F”;

Page 24: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

24

The if – else if statement (Example3)

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 25: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

25

The switch statement

switch(expression){

case constant:statement(s);break;

case constant:statement(s);break;

/* default is optional*/default:

statement(s);}

Page 26: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

26

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 27: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

27

switch statement example1

switch(grade) //grade of type char{

case ‘A’: cout<<“Excellent Work!”<<endl;break;

case ‘B’: cout<<“Good Work!”<<endl;break;

case ‘C’: cout<<“Average Work!”<<endl;break;

case ‘D’: case ‘F’:

cout<<“Poor Work!”<<endl;break;

}//end switch

Page 28: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

28

switch statement example2

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";

}

Page 29: Control structures Algorithm Development Conditional Expressions Selection Statements 1.

29

The Conditional Operator• The conditional operator replaces a simple if/else statement

• Syntax: expression ? statement1 : statement2;• expression is a conditional expression.

• if expression is true statement1 is executed

• if expression is false statement 2 is executed

• Example: a>b? ++count : c=a+b;

• Equivalent if-else:

if(a>b)

++count;

else

c=a+b;