Escolar Documentos
Profissional Documentos
Cultura Documentos
CHAPTER 04
IF CONTROL STRUCTURE
1
Prepared by: Engr. Ayesha Zaveri
WHAT IS CONTROL STRUCTURE?
Normally, statements in a program are executed one after the other in
the order in which they are written.
Various C++ statements will enable you to specify the next statement
to be executed which might be other than the next one in order. This is
called transfer of control.
1. Single-selection statement
Select or ignore a single group of actions
2. Double-selection statement
Select between two groups of actions
3. Multiple-selection statement
Select among many group of actions
DECISION MAKING
Decision making structures require that the programmer specify one or
more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined
to be true, and optionally, other statements to be executed if the
condition is determined to be false.
DECISION MAKING
Following is the general from of a typical decision making structure
found in most of the programming languages:
DECISION MAKING
C++ provides following types of decision making statements. Click
the following links to check their detail.
THE ? : OPERATOR:
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the
colon.
Example
P Q P and Q
P Q P or Q
P not P
False True
True False
BOOLEAN ALGEBRA
Can create complex logical expressions by combining simple logical expressions
Example
not (P and Q)
Boolean operators
The and operator is &&
The or operator is ||
The not operator is !
A BOOLEAN TYPE
Example logical expressions
bool P = true;
bool Q = false;
bool R = true;
bool S = (P && Q);
bool T = ((!Q) || R);
bool U = !(R && (!Q));
RELATIONAL OPERATORS
Equality operators
==
!=
Examples
int i = 32;
int k = 45;
bool q = (i == k);
bool r = (i != k);
RELATIONAL OPERATORS
Ordering operators
<
>
>=
<=
Examples
int i = 5;
int k = 12;
bool p = (i < 10);
bool q = (k > i);
bool r = (i >= k);
bool s = (k <= 12);
CONDITIONAL CONSTRUCTS
Provide
Ability to control whether a statement list is executed
Two constructs
If statement
if
if-else
if-else-if
Switch statement
THE BASIC If STATEMENT
Syntax
if (Expression)
Action Expression
Action
Action is either a single
statement or a group of
statements within braces
SEMANTICS Are the numbers
out of order
Rearrange value1
and value2 to value2 < value1
put their values
in the proper
order true fa lse
if (Expression)
Action1
else
Action2 Expression
If Expression is true then execute
Action1 otherwise execute Action2 true false
if (v == 0) {
Console.WriteLine(v is 0); Action1 Action2
}
else {
Console.WriteLine( v is not 0);
}
FINDING THE MAX
Is Value2 larger than Value1
Yes, it is . So Value2 is
larger than Value1. In
this case, Max is set No, its not. So Value1
to Value2 is at least as large as
Value2. In this case,
Value1 < Value 2 Max is set to Value1
true false
Switch statement
An advanced construct
ACTIVITY 1
With the help of 3. i + 2 * j > k
following information Answer = 1
char key = m; 4. key 1 > p
int i = 5, j = 7, k = 12; Answer = 0
double x = 22.5;
5. 25 >= x + 1.0
Evaluate Answer = 1
1. i + 2 == k 1
Answer = 0
2. 3 * i j < 22
Answer = 1
ACTIVITY 2
Evaluate the following expression
(6 * 3 == 36/2) || (13 < 3 * 3 + 4) && !(6 2 < 5)
(18==18)||(13==13)&&!(True)
(1) || (1) && (0)
Answer = 1
EXAMPLE: