Upload
ginger
View
15
Download
0
Tags:
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
1
CS 141Computer
Programming 1
Teacher AssistantHadeel Al-Ateeq
Branching Statements
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.
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
C++ Control
Structure
Sequence structure
Selection Structure
If, if… else, switch
Repetition structure
While, do…while, for
Control structures in C++
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
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.
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
Grade >=60
Grade<60
Print “Passed”
Activity Diagram
If - Single Selection Statement
Decision Symbol indicates that a decision
to be made
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 {}
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
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
Grade >=60Grade<60 Print “Passed
”
Print “Failed”
Activity Diagram
if…else-Double-selection Statement
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
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 (?:)
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
One inside another, test for multiple cases Once condition met, other statements
skipped.
Nested if.. else Statements
Example
if (conditioin1)action1;
elseif (condition2)
action2;else
if(condition3)action3;…
else
actionN;
Nested if.. else Statements
if (condition1){
if (condition2)action1;
else{
if (condtion3)action2;
elseaction3;
}}else
action4;
Nested if.. else Statements
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
Nested if.. else Statements
Nested if.. else Statements
Nested if.. else StatementsAnother
Solution
Nested if.. else StatementsAnother Solutio
n
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
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
Example
Dangling –else Problem
bool flag1, flag2;
if (flag1)----else----
if( flag1|| flag2)----
else-----
Using Boolean variables
int x1,x2;
if(x1)…
else…
if(x1||x2)…
else….
Implicit Typecasting
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
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.
[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
switch (expression){
case value1:action1;break;
case value2:action2;break;
….case valuen:
actionN;break;
default:action;
}
Switch Multiple-selection Statement
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
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
Question #1
Question #1
Question #2
Write a C++ program that calculates the minimum of two input numbers.Note: check the validity of the
inputs
Question #2
Question #2
Question #3
Write a C++ program that calculates the maximum of two input numbers.Note: check the validity of the
inputs
Question #3
Question #3
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;
*****$$$$$
$$$$$
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
*****
#####$$$$$
Write a C++ program that compares any two integers using if
statements, relational operators and equality operators.
Question #5
Sample Output:
Question #5
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
Question #6
Question #6
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