C if Statement

 What is Decision Making Statement?

In the C programming language, the program execution flow is line by line from top to bottom. That means the c program is executed line by line from the main method. But this type of execution flow may not be suitable for all the program solutions. Sometimes, we make some decisions or we may skip the execution of one or more lines of code. Consider a situation, where we write a program to check whether a student has passed or failed in a particular subject. Here, we need to check whether the marks are greater than the pass marks or not. If marks are greater, then we decide that the student has passed otherwise failed. To solve such kind of problems in c we use the statements called decision making statements.

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.


In the c programming language, there are two decision-making statements they are as follows. 

  • if statement
  • switch statement

 
if statement in c

In c, if statement is used to make decisions based on a condition. The if statement verifies the given condition and decides whether a block of statements are executed or not based on the condition result. In c, if statement is classified into four types as follows... 

  • Simple if statement
  • if-else statement
  • Nested if statement
  • if-else-if statement (if-else ladder)
 
Simple if statement
 
The simple if statement evaluates specified condition. If it is TRUE, it executes the next statement or block of statements. If the condition is FALSE, it skips the execution of the next statement or block of statements.
 
Syntax
 
if(condition)
{
 block of statement - 1;
 block of statement - 2;
      .........
     ..........
block of statement - N

Example
 
To check whether a given number is even or not.
 
#include<stdio.h>
#include<conio.h>

void main()
{
int number=0;
printf("Enter a number:");    
scanf("\n %d",&number);    
if(number%2==0)
{    
printf("\n %d is even number",number);    
}    
getch();
}


Output
 
 

 

if-else statement
 
The if-else statement is used to verify the given condition and executes only one out of the two blocks of statements based on the condition result. The if-else statement evaluates the specified condition. If it is TRUE, it executes a block of statements (True block). If the condition is FALSE, it executes another block of statements (False block).  
 
 
Syntax
 
if(condition)
{
 block of statement - 1;
 block of statement - 2;
      .........
     ..........
block of statement - N
else
{
 block of statement - 1;
 block of statement - 2;
      .........
     ..........
block of statement - N
}



Example
 
To check whether a given number is even or odd.

#include<stdio.h>
#include<conio.h>

void main()
{
int number=0;
printf("\n Enter a number:");    
scanf("%d",&number);    
if(number%2==0)
{    
   printf("\n %d is even number",number);    
}
else
{
   printf("\n %d is odd number",number);   
}    
getch();
}

Output
 

 
Nested if statement
 
Writing a if statement inside another if statement is called nested if statement.
When a series of conditions are to be checked, we may have to use more than one if... else statement in the nested form.

Syntax
 
if(condition 1)
{
 if(condition 2)
  {
 block of statements ;
else
{
 block of statements;
 }
 
Example
To check whether given number is even or odd if it is below 100.
 
 #include<stdio.h>
#include<conio.h>

void main()
{
int number=0;
printf("\n Enter a number:");    
scanf("%d",&number);
if(number<100)   
{
    printf("\n Given number is below 100\n") ;
    if(number%2==0)
  {    
    printf("\n %d is even number",number);    
  }
   else
  {
    printf("\n %d is odd number",number);    
  }    
}
 else
 {
     printf("Given number is not below 100");
 }

getch();
}
 
Output
 

 
if-else-if statement (if-else ladder)
 
Writing a if statement inside else of an if statement is called if-else-if statement. 
 
The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible.
 
 
Syntax
 
if(condition 1)
{
 block of statements ;
}
else  if(condition 2)
  {
 block of statements ;
else if(condition 3)
{
 block of statements;
 }
else
{
 block of statements;
}
 
Example
  
To calculate the grade of the student according to the specified marks
 
#include<stdio.h>
#include<conio.h>

void main()
{
int marks;   
    printf("\n Enter your marks:");  
    scanf("%d",&marks);   
    if(marks > 85 && marks <= 100)  
    {  
        printf("\n Congrats ! you scored grade A ...");   
    }  
    else if (marks > 60 && marks <= 85)   
    {  
        printf("\n You scored grade B + ...");  
    }  
    else if (marks > 40 && marks <= 60)   
    {  
        printf("\n You scored grade B ...");  
    }  
    else if (marks > 30 && marks <= 40)   
    {  
        printf("\n You scored grade C ...");   
    }  
    else   
    {  
        printf("\n Sorry you are fail ...");   
    }  
getch();
}


Output
 

 Notes
 
  •  if(10) - is TRUE
  • if(x) - is FALSE if x value is zero otherwise TRUE
  • if(a = 99) - is TRUE because a value is non-zero
  • if(a+b) - is FALSE if a+b value is zero otherwise TRUE
  • if(0) - is FALSE
  • if(10, 5, 0) - is FALSE because it considers last value
  • if(a=20, b=5, c=0) - is FALSE because last value is zero


 

No comments:

Post a Comment