Decision making with if statement
Simple if statement:
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
Here, condition after evaluation will be either true
or false.
The if statement
evaluates the condition inside the parenthesis ().
·
If
the condition is evaluated to true, statements inside the body of if are executed.
·
If
the condition is evaluated to false, statements inside the body of if are not executed.
Example 1:
#include<stdio.h>
#include<conio.h>
main()
{
int num1=1;
int num2=2;
if(num1<num2) //test-condition
{
printf("num1 is smaller than num2");
}
getch();
}
Example 2:
#include<stdio.h>
#include<conio.h>
main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n<10)
{
printf("%d is less than 10 ",n);
}
getch();
}
The if else Statement
if ... else statement is a two way branching
statement. It consists of two blocks of statements each enclosed inside if
block and else block respectively. If the condition inside if statement is
true, statements inside if block are executed, otherwise statements
inside else block are executed. Else block is optional and it may be
absent in a program.
Syntax
of if...else statement
if (condition)
{
statements;
... ...
...
}
else
{
statements;
... ...
...
}
Example 1: C program to find if
a number is odd or even.
#include<stdio.h>
#include<conio.h>
main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n%2 == 0)
{
printf("%d is even",n);
}
else
{
printf("%d is odd",n);
}
getch();
}
Example 2: C program to find if
a number is +ve or -ve.
#include<stdio.h>
#include<conio.h>
main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n > 0)
{
printf("%d is Positive number ",n);
}
else
{
printf("%d is Negative number ",n);
}
getch();
}
Nesting of if ...else statements
A nested if is an if statement that is the target of
another if statement. Nested if statements means an if statement inside another
if statement.
It is possible to include an
if...else statement inside the body of another if...else statement.
Example
1
#include <stdio.h>
#include <conio.h>
main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2)
{
if (number1 == number2)
{
printf("Both Numbers are equal”);
}
else
{
printf("number1 is big”);
}
}
else
{
printf("number2 is big”);
}
getch();
}
The else if ladder
The if...else statement executes
two different codes depending upon whether the test expression is true or
false. Sometimes, a choice has to be made from more than 2 possibilities.
The if...else ladder
allows you to check between multiple test expressions and execute different
statements.
Syntax of elseif ladder statement.
if (condition1)
{
// statement(s)
}
else if(condition2)
{
// statement(s)
}
else if (condition3)
{
// statement(s)
}
.
.
else
{
// statement(s)
}
Example:
#include <stdio.h>
#include <conio.h>
main()
{
int number1, number2;
clrscr();
printf("Enter two integers: ");
scanf("%d %d",
&number1, &number2);
if(number1 == number2)
{
printf("Both
Numbers are equal");
}
else if (number1 > number2)
{
printf("Number
1 is big ”);
}
else
{
printf("Number
2 is big ”);
}
getch();
}