Chap 5 c++

27
Chapter 5 CONTROL STRUCTURES I (Selection) BTE2313 Computer Programming Lecturer: Samson Mekbib

Transcript of Chap 5 c++

Chapter 5

CONTROL STRUCTURES I (Selection)

BTE2313 Computer Programming

Lecturer: Samson Mekbib

LEARNING OUTCOME

Understanding basic problem solving techniques.

To be able to develop algorithms through the process of top – down, stepwise refinement.

To be able to use if, if/else and switch selection structures to choose among alternative actions.

ALGORITHM and PSEUDOCODE Algorithm: A procedure for solving a problem in terms of the actions to be executed and the order in which these actions are to be executed (recipe).

Pseudocode: an artificial and informal language that helps programmer develop algorithms that will latter be converted to structured C++ program.

A pseudocode is similar to everyday English: convenient and user friendly although not actual computer programming language.

A pseudocode consists of executable statements (those that are executed when the program has been converted from pseudocode to C++.

Examples of PSEUDOCODE 1. if (score is greater than or equal to 90)

grade is A

2. if (hours worked are less than or equal to 40)

wages = rate * hours

Otherwise

wages = (rate * 40) + 1.5 * (rate * (hours -40))

3. if (temperature is greater than 20 degrees and it is not raining)

go to play golf!

Equivalent C++ code:

1. if (score >= 90)

cout << “Grade: “ << ‘A’ << endl;

2. if (Hours <= 40)

wages = rate * Hours;

else if (Hours > 40)

wages = (rate*40) + 1.5 * (rate * (Hours -40));

3. if ((temp > 20) && (!(raining))

cout << “go out to play golf! “;

Flowchart

Flowchart: A graphical representation of an algorithm

Drawn using certain special – purpose symbols such as rectangles, diamonds, ovals and small circles. These symbols are connected by arrows called flow-lines

Basic Shapes used in Flowchart

Example of flowchart: The Basic If Statement

Syntax

if (Expression)

Action

If the Expression is true then execute Action

Action is either a single statement or a group of statements within braces

Expression

Action

true false

Control Structures A computer can process a program in one of the following ways:

a) simple sequence – starts at the beginning and follows and executes the statements in order. The simple input/output examples we saw so far are simple sequence operation

*** However, C++ offers the use of the following control structures for selection (decision making) and repetition:

b) selection – the program executes particular statements depending on some conditions

c) repetition – the program repeats particular statements a certain number of times based on some conditions (discussed in detail in chapter 6)

Flow of execution: sequence, selection & repetition

Conditional Constructs Provide

Ability to control whether a statement list is executed

Two constructs

If statement

if

if-else

if-else-if

Switch statement

Reading assignment : You will be required to present to the class the concept of the switch control structure and demonstrate your understanding using a C++ program example. Presentation will be done in a group of 4-5 students. Presentation day will be the first laboratory class after your semester break.

Example of if statement

What will be the output when the following

statements run?

int m = 5;

int n = 10;

if (m < n)

++m;

++n;

cout << " m = " << m << " n = " n << endl;

Example: Absolute value of a number

if (Value < 0) {

Value = -Value;

}

Value < 0

Value = -Value

true f alse

Is our number negative?

If Value is not less

than zero then our

number is fine as is

If Value is less than

zero then we need to

update its value to

that of its additive

inverse

Our number is

now definitely

nonnegative

Sorting Two Numbers

cout << "Enter two integers: ";

int Value1;

int Value2;

cin >> Value1 >> Value2;

if (Value1 > Value2) {

int RememberValue1 = Value1;

Value1 = Value2;

Value2 = RememberValue1;

}

cout << "The input in sorted order: "

<< Value1 << " " << Value2 << endl;

Semantics

value2 < value1

int rememberValue1 = value1

value1 = value2

value2 = rememberValue1

true f alse

Are the numbers

out of order

Rearrange value1

and value2 to

put their values

in the proper

order

The numbers were

initially in order

The numbers were

rearranged into the

proper order

The numbers are in

order

The If-Else Statement Syntax

if (Expression)

Action1

else

Action2

If Expression is true then execute Action1 otherwise execute Action2

if (v == 0) {

cout << "v is 0";

}

else {

cout << "v is not 0";

}

Expression

Action1 Action2

true false

Example: Finding the Max

cout << "Enter two integers: ";

int Value1;

int Value2;

cin >> Value1 >> Value2;

int Max;

if (Value1 < Value2) {

Max = Value2;

}

else {

Max = Value1;

}

cout << "Maximum of inputs is: " << Max << endl;

Finding the Max

Value1 < Value2

Max = Value2 Max = Value1

true f alse

Is Value2 larger than Value1

Yes, it is . So Value2 is

larger than Value1. In

this case, Max is set

to Value2

No, its not. So Value1

is at least as large as

Value2. In this case,

Max is set to Value1

Either case, Max is set

correctly

if-else-if Statement

if ( nbr < 0 )

{

cout << nbr << " is negative" << endl;

}

else if ( nbr > 0 )

{

cout << nbr << " is positive" << endl;

}

else

{

cout << nbr << " is zero" << endl;

}

Nested if-else-if Example:

Switch structure

Switch structure

Switch structure

Example of a Switch structure

Practice by converting the above switch … case statements to if else statements.

Exercise Write a C++ program, which will input the marks for quiz, test and final

examination. Assume the full mark for each one is 100. Calculate the

final mark that the student receive using the following formula:

Final mark = 0.6 * final examination + 0.2* (test + quiz).

Calculate the grade that the student obtains based on the following table.

Display the mark, grade and the appropriate message to the student.

Mark Grade Message

0-49

50-64

65-74

75-100

F

C

B

A

You fail the course. Try again next semester.

You pass the course, but more exercises are needed.

You pass the course, Good luck in the next level.

Congratulations, you are excellent in this course.

Exercise A sample output of your program should look like this