Você está na página 1de 26

CSC-113 COMPUTER PROGRAMMING

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.

Control structure is used to design data flow in modules and program


as a whole.
TYPES OF CONTROL STRUCTURES
1. Sequential structure
Processes one instruction after another

2. Selection structures (if, if-else, switch)


Decision structure
Make choices by using relational or logical operators
Case structure
Enable to pick up one of a set of tasks

3. Loop structure (for, while, do-while)


Enable to repeat tasks
FLOW OF EXECUTION
TYPES OF SELECTION STATEMENTS
There are three types of selection statements:

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:

We have covered conditional operator ? : in previous chapter which can be used to


replace if...else statements. It has the following general form:

Exp1 ? Exp2 : Exp3;

Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the
colon.

The value of a ? expression is determined like this: Exp1 is evaluated. If it is true,


then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is
false, then Exp3 is evaluated and its value becomes the value of the expression.
BOOLEAN ALGEBRA
Truth tables
Lists all combinations of operand values and the result of the operation for
each combination

Example
P Q P and Q

False False False


False True False
True False False
True True True
BOOLEAN ALGEBRA
Or truth table

P Q P or Q

False False False


False True True
True False True
True True True
BOOLEAN ALGEBRA
Not truth table

P not P

False True
True False
BOOLEAN ALGEBRA
Can create complex logical expressions by combining simple logical expressions
Example
not (P and Q)

A truth table can be used to determine when a logical expression is true

P Q P and Q not (P and Q)

False False False True


False True False True
True False False True
True True True False
A BOOLEAN TYPE
C++ contains a type named bool
Type bool has two symbolic constants
true
false

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

If the Expression is true then


true false
execute Action

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

int rememberValue1 = value1


value1 = value2
value2 = rememberValue1

The numbers were


rearranged into the
proper order
The numbers were
initially in order
The numbers are in
order
THE If-Else STATEMENT
Syntax

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

Max = Value2 Max = Value1

Either case, Max is set


correctly
SELECTION
It is often the case that depending upon the value of an expression we
want to perform a particular action
Two major ways of accomplishing this choice
if-else-if statement
if-else statements glued together

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:

Você também pode gostar