Anda di halaman 1dari 11

Conditional Statement

• Are statements that check an expression then may or may not


execute a statement or group of statements depending on the result
of the condition.
types of conditional statement
• if statement
• if else statement
• if else if ladder statement
• nested switch statement
If statement
Syntax:
if(expression)
{
statement sequence;
}
if statement, if the expression evaluates to TRUE the statement or the
block of statements that forms the target of the if statement will be
executed
Example of if statement

Prob. Write a program that will output “Congratulations you Passed” if the
students grade is greater than or equal to 75.
#include <stdio.h>
int grade;
int main( )
{
printf(“\n Enter student grade: “);
scanf(“%d”, &grade);
if (grade>=75) {
printf(“congratulations you Passed”); }}
If-else statement
Syntax:
if(expression)
statement1;
else
statement2;
In an if-else statement, if the expression is TRUE(1) the
statement or block of statements after the if statement will be
executed, otherwise the statement or block of statements in the else
statement will be executed.
Example of if else statement
Problem: write a program that will be output “Congratulations you Passed” if the students grade is
greater than or equal to 75, otherwise the output “sorry you failed”.
#include<stdio.h>
int grade;
int main
{
printf(“Enter student grade:”);
scanf(“%d”,&grade);
if(grade>=75)
printf(“congratulations you passed”);
else
printf(“Sorry you failed”); }
if-else-if ladder
Syntax: if(expression1)
statement1;
else if(expression2)
statement2;
else if(expression3)
statement3;
else
statement_else;
if-else-if statement
• In this statement the expressions are evaluated from the top
downward. As soon as a true condition is found, the statement
associated with it 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 condition
are false the last else statement is performed. If the final else is not
present, then no action takes place.
Ex. Write a program that will ask the user input an integer then
the output is
the equivalent day of the week. 1 is Sunday, 2 is Monday and
so on.
#include <stdio.h>
int main else if(day==4)
{ printf(“Wednesday”);
int day; else if(day==5)
printf(“Enter an integer: “); printf(“Thursday);
scanf(“%d”,&day); else if(day==6)
If(day==1) printf(“Friday”);
printf(“Sunday”); else if(day==7)
else if(day==2) printf(“Saturday”);
printf(“Monday”); else
else if(day==3) printf(“day is not available”);
printf(“Tuesday”); }
Nested switch statement
switch statement is a multiple branch decision statement . It is a
statement variable is successively tested against a list of integer or
character constants. If a match is found a statement or block of
statements is executed. The default part of the switch is executed if no
matches are found.
NOTE:
The break statement is used to terminate the statement sequence
associated with each case constant. It is a keyword which means that
at the point of execution, you should jump to the end of the switch
statement terminated by the symbol }.
Syntax: Nested Switch Statement
switch(variable)
{ case constant1:{
switch(variable){
case constant1:
statement sequence;
break;
case constant2:
statement sequence;
break;
default:
statement sequence; }}}
Enter an integer: 11
Day is not available

#include<stdio.h>
main() case 2:
{ printf(“Monday”);
int day; break;
printf(“Enter an integer:”); case 3:
scanf(“%d”, &day); printf(“Tuesday”);
switch (day){ break;
case 1: …….
printf(“Sunday”); default:
break; printf(“Day is not available”);
}}