Professional Documents
Culture Documents
Control Structures:
Selection/ Branch/Conditional
1
Lecture 12 Objectives
12.1 Introduction
12.2 Logical Data and Operators
12.3 Single-Selection: if
12.4 Double-Selection: if...else
12.5 Summary
Lecture 12 12.1 Introduction
_____________________________________________________________________________________________________________________
I
Forouzan, B.A. and Gilberg, R.F (2001). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.
Lecture 12 12.1 Introduction
_____________________________________________________________________________________________________________________
I
Forouzan, B.A. and Gilberg, R.F (2001). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.
Lecture 12 12.1 Introduction
Re
c al
• In Lecture 6, control structures were l
discussed in the context of Pseudo-code
and Flowcharts.
Control
Structures
Multiple-
Single-
Double-Selection Selectio
Selection
n
if if...else switch
____________________________________________________________________________________________________________________________________
I
Radzi, N.H.M, Hashim, S.Z.M. and Samsuri, P. (2001). Pengaturcaraan C. Malaysia: McGraw-Hill.
2
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education
Lecture 12 Contents
12.1 Introduction
12.2 Logical Data and Operators
• Logical data in C++
• Logical operators
• Evaluating logical expressions
• Relational operators
12.3 Single-Selection: if
12.4 Double-Selection: if...else
12.5 Summary
Lecture 12 12.2 Logical Data and Operators
Introduction
_____________________________________________________________________________________________________________________
I
Forouzan, B.A. and Gilberg, R.F (2001). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.
Lecture 12 12.2 Logical Data and Operators
Logical data in C++
_____________________________________________________________________________________________________________________
I
Forouzan, B.A. and Gilberg, R.F (2001). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.
Lecture 12 12.2 Logical Data and Operators
The flag
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Lecture 12 12.2 Logical Data and Operators
Relational operators
• Used to compare numbers to determine relative order
• Relational expressions produce true or false as output
• Operators:
____________________________________________________________________________________________________________________________
I
Forouzan, B.A. and Gilberg, R.F (2001). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.
Lecture 12 12.2 Logical Data and Operators
Relational Operators Example
int main()
{ int num;
cout<<"Enter number: ";
cin>>num;
return 0;
}
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Lecture 12 12.2 Logical Data and Operators
_____________________________________________________________________________________________________________________
I
Forouzan, B.A. and Gilberg, R.F (2001). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.
Lecture 12 12.2 Logical Data and Operators
_____________________________________________________________________________________________________________________
I
Forouzan, B.A. and Gilberg, R.F (2001). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.
Lecture 12 12.2 Logical Data and Operators
false true
12.1 Introduction
12.2 Logical Data and Operators
12.3 Single-Selection: if
12.4 Double-Selection: if...else
12.5 Summary
Lecture 12 12.3 Single-Selection: if
The if statement
• Allows statements to be conditionally executed or skipped
over.
2. if (expression)
{
statement_1;
statement_2;
…
statement_n;
}
• To evaluate 1:
o If the expression is true, then statement is executed.
o If the expression is false, then statement is skipped.
Lecture 12 12.3 Single-Selection: if
#include <iostream>
Example: if using namespace std;
if (grade>=60)
cout<<“\nPassed”;
return 0;
}
_____________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Lecture 12 12.3 Single-Selection: if
Activity 12.2: Trace the following programs if the input is 10 and 50.
#include <iostream>
using namespace std;
if (mark>=30)
pass=true;
if (pass)
cout<<“\nYou passed the test”;
if(!pass)
cout<<“\nYou failed the test”;
return 0;
}
Lecture 12 12.3 Single-Selection: if
Solution 12.2:
Lecture 12 12.3 Single-Selection: if
Start
Read kWh
Total=kWh*20 Category A
Print Total
End
Lecture 12 Contents
12.1 Introduction
12.2 Logical Data and Operators
12.3 Single-Selection: if
12.4 Double-Selection: if...else
• Nested if
• Conditional expressions
12.5 Summary
Lecture 12 12.4 Double-selection: if...else
The if...else statement
• Provides two possible paths of execution.
• Flowchart:
Lecture 12 12.4 Double-selection: if...else
• General Format:
if (expression) if (expression)
{ statement1;
statement1; // or block else
} statement2;
else Note: If consists of only
{ one statement, then the
statement2; // or block curly braces may be
} omitted !
To evaluate:
•If the expression is true, then statement1 is executed
and statement2 is skipped.
•If the expression is false, then statement1 is skipped
and statement2 is executed.
Lecture 12 12.4 Double-selection: if...else
Example: Suppose the passing grade on an exam is 60.
• Flowchart:
____________________________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Lecture 12 12.4 Double-selection: if...else
if (condition)
statement;
Summary of if, if/else constructs:
if (condition)
{ statement_1;
The three forms of if statements. :
:
statement_n; }
•The condition is always in
parentheses. if (condition)
{ statement_1;
:
•All TRUE-parts and all FALSE-parts :
are a single statement or a block of statement_n; }
code / compound statements else
{ statement_1;
:
:
statement_n; }
Lecture 12 12.4 Double-selection: if...else
if (hours>40)
{
base=rate*40;
bonus=1.5*rate*(hours-40);
}
else
base=hours*rate;
total=base+bonus;
cout<<“\nWeekly Payroll”;
cout<<“\nBase salary = ”<<base;
cout<<“\nBonus salary = ”<<bonus;
cout<<“\nTotal salary = ”<<total;
return 0;
}
Lecture 12 Contents
12.1 Introduction
12.2 Logical Data and Operators
12.3 Single-Selection: if
12.4 Double-Selection: if...else
12.5 Summary
Lecture 12 12.5 Summary