Professional Documents
Culture Documents
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;
return 0;
}
Multiple statement within if
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");
}
}