Top Banner
1 Outline Introduction. if statement. if/else statement. Nested if/else statements. Examples.
15

Outline - International University of Sarajevo

Jan 18, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Outline - International University of Sarajevo

1

Outline

Introduction.

if statement.

if/else statement.

Nested if/else statements.

Examples.

Page 2: Outline - International University of Sarajevo

2

Control Structures

Sequential execution Statements executed one after the other in the order written

Transfer of control When the next statement executed is not the next one in

sequence

All programs written in terms of 3 control structures: Sequence structure

Built into C++. Programs executed sequentially by default.

Selection structures C++ has three types - if, if/else, and switch

Repetition structures C++ has three types - while, do/while, and for

Page 3: Outline - International University of Sarajevo

3

Control structure example

Sequence structureint main()

{

int i=2,j=10;

int z= (++j)*--i

cout<<z;

return 0;

}

Selection structureint main()

{

int grade=0;

cout<<“enter grade”;

cin>>grade;

if(grade>90)

{

cout<<“high grade”;

}

else

{

cout<<“low grade”;

}

}

Repetition structureint main()

{

int grade=0;

cout<<“enter grade”;

cin>>grade;

while (grade<50)

{

grade +=10;

}

cout<<grade;

}

Page 4: Outline - International University of Sarajevo

4

Combination of Control Structures

Two types of control structures combination:

Control structure stacking: use them one after the other.

Control structure nesting: use one control structure inside the body of another control structure.

Page 5: Outline - International University of Sarajevo

5

Stacking vs. Nesting

Stacking control structure int main()

{

int grade=0;

cout<<“enter grade”;

cin>>grade;

if(grade>90)

{

cout<<“high grade”;

}

else

{

cout<<“low grade”;

}

}

Nesting control structure int main()

{

int grade=0;

cout<<“enter grade”;

cin>>grade;

if (grade<90)

{

if (grade<60)

{

if(grade<50)

{

cout<<“fail”;

}

else

{

cout<<“pass”;

}

}

}

}

Page 6: Outline - International University of Sarajevo

6

The if Selection Structure I

Selection structure used to choose among alternative courses of action

Pseudocode example: If student’s grade is greater than or equal to 60

Display “Passed”

If the condition is true

print statement executed and program goes on to next statement

If the condition is false

print statement is ignored and the program goes onto the next statement

Indenting makes programs easier to read C++ ignores white-space characters

Page 7: Outline - International University of Sarajevo

7

The if Selection Structure II

Translation of pseudocode statement into C++:if ( grade >= 60 )

cout << "Passed";

Diamond symbol (decision symbol)

indicates decision is to be made

Contains an expression that can be true or false.

Test the condition, follow appropriate path

if structure is a single-entry/single-exit

structure

Page 8: Outline - International University of Sarajevo

8

The if Selection Structure III

Flowchart (or UML activity diagram) of pseudocode statement

true

false

grade >= 60 print “Passed”

A decision can be made on

any expression.

zero - false

nonzero - true

Example:

3 - 4 is true

Page 9: Outline - International University of Sarajevo

9

The if/else Selection Structure I

if

Only performs an action if the condition is true (single selection structure)

if/else

A different action is performed when condition is true and when condition is false (double selection structure).

Psuedocodeif student’s grade is greater than or equal to 60

Display “Passed”else

Display “Failed”

C++ codeif ( grade >= 60 )

cout << "Passed";else

cout << "Failed";

Page 10: Outline - International University of Sarajevo

10

The if/else Selection Structure II

Ternary conditional operator (?:) (the only C++ ternary operator) Takes three arguments (condition, value if true, value if false)

Our C++ code could be written as:cout << ( grade >= 60 ? “Passed” : “Failed” );//omitting ( ) is syntax error

OR

grade >= 60 ? cout << “Passed” : cout << “Failed”;

Remember that the precedence of ?: is low, so do not forget the parenthesis that is used to force its priority.

truefalse

display “Failed” display “Passed”

grade >= 60

Page 11: Outline - International University of Sarajevo

11

Nested if/else Structure I

Nested if/else structures Test for multiple cases by placing if/else selection structures

inside if/else selection structures.if student’s grade is greater than or equal to 90

Display “A”else

if student’s grade is greater than or equal to 80Display“B”

else if student’s grade is greater than or equal to 70

Display “C”else

if student’s grade is greater than or equal to 60 Display “D”

elseDisplay“F”

Once a condition is met, the rest of the statements are skipped

The above code can be rewritten using else if as in the next slide

Page 12: Outline - International University of Sarajevo

12

Nested if/else Structure II

if student’s grade is greater than or equal to 90Display “A”

else if student’s grade is greater than or equal to 80Display “B”

else if student’s grade is greater than or equal to 70 Display“C”

else if student’s grade is greater than or equal to 60 Display “D”

elseDisplay“F”

Pay attention to the indentation level and how it is differed from the previous slide.

Page 13: Outline - International University of Sarajevo

13

The if/else Body I

The body of if or else is the statement that will be implemented after if or else.

If no braces after if or else then the body will be only the first statement after them.

If there are braces, then the body will be the compound statement.

Page 14: Outline - International University of Sarajevo

14

The if/else Body II

Compound statement: Set of statements within a pair of braces Example:

if ( grade >= 60 )cout << "Passed.\n";

else {cout << "Failed.\n";cout << "You must take this course again.\n";

}

Without the braces,cout << "You must take this course again.\n";

would be automatically executed and the body of the else will be the first statement after it only.

Block of code Compound statements with declarations

Page 15: Outline - International University of Sarajevo

15

The if/else Body III

Every if/else or if is considered as one statement with their bodies. How??

Placing lines of codes between else and the body of its if is syntax error.

Leaving the parenthesis after the if empty (you have not put an expression for the condition) is syntax error.

if () // no condition syntax error

cout<<"b";

If( grade<50)

{ cout<<“F”;

cout<<“Fail”;

}

cout<<“you fail”; // syntax error

else

Cout<<“pass”;

if (grade > 60)cout<<"b";

cout<<"hello"; // will produce a syntax error else

cout<<"c";