Statements and blocks in C
Generally C program statement is executed in an order in which they appear in the program .
But sometimes there are number of situation we need to change the order of execution, these are the decision making condition for execution part of certain condition that is called control statement.
Control statement defined how the control is transferred from one part of program to the other part of the program.
There are several decision control statement like if...else, switch, while, do....while, for loop, break, continue, goto etc.
if Statement
if statement will execute when the condition is true, if condition is false complier will skip the execution of statement, and syntax is as follow:
if(Condition)
{
Statement 1;
Statement 2;
}
The syntax shows the general use of if statement.
The statement block may be single or Multiple statement and it can be enclosed into the { } pair of braces.
Every statement is closed using semicolon (;)
No semicolon allowed after the condition.
If else Statement:
if the condition is true only if statement block will executed and if condition is false then else statement block will be executed, and syntax is as follow:
if(Condition)
Statement;
else
Statement;
The syntax shows the general use of if statement. The statement block may be single or Multiple statement and it can be enclosed into the { } pair of braces.Every statement is closed using semicolon (;)No semicolon allowed after the condition.
Flow Chart of If .. else Statement:
Example of If .. else Statement:
/* Program for simple if-else statement */
#include<studio.h>
Nested if-else
Statement;
}
}
if(condition)
{
if(condition)
Statement;
else
{
Statement;
Statement;
}
else
Statement;
}
if (condition)
Statement;
else if(condition)
Statement;
else if(condition)
Statement;
else
Statement;
Example : Program to calculate result of examination .
/* Program for nested if-else statement */
#include<studio.h>
percentage(s1+s2+s3+s4+s5+s6)*100/600;
if(percentage<=59 && percentage>=50)
printf("Second division");
else if(percentage<=49&& percentage>=40)
printf("passed");
else
printf("fail");
}
getch();
}