0% found this document useful (0 votes)
10 views20 pages

PF Lecture 5

Uploaded by

zeeshanilaghari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

PF Lecture 5

Uploaded by

zeeshanilaghari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Programming Fundamentals

Lecture 5
Control Structures-Unit 2
Control Structures
Selection: if and if...else
 Although there are only two logical values, true and
false,
 they turn out to be extremely useful because they
permit programs to incorporate decision making that
alters the processing flow.
 The remainder of this Lecture discusses ways to
incorporate decisions
 There are two selections, or branch control structures:
 if statements and the
 switch structure.
 This section discusses how if and if. . .else
statements can be used to create one-way selection,
two-way selection, and multiple selections.
One-Way Selection
 A bank would like to send a
notice to a customer if her or
his checking account balance
falls below the required
minimum balance. That is, if
the account balance is below
the required minimum balance,
it should send a notice to the
customer; otherwise, it should
do nothing.
 In C++, one-way selections
are incorporated using the if
statement. The syntax of one-
way selection is:
One-Way Selection
 Note that the element of this
selecation
 It begins with the reserved

word if, followed by an


expression contained within
parentheses, followed by a
statement.
 Note that the parentheses
around the expression are
part of the syntax.
 The expression is
sometimes called a decision
maker because it decides
whether to execute the
statement that follows it.
One-Way Selection

 The expression is usually a


logical expression.
 If the value of the expression is
true, the statement executes.
 If the value is false, the
statement does not execute
and the computer goes on to
the next statement in the
program.
Exemple
Code Exemple
Absolute value of an integer
#include <iostream>
using namespace std; if (number < 0)
int main() number = -number;
{ cout << "Line 7: The
int number, temp; absolute value of "
cout << "Line 1: Enter an << temp << " is " << number
integer: "; << endl;
cin >> number; return 0;
cout << endl; }
temp = number;
Two-Way Selection(IF –ELSE)
 There are many programming
situations in which you must
choose between two
alternatives.

 For example, if a part-time


employee works overtime, the
paycheck is calculated using
the overtime payment formula;
otherwise, the paycheck is
calculated using the regular
formula.
 This is an example of two-way
selection.
Two-Way Selection(IF –ELSE)
 To choose between two
alternatives—that is, to
implement two-way
selections

 C++ provides the if. . .


else statement.
 Two-way selection uses
the following syntax
Two-Way Selection(IF –ELSE)
Programm exemple
#include <iomanip> wages = 40.0 * rate +
using namespace std; 1.5 * rate * (hours - 40.0);
int main() else
{ wages = hours * rate;
double wages, rate, hours; cout << endl;
cout << fixed << showpoint << cout << "Line 9: The wages are
setprecision(2 $" << wages
cout << "Line 2: Enter working << endl;
hours and rate: "; return 0;
cin >> hours >> rate; }
if (hours > 40.0)
Compound (Block of) Statements
 The if and if. . .else structures
control only one statement at a
time. Suppose, however, that
you want to execute more than
one statement if the
expression in an if or if. . .else
statement evaluates to true.
 To permit more complex
statements, C++
 provides a structure called a
compound statement or a
block of statements
Multiple Selections: Nested if
 In the previous sections, you learned how to implement
one-way and two-way selections in a program
 Some problems require the implementation of more than
two alternatives.
 For example, suppose that if the checking account
balance is more than $50,000, the interest rate is 7%;
if the balance is between $25,000 and $49,999.99,
the interest rate is 5%; if the balance is between
$1,000 and $24,999.99, the interest rate is 3%;
otherwise, the interest rate is 0%.
 This particular problem has four alternatives—that is,
multiple selection paths.
Multiple Selections: Nested if
Another Exemple
One More Exemple
Class Work: Write a programm for follwing situaions
(Indvidual work: this will be evaluated in class )
 Any question ?

You might also like