50
CS 141 Computer Programming 1 Teacher Assistant Hadeel Al-Ateeq 1 Branching Statements

CS 141 Computer Programming 1

  • Upload
    ginger

  • View
    15

  • Download
    0

Embed Size (px)

DESCRIPTION

Branching Statements. CS 141 Computer Programming 1. Teacher Assistant Hadeel Al- Ateeq. Outline. Control Structure. Control Structures in C++. Sequence Structure. Selection Statements. If- single selection statement. If else – double selection statement. Conditional Operators. - PowerPoint PPT Presentation

Citation preview

Page 1: CS 141 Computer Programming 1

1

CS 141Computer

Programming 1

Teacher AssistantHadeel Al-Ateeq

Branching Statements

Page 2: CS 141 Computer Programming 1

2

Outline Control Structure. Control Structures in C++. Sequence Structure. Selection Statements. If- single selection statement. If else – double selection statement. Conditional Operators. Nested if-else statements. Dangling else Problem. Using Boolean Variables. Implicit Typecasting. Switch – multiple selection statement. Questions.

Page 3: CS 141 Computer Programming 1

Control Structure (Logic Structure)

Sequence structure: Process one instruction after another in the order they

are written. Selection structures:

Decision structure Make choices by using relational or logical

operators. Case structure

Enable to pick up one of a set of tasks. Repetition structure:

Enable to repeat tasks.

Control Structures

Page 4: CS 141 Computer Programming 1

C++ Control

Structure

Sequence structure

Selection Structure

If, if… else, switch

Repetition structure

While, do…while, for

Control structures in C++

Page 5: CS 141 Computer Programming 1

Models the workflow (activity) of a portion of a software system

Add grade to

total

Add 1 to counter

Corresponding C++ statement:total=total + grade;

Corresponding C++ statement:counter=counter + 1;

Sequence StructureActivity Diagram

Action states contains action

expressions

Transitions order in which actions occur

Initial State Beginning of the

workflow

Final State End

of the workflow

Notes Describe the purpose of

the symbols

Dotted line connects each

note with element it describes

Page 6: CS 141 Computer Programming 1

Selection Statements

Types

Single selection statement

Double-selection statement

Multiple-selection

statementSelects or ignores a single

group of actions.Selects among many

group of actions.

Selects between two groups of actions.

Page 7: CS 141 Computer Programming 1

Use the keyword if. Begin with if followed by condition; then action

or group of actions (compound statements or blocks) are listed.

Enclose compound statements between braces {} and indent these statements.

If condition is true , action is performed. Otherwise, actions is ignored.

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

Example: Pseudocode:If student grade is

greater than or equal to 60

Print “Passed”

If - Single Selection Statement

Page 8: CS 141 Computer Programming 1

Grade >=60

Grade<60

Print “Passed”

Activity Diagram

If - Single Selection Statement

Decision Symbol indicates that a decision

to be made

Page 9: CS 141 Computer Programming 1

if (condition)action;

if (condition){

action1;action2;…..….….actionN;

}

Condition can be relational or equality operators or any other

expressionsZero False

nonzero True

If - Single Selection Statement

Single Action

Multiple Actions surrounded by

braces {}

Page 10: CS 141 Computer Programming 1

Use the keyword if and else. Begin with if followed by condition; then

action or group of actions are listed. End with else then action or group of

actions are listed. If condition is true, action that followed by if

is performed. Otherwise, action that followed by else is

performed.

if…else-Double-selection Statement

Page 11: CS 141 Computer Programming 1

if (condition)action1;

elseaction2;

if (condition){

action1;…..actionN;

}else{

action1;….actionN;

}

Single Action

Multiple Actions surrounded by

braces {}

Condition can be relational or equality operators or any other

expressionsZero False

nonzero True

if…else-Double-selection Statement

Page 12: CS 141 Computer Programming 1

Grade >=60Grade<60 Print “Passed

Print “Failed”

Activity Diagram

if…else-Double-selection Statement

Page 13: CS 141 Computer Programming 1

If (grade >=60) cout<<“Passed”; else

cout<<“Failed”;

Pseudocode:if student's grade is

greater than or equal to 60

Print “passed”Else

Print ”failed”

Example

if…else-Double-selection Statement

Page 14: CS 141 Computer Programming 1

Provide similar result of if…else double selection statement.

Ternary operator requires three operands: Condition. Value when condition is true. Value when condition is false.

Syntax: (Condition? Condition’s true value:

condition’s false value)

Conditional Operator (?:)

Page 15: CS 141 Computer Programming 1

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

Conditional Operator (?:)Example

cout<<(grade >=60 ? “passed”: “failed”);

Can be written as:

int i=1, j=2, max;max=(i>j ?i:j);

Example Precedence of the conditional operator is low

Page 16: CS 141 Computer Programming 1

One inside another, test for multiple cases Once condition met, other statements

skipped.

Nested if.. else Statements

Example

Page 17: CS 141 Computer Programming 1

if (conditioin1)action1;

elseif (condition2)

action2;else

if(condition3)action3;…

else

actionN;

Nested if.. else Statements

Page 18: CS 141 Computer Programming 1

if (condition1){

if (condition2)action1;

else{

if (condtion3)action2;

elseaction3;

}}else

action4;

Nested if.. else Statements

Page 19: CS 141 Computer Programming 1

Write a C++ program that prints A for exam grades greater than or equal to 90, B for grades in the

range 80 to 89, C for grades in the range 70 to 79, D for grades in the range 60 to 69 and F for all other

grades.

Nested if.. else Statements

Question

Page 20: CS 141 Computer Programming 1

Nested if.. else Statements

Page 21: CS 141 Computer Programming 1

Nested if.. else Statements

Page 22: CS 141 Computer Programming 1

Nested if.. else StatementsAnother

Solution

Page 23: CS 141 Computer Programming 1

Nested if.. else StatementsAnother Solutio

n

Page 24: CS 141 Computer Programming 1

x=10; y=2;if (x>5)

if(y>5)cout<<“x and y are >5

“<<endl;else

cout<<“x is <=5”<<endl;

Each else associated with immediately preceding if

There is an exception when placing braces {}

Logical error !!

Dangling –else Problem

Example

Page 25: CS 141 Computer Programming 1

x=10; y=2;

if(x>5){

if(y>5)cout<<“x and y are >5”<endl;

}

elsecout<<“x is <=5”;

Dangling –else ProblemCorrectnes

s

Block

Page 26: CS 141 Computer Programming 1

Example

Dangling –else Problem

Page 27: CS 141 Computer Programming 1

bool flag1, flag2;

if (flag1)----else----

if( flag1|| flag2)----

else-----

Using Boolean variables

Page 28: CS 141 Computer Programming 1

int x1,x2;

if(x1)…

else…

if(x1||x2)…

else….

Implicit Typecasting

Page 29: CS 141 Computer Programming 1

Confusing the equality operator == with the assignment operator = results in logic errors.

int x=0;if (x=2)

cout<<"x is equal to 2";else

cout<<"x is not equal to 2";

This message will always be printed

!!

Note

Example

int X=0;X==1;

X’s value will remain

0

Page 30: CS 141 Computer Programming 1

Perform actions based on possible values of variable or expression.

Use keywords switch, case, default and break. Begin with switch followed by controlling expression

enclosed between parentheses (). Value of the controlling expression is compared with

each case label, then if a match occurs the program executes action for that case.

No matching, the program executes the optional default statement.

Switch - Multiple-selection Statement

Break Keyword

Causes immediate exit from switch statement. Jumps out of the switch statement, and do not execute

any more code.

Page 31: CS 141 Computer Programming 1

[True]

case a action(s)

Activity Diagram

Switch - Multiple-selection Statement

break

case b action(s)

break

case z action(s)

break

default action(s)

[True]

[True]

[False]

[False]

[False]

[False]

case a

case b

case z

Page 32: CS 141 Computer Programming 1

switch (expression){

case value1:action1;break;

case value2:action2;break;

….case valuen:

actionN;break;

default:action;

}

Switch Multiple-selection Statement

Page 33: CS 141 Computer Programming 1

switch (number){

case 0: cout<<“too small, sorry!”;break;case 5: cout<<“good job!”<<endl; //fall through

case 4: cout<<“nice pick!”<<endl; //fall through

case 3: cout<<“excellent !”<<endl; //fall through

case 2: cout<<“masterfull!”<<endl; //fall through

case 1: cout<<“incredible!”<<endl; //fall through

break;}

Switch Multiple-selection StatementExample

Page 34: CS 141 Computer Programming 1

Question #1

Write a C++ program that outputs the fine for driving too fast. It takes BOTH the

speed limit and current speed, compares between them and prints the

appropriate fine as follows:

You are lucky!, if the result of comparison is less than 10.

Fine payable: 40 Dollars, if the result of comparison is in the range of 10 and 20.

Fine payable: 80 Dollars, if the result of comparison is in the range of 20 and 30.

Hand over your driver’s license!, otherwise

Page 35: CS 141 Computer Programming 1

Question #1

Page 36: CS 141 Computer Programming 1

Question #1

Page 37: CS 141 Computer Programming 1

Question #2

Write a C++ program that calculates the minimum of two input numbers.Note: check the validity of the

inputs

Page 38: CS 141 Computer Programming 1

Question #2

Page 39: CS 141 Computer Programming 1

Question #2

Page 40: CS 141 Computer Programming 1

Question #3

Write a C++ program that calculates the maximum of two input numbers.Note: check the validity of the

inputs

Page 41: CS 141 Computer Programming 1

Question #3

Page 42: CS 141 Computer Programming 1

Question #3

Page 43: CS 141 Computer Programming 1

State the output for each of the following:

When x is 9 and y is 11. When x is 11 and y is 9.

Question #4

Output

if(x<10)if(y>10)cout<<“*****”<<endl;elsecout<“#####”<<endl;cout<<“$$$$$”<<endl;

*****$$$$$

$$$$$

Page 44: CS 141 Computer Programming 1

if(x<10){if(y>10)cout<<“*****”<<endl;}else{cout<<“#####”<<endl;cout<<“$$$$$”<<endl;}

Question #4

State the output for each of the following:

When x is 9 and y is 11. When x is 11 and y is 9.

Output

*****

#####$$$$$

Page 45: CS 141 Computer Programming 1

Write a C++ program that compares any two integers using if

statements, relational operators and equality operators.

Question #5

Sample Output:

Page 46: CS 141 Computer Programming 1

Question #5

Page 47: CS 141 Computer Programming 1

Question #6

Write a C++ program that prints out the entered character choice.

It accepts only three characters a, b and c (upper or lower cases).

Otherwise it prints“Your choice is not defined”

Note: Use Switch Statement

Page 48: CS 141 Computer Programming 1

Question #6

Page 49: CS 141 Computer Programming 1

Question #6

Page 50: CS 141 Computer Programming 1

Question #7

Trace the following code segment and show the value of value

variable.

Value

Cannot be determined

because the value of input variable

is unknown