• No results found

Lec3 - Conditions & Branching.pdf

N/A
N/A
Protected

Academic year: 2020

Share "Lec3 - Conditions & Branching.pdf"

Copied!
15
0
0

Loading.... (view fulltext now)

Full text

(1)

Structured Programming

Using C++

Lecture 3 : Flow of Control (I)

Dr. Amal Khalifa

Quiz

Dr. Amal Khalifa, 2012

2

(2)

Lecture Contents:

3

Boolean Expressions

 Building, Evaluating & Precedence Rules

Branching Mechanisms

 if-else

 switch

 Nesting if-else

Dr. Amal Khalifa, 2012

Relational Operators

Relational Operators

Operator

Description

> Greater than

< Less than

= Equal to

(3)

Boolean Expressions:

2-5

Evaluating Boolean Expressions:

(4)

Precedence of Operators (1 of 4)

2-7

(5)

Precedence of Operators (3 of 4)

Copyright © 2010 P earson Addison -Wesley. All rights reserved.

2-9

Precedence of Operators (4 of 4)

Copyright © 2010 P earson Addison -Wesley. All rights reserved.

(6)

Precedence Examples

2-11

Arithmetic before logical

 x + 1 > 2 || x + 1 < -3 means:

 (x + 1) > 2 || (x + 1) < -3

Short-circuit evaluation

 (x >= 0) && (y > 1)

 Be careful with increment operators!

 (x > 1) && (y++)

Integers as boolean values

 All non-zero values  true

 Zero value  false

Compare and Branch

 A program can instruct a computer to compare two

items and do something based on a match or mismatch which, in turn, redirect the sequence of programming instructions.

 There are two forms:

(7)

IF-THEN-ELSE

false true

Entry

Exit

condition

“true”

statement

“false”

statement

Branching Mechanisms

2-14

 Use { } for multiples statement

 Also called a "block" stmt  Even if just one statement  Enhances readability 

else clause is optional

 If, in the false branch (else), you want "nothing" to happen,

(8)

Example 1: Grades

 Write an algorithm to determine a student’s final

grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

Hint:

 the max grade is 100.

 The student is considered pass if the average >= 50%

Example 2: Payroll

 write a program that computes the salary of a sales

(9)

Common Pitfalls

2-17

 Operator "=" vs. operator "=="  One means "assignment" (=)  One means "equality" (==)

 VERY different in C++!  Example:

if (x = 12) Note operator used! Do_Something

else

Do_Something_Else

Nested if

18

if-else statements

contain smaller

statements

 Compound or simple

statements (we’ve seen)

 Can also contain any

(10)

Multiway if-else

2-19

Not new, just different indenting

Avoids "excessive" indenting

 Syntax:

(11)

Example 3: Grades revisited

 Write a program to determine a student’s final grade

according to the following rules :  A  90% or more

 B  80% - 90%

 C  70% - 80 %

 D  70% - 60 %  F  otherwise

 Notice that the final grade is calculated as the average

of the four marks, where the max grade is 100.

The switch Statement

(12)

The switch Statement in Action

2-23

Example 4:

Simple calculator

 write a program that accepts two numbers form the

user as well as a mathematical operation to perform. The user must choose between the basic four

operators: ( +, -, *, /).

 Additional activity:

(13)

switch Pitfalls/Tip

 Forgetting the break;

 No compiler error

 Execution simply "falls thru" other cases until break;

 Biggest use: MENUs

 Provides clearer "big-picture" view  Shows menu structure effectively  Each branch is one menu choice

2-25

Copyright © 2010 Pearson Addison-Wesley. All rights

Example 5:

Dr. Amal Khalifa, 2012 26

switch (response) {

case "1":

// Execute menu option 1 break;

case "2":

// Execute menu option 2 break;

case 3":

// Execute menu option 3 break;

default:

(14)

switch Pitfalls/Tip

2-27

 Execution "falls thru" until break  switch provides a "point of entry"  Example:

case "A": case "a":

cout << "Excellent: you got an "A"!\n"; break;

case "B": case "b":

cout << "Good: you got a "B"!\n"; break;

 Note multiple labels provide same "entry"

Conditional Operator

Also called "ternary operator"

 Allows embedded conditional in expression

 Essentially "shorthand if-else" operator

 Example:

if (n1 > n2) max = n1; else

(15)

Dr. Amal Khalifa, 2012 29

That’s all for today !!

References

Related documents

 We will increase public awareness about mood disorders and empower people to develop and manage mental wellness..  We will provide a welcoming atmosphere to those dealing

Like other parts of the world requirement engineering practitioners of the local software industry have also experienced social, cultural, political and cognitive

Product mix is shifting towards Google and new customer sales have declined dramatically Product mix is shifting towards Google and new customer sales have declined dramatically

Teleport questions with parents is it phrase is important to do they are better understand grammar quiz and relative clause and organize your team has a quizizz.. Nailed it to use

The student is responsible for completing the course’s assessment tasks: that a sufficient amount of course points is earned and a passing course grade is obtained.. A passing

excitement; — कड़ा करना to pull oneself together; to steel one's heart; to prepare oneself for a shock/hardship, etc; — काँपना to be terrified; to be struck

Hyphae localization in tissue surrounding the wound or inoculation sites indicates that Pch colonizes all cell types, such as vascular tissues, paratracheal parenchyma cells,

Financial Due Diligence Tax Due Diligence Legal Due Diligence Vendor Due Diligence Buyer Due Diligence Data Room Services. Our personable and pragmatic approach ensures