4. Conditional Statement1 2
4. Conditional Statement1 2
Conditional Statements
• are statements that check an expression then may
or may not execute a statement or group of
statements depending on results of the condition.
5 types of conditional statements
1. IF statement
2. IF-ELSE statement
3. NESTED-IF statement
4. IF-ELSE-IF ladder
5. SWITCH statement
IF statement
• The general form of the if statement is:
if(expression)
statement;
expression is relational or boolean expression
that evaluates to a TRUE or FALSE (0) value
statement may either be a single statement or a
block of true statements
IF statement
• The general form of the if statement with block of
statements is:
if (expression)
{
statement_sequence;
}
if the expression evaluates to TRUE(1), the statement or the
block of statements that forms the target of the if
statement will be executed. Otherwise, the program will
ignore the statement or the block of statements.
#include<stdio.h> Output:
int grade;
main() Enter student grade: 96
Congratulations you PASSED
{
clrscr();
printf (“Enter Student grade:”);
scanf (“%d”, &grade);
if (grade >=60)
printf (“Congratulations you PASSED!”);
getch();
}
IF-ELSE statement
• The general form of the if-else statement is:
if (expression)
statement_1;
else
statement_2;
where:
expression is relational or boolean expression that
evaluates to a TRUE(1) or FALSE(0) value statement_1
and statement_2 may either be a single statement or a
block of statements
IF-ELSE statement
The general form of the if-else statement with block of statements is:
if (expression)
{
statement_sequence;
}
else
{
statement_sequence;
}
if the expression is TRUE(1), the statement of block of statements after the
if statement will be execution otherwise, the statement or block of
statements in the else statement will be executed.
Note: Only the code associated with the if or the code that is associated
with the else executes, never both.
Example
Write a program that will output “ Congratulations you PASSED!” if the students
grade is greater than or equal to 60. Otherwise output, “Sorry you FAILED!”
#include<stdio.h>
int grade; Output:
main()
Enter student grade: 45
{
Sorry you FAILED!
clrscr();
printf (“Enter Student grade:”);
scanf (“%d”, &grade);
if (grade >=60)
printf (“Congratulations you PASSED!”);
else
printf (“Sorry you FAILED!”);
getch();
}
NESTED-IF statement
• One of the most confusing aspects of the if
statement in any programming language is nested if.
• Nested if is an if statement that is the object of
either an if or else. This is sometimes referred to as
“an if within an if.”
• The reason that nested ifs are so confusing is that it
can be difficult to know what else associates with
what if. Turbo C provides a very simple rule for
resolving this type of solution. “else is linked to the
closest preceding if that does not already have an
else statement associated with it.”
NESTED-IF statement
• Situation 1. The else at number 3 is paired
with the if in number 2 since it is the nearest if
statement with the else statement:
1. if……
2. if …….
.
.
.
3. else
NESTED-IF statement
• Situation 2. The else in number 5 is paired with the if in number 1.
1. if……
2. {
3. if …….
.
.
4. }
5. else
Notice that there is a pair of braces found in no. 2 and no. 4. The pair of
braces defines the scope of the if statement in no.1 starting from the
{no 2 and ends with } in no.4. Therefore, the else statement in no.5
cannot be paired with the if statement in no. 3 because the else
statement is outside the scope of the first if statement. This makes
the if statement in no.1 the nearest if statement to the else statement
in no.5
Example
Write a program that reads in three numbers A,B, and C and determine which is the largest.
#include<stdio.h>
int A,B,C;
main()
{
clrscr();
printf (“Enter three numbers:”); Output:
scanf (“%d%d%d”, &A, &B, &C);
if (A >B) Enter three numbers: 7 11 5
if (A>C) B is the largest.
printf (“A is the largest.\n”);
else
printf (“C is the largest.\n”);
else
if (B>C)
printf (“B is the largest.\n”);
else
printf(“C is the largest.\n”);
getch();
}
IF-ELSE-IF LADDER
• The general form of the if-else-if ladder statement is:
if (expression_1)
statement_1;
else if (expression_2)
statement_2;
else if (expression_3)
statement_3;
:
else
statement_else;
where:
expr_1, expr_2 up to expr_n is relational or boolean expression that evaluates
to a TRUE(1) or FALSE(0) value.
statement_1, statement_2 up to statement_else may either be a single
statement or a block of statement.
IF-ELSE-IF LADDER
• In an if-else-if ladder statement, the expressions are
evaluated from the top downward. As soon as a true
condition is found, the statement associated with in is
executed, and the rest of the ladder will not be
executed. If none of the condition is true, the final
else is executed.
• The final else acts as a default condition. If all other
conditions are false, the last else statement is
performed. If the final else is not present, then no
action takes place.
Note: The final else is optional, you may include this part if needed
in the program of you may not include if not needed.
Example
Write a program that will ask the user to input an integer then output the equivalent
day of the week, 1 is Sunday, 2 is Monday and so on. If the inputted number is not
within 1-7 output “Day is not available!”