Conditional Statements
Conditional Statements
if (test expression)
{
// code or statements
}
• The if statement evaluates the test expression inside the parenthesis ().
if (test expression)
{
// run code if test expression is true
}
else
{
// run code if test expression is false
}
Program 2
• Write a program to find out the given number is positive or negative
using if-else.
#include <stdio.h>
int main()
{
int num1;
printf("Enter a non zero number : ");
scanf("%d", &num1);
if (num1>0)
{
printf(" %d is a positive number ", num1);
}
else
{
printf(" %d is a negative number ", num1);
}
return 0;
}
Nested If-else
• A nested if in C is an if statement that is the target of another if
statement. Nested if statements mean an if statement inside another
if statement. The syntax of the nested if..else statement is:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
Program 3
• Write a program to find out the biggest among three given numbers
using nested if-else.
#include <stdio.h>
}
else
{
if (num2>num1 && num2>num3)
{
printf(" %d is the greatest number ", num2);
}
else
{
printf(" %d is a negative number ", num3);
}
}
return 0;
}
If-elseif ladder
• In If elseif ladder the statements are executed from the top down. As
soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the C else-if ladder
is bypassed. If none of the conditions is true, then the final else
statement will be executed. if-else-if ladder is similar to the switch
statement.
if (condition)
{
statement;
else if (condition)
statement;
.
.
else
statement;
Program 4
• Write a program to display ONE if user enters 1, display TWO if user
enters 2 , display THREE if the input is 3 and for any other input display
invalid input using if-elif ladder.
#include <stdio.h>
}
else if(num1==2)
{
printf(" The entered number is TWO ");
}
else if(num1==3)
{
printf(" The entered number is THREE ");
}
else
{
printf(" The entered number is INVALID ");
}
return 0;
Program 5
• Display the grade of a student based on the marks. The grades are
given as follows
Marks >=90 - S grade
Marks 80 to 89 - A grade
Marks 70 to 79 - B grade
Marks 60 to 69 - C grade
Marks 55 to 59 - D grade
Marks 50 to 54 - E grade
Marks < 50 - F grade
Program 6
• Write a program to print the given THREE numbers in descending order.
Switch statement
• The switch statement in C is an alternate to if-else-if ladder statement
which allows us to execute multiple operations for the different
possible values of a single variable called switch variable. Here, We can
define various statements in the multiple cases for the different values
of a single variable.
• The switch statement allows us to execute one code block among many
alternatives.
• You can do the same thing with the if...else..if ladder. However, the
syntax of the switch statement is much easier to read and write.
Switch statement
• The syntax is
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
Rules for Switch statement
• The switch expression must be of an integer or character type.
• The case value can be used only inside the switch statement.