Statements and blocks in C

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.

Flow Char of If Statement:
If flow chart

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:

If else flow chart


Example of If .. else Statement:


/* Program for simple if-else statement */
#include<studio.h>
#include<conio.h>
void main()
{
   int a=10,b=20; 
   clrscr();
 if(a>b)
    printf("\n a is smaller"); 
else
    printf("\n b is grater");
getch();
}


Nested if-else

When we declare entire if –else statement in body of the if statement or in else statement then it is called nested if else. 
At-least one main if required so There should not else part without if ()
Syntax of Nested if-else

if(condition)
Statement;
else 
  { 
    if(condition)
      Statement; 
    else 
    { 
      Statement;
      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>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,s6; 
float percentage;
 clrscr();
printf("enter marks in six subject"); scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5,&s6);
percentage(s1+s2+s3+s4+s5+s6)*100/600;
if(percentage>=60)
    printf(" First division");
else
  {
if(percentage<=59 && percentage>=50)
  printf("Second division");
else if(percentage<=49&& percentage>=40)
  printf("passed");
else 
printf("fail");
  }
getch();
}

Previous
Next Post »