0% found this document useful (0 votes)
2 views

4. Conditional Statement1 2

Uploaded by

vibeshepta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

4. Conditional Statement1 2

Uploaded by

vibeshepta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Conditional Statement

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.

Note: Never place a semicolon after the expression in an if statement


Example
Write a program that will output “ Congratulations you PASSED!” if the
students grade is greater than or equal to 60.

#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!”

#include<stdio.h> else if (day==4)


main() printf (“Wednesday”);
{ else if (day==5)
int day; printf (“Thursday”);
else if (day==6)
printf (“Enter an interger:”);
printf (“Friday”);
scanf (“%d”, &day); else if (day==7)
if (day== 1) printf (“Saturday”);
printf (“Sunday”); else
else if (day==2) printf (“Day is not available!”);
printf (“Monday”); getch();
}
else if (day==3)
Output:
printf (“Tuesday”); Enter an integer: 4
Wednesday
SWITCH Statement
• General form of the switch statement is:
switch (variable)
{
case constant1:
statement_sequence_1;
break;
case constant2:
statement_sequence_2;
break;
:
default:
statement_sequence_default;
}
SWITCH Statement
• In a switch statement, a variable is
successively tested against a list of integer or
character constants. If a match is found, a
statement or block of statements is executes.
The default part of the switch is executed if no
matches are found.
Three important things to know about switch
statements
1. The switch differ from if statements in such a way the
switch can only test for equality whereas if can evaluate a
relational or logical expression.
2. No two case constants in the same switch can have
identical values. Of course, a switch statement enclosed
by an outer switch may have case constant that are the
same.
3. If character constants are used in the switch, they are
automatically converted to their integer values.

Note: The break statement is used to terminate the statement sequence


associated with each case constant. It is a Turbo C keyword which means that
at that point of execution, you should jump to the end of the switch
statement terminated by the symbol }
NESTED SWITCH Statement
• General form of the nested switch statement is:
switch (variable)
{
case constant1; {
switch (variable)
{
case constant1:
statement sequence;
break;
case constant2:
statement sequence;
break;
}
break;
}
case constant2:
statement sequence;
break;
:
default:
statement sequence;
}
Example
Output:
Rewrite the program in if –else –if –ladder Enter an integer: 4
Wednesday
#include<stdio.h>
main() case 3:
printf (“Tuesday”);
{ break;
int day; case 4:
printf (“Enter an interger:”); printf (“Wednesday”);
break;
scanf (“%d”, &day); case 5:
switch (day) printf (“Thursday”);
{ break;
case 6:
case 1: printf (“Friday”);
printf (“Sunday”); break;
break; case 7:
printf (“Saturday”);
case 2: break;
printf (“Monday”); default:
break; printf (“Day is not available!”);
}
getch();
}

You might also like