0% found this document useful (0 votes)
21 views43 pages

Lecture 9 PF

Programming fundamental

Uploaded by

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

Lecture 9 PF

Programming fundamental

Uploaded by

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

PROGRAMMING

FUNDAMENTALS LECTURE #

9
C O N T R O L STRUCTURES

• A computer can proceed:

• In sequence

• Selectively (branch): making a choice

• Repetitively (iteratively): looping

• Some statements are executed only if certain conditions


are met

• A condition is met if it evaluates to true

2
C O N T R O L STRUCTURES

3
C O N T R O L STRUCTURES

• You must understand the nature of conditional


statements and how to use them.
• Consider the following three statements:

4
O N E - WAY S E L E C T I O N

• The syntax of one-way


selection is:

• The statement is executed if the value of the expression is


true

• The statement is bypassed if the value is false; program


goes to the next statement

• if is a reserved word

5
O N E - WAY S E L E C T I O N

6
O N E - WAY S E L E C T I O N

7
O N E - WAY S E L E C T I O N

8
O N E - WAY S E L E C T I O N

9
T W O - WAY S E L E C T I O N

• Two-way selection takes the


form:

• If expression is statement1 is otherwis


true, executed; e,
statement2 is
• statement1
executed and statement2 are any C++
statements

• else is a reserved word

10
T W O - WAY S E L E C T I O N

11
T W O - WAY S E L E C T I O N

12
T W O - WAY S E L E C T I O N

13
C O M P O U N D ( B L O C K OF S TAT
EMENTS)

• Compound statement (block of


statements):

• A compound statement is a single


statement

14
C O M P O U N D ( B L O C K OF S TAT
EMENTS)

if (age > 18)


{
cout << "Eligible to vote." << endl; cout
<< "No longer a minor." << endl;
}
else
{
cout << "Not eligible to vote." << endl;
cout << "Still a minor." << endl;
}

15
D E C I S I O N M A K I N G : E Q U A L I T Y A N D REL AT I O N A L OPER
ATOR S

if structure
• Decision based on truth or false of
condition
• If condition met, body executed
• Else, body not executed

Equality and relational operators


• Equality operators
• Same level of precedence
• Relational operators
• Same level of precedence
• Associate left to right

16
D E C I S I O N M A K I N G : E Q U A L I T Y A N D REL AT I O N A L OPER
ATOR S

C++
Standard C++
relation Meaning
algebraic condition
al
equality
operato
operator
r
Relational operators

> > x>y x is greater than y

< < x<y x is less than y

≥ >= x >= y x is greater than or equal to


y

≤ <= x <= y x is less than or equal to y

Equality operators

= == x == y x is equal to y

 != x != y x is not equal to y

17
REL AT I O N A L OPER
ATOR S

• A condition is represented by a logical (Boolean)


expression that can be true or false

• Relational
operators:

• Allow
comparisons

• Require two
operands
(binary)

• Evaluate to true
or false
18
REL AT I O N A L OPER ATOR S A N D S AMPLE DATA
TYPES

19
C O M PA R I N G C H A RA C T E R S

• Expression with relational operators

• Depends on machine’s collating sequence

• ASCII character set

• Logical (Boolean) expressions

• Expressions such as 4 < 6 and 'R' > 'T’

• Returns an integer value of 1 if the logical expression


evaluates to true

• Returns an integer value of 0 otherwise

20
C O M PA R I S O N OF
C H A RA C T E R S

• For characters

• Respective ASCII values are


compared

• 'R' > 'T' is false (82 >


84)

• '+' < '*' is false (43 <


42)

• 'A' <= 'a' is true


(65<97)

21
REL AT I O N A L A N D E Q U A L I T Y OPER ATOR S
( CONT.)

• The relational operators have


very low precedence and associate left-to-
right

• The equality operators have


very-very low precedence and associate
left-to-right
17 < foo ==
• Some examples:
x 3.14
age != x+1 >=
21 4*y-z

22
PRECEDENCE

Operato Precedence
rs highest (applied
() first)
*/%
+-
< <= >
>=
== !=
lowest (applied
= last)

23
LO G I C A L OPER ATOR S A N D LO G I C A L
EXPRESSIONS

Operator Description
! NOT
&& AND
| OR

24
LO G I C A L OPER ATOR S A N D LO G I C A L
EXPRESSIONS

• A N D & OR operators work just like AND/OR-Gate as you


studied in
Physics in intermediate
• A N D = True iff all conditions are TRUE
• OR = False iff all results are FASLE

25
O R D E R OF P R E C E D E N C E

26
O R D E R OF P R E C E D E N C E

• Suppose you have the following


declarations
bool found = true;
int age = 20;
double hours =
45.30;
double overtime = 15.00;
int count = 20;
Expression
char ch = 'B'; Value / Expression
false
!found
Because found is true, !found is false
true
hours > 40.0 Because hours is 45.3 and 45.3 > 40.0 is true, the
expression
hours > 40.0 evaluates to true
false
!age Age is 20, which is non zero so age is true. Therefore !
age is
false
false
!found && (age !found is false; age >= 18 is 20 >= 18 is true.
27
>=18) Therefore !found && (age >=18) is false && true,
O R D E R OF P R E C E D E N C E

bool found = true;


int age = 20;
double hours =
45.30;
double overtime = 15.00;
int count = 20;
char ch = 'B';
Expression Value / Expression
true
hours + overTime is 45.30 + 15.00 = 60.30 and
hours + overTime <= 75.0
60.30 <=
75.0 is true, it follows that hours +
overtime <= 75 evaluates to
true
true
Now count is 20, Because 20 >= 0 is true, count
(count >= 0) && (count <= >=0 is
100) true. Also 20 <= 100 is true, count <=100
is true. Therefore (count >= 20) && (count
<= 100) is true & true, which evaluates to
true
true
Here ch is ‘B’. Because ‘A’ <= ‘B’ is true,‘A’ 28
(‘A’ <= ch && ch <= ‘Z’) <= ch evaluates to true. Also, because ‘B’ <=
LO G I C A L OPER ATOR S A N D LO G I C A L
EXPRESSIONS

29
LO G I C A L OPER ATOR S A N D LO G I C A L
EXPRESSIONS

30
C O N D I T I O N A L OPER ATOR
(? :)

• Conditional operator (?:) takes three arguments

• Ternary operator

• Syntax for using the conditional operator:

expression1 ? expression2 : expression3

• If expression1 is true, theresultof the conditional


expression is
expression2

• Otherwise, the result is expression3

31
C O N D I T I O N A L OPER ATOR
(? :)

Consider the following statement

if(x >= y)

large = x;

else

large = y;

You can use the conditional operator to simplify the


writing of
this if…else statement as follows:

large = (x >= y)? x: y ;

32
M U LT IPLE SE LE C TIONS: N E S T E D IF ( C O N T ’
D.)

33
M U LT IPLE SE LE C TIONS: N E S T E D IF ( C O N T ’
D.)

34
M U LT IPLE SE LE C TIONS: N E S T E D IF ( C O N T ’
D.)

35
IF- ELSE PAIRING

Assume that all the variables are properly declared and consider the
following
statements:
if (gender == 'M') //Line 1
if(age < //Line 2
21) policyRate = //Line 3
0.05;
els //Line 4
e policyRate = //Line 5
0.035;
else if (gender == //Line 6
'F’)
//Line 7
if(age < policyRate = //Line 8
21) 0.04;
els //Line 9
e policyRate = //Line
0.03; 10

In this code, the else in Line 4 is paired with the if in Line 2. Note that for the else in Line
4, the most recent incomplete if is the if in Line 2. The else in Line 6 is paired with the if in
Line 1. The else in Line 9 is paired with the if in Line 7. Once again the indentation
does not determine the pairing, but it communicates the pairing

36
C O M PA R I N G IF- ELSE S TAT
E ME NTS

37
SHORT- C I RC U I T EVALUAT I O N

• Short-circuit evaluation: evaluation of a logical


expression stops as soon as the value of the
expression is known

• Example:

Assume x = 21, y=5, z = 3, ch = ‘B’

(x >= 20) | ( y == 10)

//Line 1 (ch == 'A') && (z <

7)
38
SHORT- C I RC U I T ANALYSIS

int a=10;
int b = 5;

1.(a > 5 && b == && > 10 && (b = 15));


5 a
2.((a = 5) || b 5 a > 10 && (b = 15));
== &&
3.a = 10, b =
5;
4.(a > 5 && b = 5 || a > 10 && (b = 15));
=
5.(a > 5 && b = 5 && a > 10 || (b = 15));
=
6.(a = 5 || b = 5 && a > 10 && (b = 5));
=

39
A S S O C I AT IVITY OF REL AT I O N A L OPER
ATOR S

#include<iostream>
using namespace std;
int main()
{
int x;
cout << "Enter and integer =

"; cin >> x;


cout << endl;

if (0 <= x <= 10)


cout << x << " is within
0 and 10" << endl;
else
cout << x << " is not
within 0 and 10" << endl;
return 0;
}

40
A S S O C I AT IVITY OF REL AT I O N A L OPER
ATOR S
• x=
7
0 <= x <= = 0 <= 7 <=
10 10
= (0 <=7) <= Because relationship operators are
10 evaluated from left to right
= 1 <= 10 Because 0<=7 is true, 0<=7 evaluates to 1
= 1 (true)

• x=
030
<= x <= = 0 <= 30 <=
10 10
= (0 <=30) Because relationship operators are
<= 10 evaluated
from left to right
= 1 <= 10 Because 0<=30 is true, 0<=30 evaluates
to 1
= 1 (true)
Solution:
0 <= x && x
<= 10
41
E Q U A L I T Y ( ==) OPER ATOR VS A S S I G N M E N T ( =)
OPER ATOR

• C++ allows you to use any expression that can be


evaluated to either true or false as an expression in the
if statement:

if (x = 5)

cout << "The value is five." << endl;

• The appearance of = in place of == resembles a silent


killer

• It is not a syntax error

• It is a logical error

42
EN
D

You might also like