You are on page 1of 11

Decision Control

Instructions
Decision control instruction can be
implemented using
 if statement
 if-else & else-if statement
  switch-case statements
if statement
 C uses the keyword if to implement the decision control instruction.
 general form of if statement looks like this:

 The keyword if tells the compiler that what follows is a decision control instruction.
 The condition following the keyword if is always enclosed within a pair of parentheses.
 If the condition, whatever it is, is true, then the statement is executed. If the condition is not true,
then the statement is not executed; instead the program skips past it.
Example:

#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
prinf(“end of program”);
}
Example:
 #include<stdio.h>
 int main()
 {
 // This variable is to store the input number
 int num;

 printf("Enter an integer: ");


 scanf("%d",&num);

 // Modulus (%) returns remainder


 if ( num%2 == 0 )
 printf("%d is an even number", num);
 else
 printf("%d is an odd number", num);

 return 0;
 }
Multiple statement within if

 We can use multiple if statements to check more than one conditions.


Example
 #include <stdio.h>
 int main()
 {
 int x, y;
 printf("enter the value of x:");
 scanf("%d", &x);
 printf("enter the value of y:");
 scanf("%d", &y);
 if (x>y)
 {
 printf("x is greater than y\n");
 }
 if (x<y)
 {
 printf("x is less than y\n");
 }
 if (x==y)
 {
 printf("x is equal to y\n");
 }
 printf("End of Program");
 return 0;
 }
If-else

 we can execute one group of statements if the expression evaluates to true and
another group of statements if the expression evaluates to false
 the else statement can be used to do so.
 If condition returns true then the statements inside the body of “if” are executed
and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped
and the statements in “else” are executed.
Example
 #include <stdio.h>
 int main()
 {
 int age;
 printf("Enter your age:");
 scanf("%d",&age);
 if(age >=18)
 {
 printf("You are eligible for voting");
 }
 else
 {
 printf("You are not eligible for voting");
 }
 }

You might also like