Lecture-4
Conditional Execution
Dr. Abdul Hameed
Opverse - Tech
Objectives
▪ Explain the Selection Construct
- If Statement
- If – else statement
- Multi if statement
- Nested if statement
Relational expressions
• The <condition> used in an if or if/else statement
• These conditions are called relational expressions.
• Relational expressions use one of the following six relational operators:
Operator Meaning Example Value
== equals 1 + 1 == 2 true
!= does not equal 3.2 != 2.5 true
< less than 10 < 5 false
> greater than 10 > 5 true
<= less than or equal to 126 <= 100 false
>= greater than or equal to 5.0 >= 5.0 true
Conditional Statement
▪ Conditional statements enable us to change the flow of the program
▪ A conditional statement evaluates to either a true or a false value
Example :
To find whether a number is even or odd we proceed as follows :
1. Accept a number
2. Find the remainder by dividing the number by 2
3. If the remainder is zero, the number is “EVEN”
4. Or if the remainder is not zero the number is “ODD”
Selection Constructs
C supports two types of selection statements
The if statement
The switch statement
The if statement
Syntax:
If the if expression evaluates to true, the block following
the if statement or statements are executed
The if statement
Program to display the values based on a condition
#include <stdio.h>
int main()
{
int x, y; Example
char a = ‘y’;
x = y = 0;
if (a == ‘y’)
{
x += 5;
printf(“The numbers are %d and \t%d”, x, y);
}
if – else statement
Syntax:
if – else statement
▪ If the if expression evaluates to true, the block
following the if statement or statements are executed
▪ If the if expression does not evaluate to true then the statements
following the else expression take over control
▪ The else statement is optional. It is used only if a statement or a
sequence of statements are to be executed in case the if
expression evaluates to false
if – else statement
Program to display whether a number is Even or Odd
#include <stdio.h>
int main()
{
int num , res ;
Example
printf(“Enter a number :”);
scanf(“%d”,&num);
res = num % 2;
if (res == 0)
printf(“Then number is Even”);
else
printf(“The number is Odd”);
}
if–else–if statement
Syntax:
if–else–if statement
▪ The if – else – if statement is also known
as the if-else-if ladder or the if-else-if
staircase
▪ The conditions are evaluated from the top
downwards
if–else–if statement
Program to display a message based on a value
#include <stdio.h>
Int main()
{
int x;
x = 0;
printf(“Enter Choice (1 - 3) : “);
Example
scanf(“%d”, &x);
if (x == 1)
printf (“\nChoice is 1”);
else if ( x == 2)
printf (“\nChoice is 2”);
else if ( x == 3)
printf (“\nChoice is 3”);
else
printf (“\nInvalid Choice “);
}
Nested if
▪ The nested if is an if statement, which is placed
within another if or else
▪ In C, an else statement always refers to the
nearest if statement that is within the same
block as the else statement and is not already
associated with an if
Nested if
Syntax:
▪ Note that the inner else is associated with if(exp3)
▪ According to ANSI standards, a compiler should
support at least 15 levels of nesting
Nested if
#include <stdio.h>
int main ()
{
int x, y;
x = y = 0;
printf (“Enter Choice (1 - 3) : “);
scanf (“%d”, &x);
Example
if (x == 1)
{
printf(“\nEnter value for y (1 - 5) : “);
scanf (“%d”, &y);
if (y <= 5)
printf(“\nThe value for y is : %d”, y);
else
printf(“\nThe value of y exceeds 5 “);
}
else
printf (“\nChoice entered was not 1”);
}