3. Forms of if statement
Simple if statement.
If…else statement.
Nested if…else statement.
Else .. if ladder.
4. IF STATEMENT
If statement is the decision making statement.
General form:
If (test expression)
{
statement – block;
}
statement-x;
5. IF STATEMENTS
EXAMPLE PROGRAM :
Main()
{
int a,b,c,d;
float ratio;
Printf(“Enter four integer valuesn”);
Scanf(“%d,%d,%d,%d”,&a,&b,&c,&d);
If(c-d !=0) /*execute statement block*/
{
ratio = (float)(a+b)/(float)(c-d);
printf(“ratio =%fn”,ratio);
}
}
7. IF… ELSE STATEMENTS
If … else statement is the extension of if statement.
General form:
If (test expression)
{
//statement (s)
}
else
{
//statement(s)
}
8. IF ELSE STATEMENTS
EXAMPLE PROGRAM:
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
10. NESTED IF ELSE STATEMENT
The nested if else statement is that contains an if
else inside the body of the another if else
statements.
If the condition 1 is false the statement three will
be executed ;
Otherwise it contains to perform the second test .
If the condition 2 is true
11. GENERAL FORM:
if (testExpression1)
{
// statement(s)
}
else if(testExpression2)
{
// statement(s)
}
else if (testExpression 3)
{
// statement(s)
}
.
.
else
{
// statement(s)
}
12. NESTED IF ELSE STATEMENTS
include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2)
{
if (number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}
14. NESTED IF ELSE STATEMENT
OUTPUT:
if (a > b) {
print("Hello");
}
print("Hi");
15. ELSE IF LADDER
The if...else statement execute two different
codes depending upon whether the test
expression is true or false. Sometimes a choice
has to be made from more than two possibility.
The if else ladder allows you to check for
multiple test expressions and execute various
statement(s).
18. SWITCH STATEMENT
A switch statement allows a variable to be
tested for equality against a list of values. Each
value is called a case, and the variable being
switched on is checked for each switch case.