You are on page 1of 23

Conditional Control

Structure

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Conditional Control Structure
 Organized in such a way that there is always a condition that has to
be evaluated first. The condition will either evaluate to a true or
false.
 In C, false is zero (0) and true is non-zero (i.e. positive or negative
value).
 2 Types:
1. if statement (including if-else and nested if)
2. switch case statement

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Conditional Operators

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Logical Operators

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Notes on Operators
• When the expression is a
combination of arithmetic,
relational and logical
operations, it is evaluated
following the order of
precedence as detailed below
where the topmost has the
higher precedence down to
the lowest.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
If Structures
• The if statement performs a task or a group of tasks when the
condition is true or when it is false, if there is an alternative for false.
For multiple statements inside an alternative, they should be enclosed
with a pair of braces.
• Types of if:
1.if (single alternative)
2.if-else (double alternatives)
3.If-else if-else (multiple alternatives)
4.Nested if

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
if (Single Alternative)
• Definition:
It performs an indicated action only when the condition is true, otherwise the action is skipped.
• Syntax:
if (<expression>)
<statement>
• Example:
if(age > 18)
printf(“You are an adult!”);
• Another Example:
if(age > 18){
printf(“You are an adult!”);
printf(“Welcome to the club!”);
}

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Example (Single-if)
Write a program that will allow the user to input an integer value. If the value is greater than or equal to
zero, print the word
“POSITIVE”.
Begin

num

T
if “POSITIVE”
(num>=0)

End

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Sample Problem (Single-if)
Write a program that will allow the user to input a number. Print the
word NEGATIVE if the number is a negative value.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
if-else (Double Alternatives)
Definition:
• It allows the programmer to specify that different actions are to be
performed when the condition is true than when the condition is false.
• If condition evaluates to true, then statementT is executed and statementF is
skipped; otherwise, statementT is skipped and statementF is executed.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
if-else (Double Alternatives)
• Syntax: • Example:
if (condition) if(age > 18)
statementT; printf(“You are an adult!”);
else else
statementF; printf(“You are a minor!”);

Condition T Statement

F
Statement

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Good Programming Practices
• Indent both body statements of an if...else statement.
• If there are several levels of indentation, each level should be indented
the same additional amount of space.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Sample Problem (Double-if)
Write a program that will allow the user to input a number. Print the
word NEGATIVE if the number is a negative value otherwise print
POSITIVE if the number is positive.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
If - else if - else (Multiple Alternatives)
The conditions in a multiple-alternative decision are evaluated in
sequence until a true condition is reached. If a condition is true, the
statement following it is executed, and the rest of the multiple-
alternative decision is skipped. If a condition is false, the statement
following it is skipped, and the condition next is tested. If all conditions
are false, then the statement following the final else is executed.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
If - else if - else (Multiple Alternatives)
• Syntax: Condition T Statement

if (condition)
F
statement;
Condition T Statement
else if (condition)
statement; F
else if (condition) Condition T Statement

statement; F
else
Statement
statement;

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Sample Problem (Multiple-if)
Make a C program that will accept a score and display the remark based
on the following conditions:
Score Remark
86 – 100 (inclusive) Very Good
75 – 85 (inclusive) Fair
Below 75 Failed

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Nested-if
• Placing another if statement inside an if statement is called a nested if.
• A nested if statement can perform much faster than a series of single-
selection if statements because of the possibility of early exit after one
of the conditions is satisfied.
• In a nested if statement, you test the conditions that are more likely to
be true at the beginning of the nested if statement. This will enable the
nested if statement to run faster and exit earlier than testing
infrequently occurring cases first.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Nested-if
• Syntax: • Example:
if (condition) if(num>0){
{ printf("Positive!");
if (condition) if(num%2==0)
printf(“Even!”);
statement;
else
else
printf(“Odd!”);
statement;
}
}
else if(num<0)
printf(“Negative!”);
else
printf(“Zero!”);
Prepared by: Dr. Cheryl B. Pantaleon
Fundamentals of Programming
Good Programming Practices
• A nested if...else statement can perform much faster than a series of
single-selection if statements because of the possibility of early exit
after one of the conditions is satisfied.
• In a nested if...else statement, test the conditions that are more likely
to be true at the beginning of the nested if...else statement. This will
enable the nested if...else statement to run faster and exit earlier than
testing infrequently occurring cases first.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Good Programming Practices
• Always putting the braces in an if...else statement (or any control
statement) helps prevent their accidental omission, especially when
adding statements to an if or else clause at a later time. To avoid
omitting one or both of the braces, some programmers prefer to type
the beginning and ending braces of blocks even before typing the
individual statements within the braces.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Good Programming Practices
• Forgetting one or both of the braces that delimit a block can lead to
syntax errors or logic errors in a program.
• Placing a semicolon after the condition in an if statement leads to a
logic error in single-selection if statements and a syntax error in
double-selection if...else statements (when the if part contains an
actual body statement).

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
switch-case Statement
• The switch statement uses a variable to be compared to a set of values.
Each value is called a case which should only be an int or a char, that
should also be the data type for the variable.
• In the switch statement, the variable or also called the controlling
expression, is evaluated and compared to each of the case labels in the
case constant until a match is found. A case constant is made of one or
more labels of the form case followed by a constant value and a colon.
When a match between the value of the controlling expression and a
case label value is found, the statement following the case label are
executed until a break statement is encountered. Then the rest of the
switch statement is skipped.
Prepared by: Dr. Cheryl B. Pantaleon
Fundamentals of Programming
Syntax (switch-case)
switch (controlling expression)
{
case constant: statement;
break;
case constant: statement;
break;
. . .
default: statement;
}
Prepared by: Dr. Cheryl B. Pantaleon
Fundamentals of Programming

You might also like