C if else statement and programs with exampls

C if else and if …else Statement and Nested If…else Statements in C 

C if statement: The if statement is decision-making statement and is used to control the flow of executions of statements. If is the two-way decision statement and is used in conjunction with a Test expression.

Declaration of if statement:
if(test expression).

Explanation: In the above declaration the if statement allows the computer to evaluate the expression first and then depending on whether the value of the expression (means condition) is “True” or “False” it transfers the control to next statement. The program has to follow two paths one for the true condition another for the false condition.
 SIMPLE IF STATEMENT
If (test expression)
    {
      statement-block;
     }
      statement-n;
Explanation: The statement block may be a single statement or a group of statements. If the test expression is true, the statement-block will be executed otherwise the statement-block will be skipped and the execution will jump to the statement-n;

C if else examples using Integers:

C if else examples using Integers:
#include <stdio.h>
int main()
{
    int i = 6, j = 7;
    if(i<j)
    {
     printf("J is Greater then I");
   }
   return 0;
}
Output of c if else program:
J is Greater then I
C if else example2:

C if else Example 2:
#include <stdio.h>
int main()
{
    int i = 6, j = 7;
    if(8>6)
 {
 printf(" Welcome to TutorialsWeb ");
 }
   return 0;
}
Output:
Welcome to TutorialsWeb
else: It is a keyword using this keyword we can create an alternate block for “if” condition using “else” means always optional, it is recommended to use. When we have an alternate block of “if” condition. When we are working with “if” and “else” only one the block will be executed and when the, if the condition is false, then only else part will be executed.


if …else Statement: The if …else statement is an extension of the simple if statement. 

The Syntax of the if…else statement is below:

Syntax of C if …else statement
if(test expression)
{
 True-block statement(s);
}        
else
{
 False-block statement(s);
}
statement(x);

C if else Explanation: If the Test expression is true, then the true-block statement(s), immediately following the if statements are executed. Or the False-block Statements are executed. In other cases, either true-block (or) False-block will be executed, not both. Then the control is transferred subsequently to the statement-x;


C if.... else program:

Example 2:
#include <stdio.h>
int main()
{
    int i,j;
    printf("Enter a number the value of i = ");
    scanf("%d",&i);
    printf("Enter a number the value of j = ");
    scanf("%d",&j);
 if(i > j)
 {
 printf("I is Greater then J \n");
 }
 else
 {
 printf("J is Greater then I");
 }
printf("Out Of If Else Loop");
   return 0;
}
Output:
Enter a number the value of i = 14
Enter a number the value of j = 4
I is Greater then J
Out Of If Else Loop
Explanation of above if ...else program:
  1. Two integers “I” and “J” are declared. Input values are received for both “I” and “J” using Scanner.
  2. In the above program if else condition statement is used to check whether “I” is greater than “J”.
  3. In the above program if else condition statement is used to check whether “I” is greater than “J”.
  4. If “J” is greater than “I”, the else part will be executed. The Print message “J is Greater then I” is Printed.
  5. After completing the else part the statement-x will be executed.
Nested If…else Statements in C: When a series of the decision is involved then we have to use more than one if…else statement in the nested form shown below.

Explanation of Nested If…else Statements: In the above logic of execution if the Test condition-1 is True, it continues to the Test condition-2. If the Test condition-2 is True, the statement-1 will be evaluated other-wise else statement-2 evaluated. If the condition-1 is false, the statement-3 will be executed, and then the control is transferred to the statement-x;

Nested If…else Statements program:

Nested If...else program:
#include <stdio.h>
int main()
{
    int n;
    printf("Enter a number the value of n = ");
    scanf("%d",&n);
 if(n > 0)
 {                                                                                          
 if(n == 0)
 {
  printf("It is Zero");
 }
 else
 {
 printf("Number is greater than Zero");
 }
 }
 else
 {
 printf("Number is less than Zero");
 }
   return 0;
}
The output of Nested if...else program:
Enter the Number of n =
12
Number is greater than Zero
The Else If Ladder: Another way of putting ifs together when multipath decisions are involved. A multipath decision of ifs in which the statement associated with each else is an if. The syntax of Else If Ladder is shown below:

Explanation: In the above Else If Ladder the conditions are evaluated from the top of the ladder to downwards. When the condition found to be True, the statement associated with it is executed and the control is transferred to the statement-x skipping the rest of the ladder. When all the n conditions become False, then the final else with default statement will be executed.
C if else ladder Program:

C if else ladder program:
#include <stdio.h>
int main()
{
    int m;
    printf("Enter a number the value of m = ");
    scanf("%d",&m);
  if(m<50)
    {            
     printf("fail"); 
    } 
    else if(m >= 50 && m < 60)
    { 
     printf("D grade"); 
    } 
    else if(m >= 60 && m < 70)
    { 
    printf("C grade"); 
    } 
    else if(m >=70 && m < 80)
    { 
     printf("B grade"); 
    } 
    else if(m >= 80 && m < 90)
    { 
     printf("A grade"); 
    }
    else if(m >=90 && m < 100)
    { 
     printf("A+ grade"); 
    }
    else
    { 
     printf("Invalid!"); 
    } 
   return 0;
}

Output:
Enter the Marks
77
B grade
Share:

Our Services

Search This Blog

  • ()
Powered by Blogger.

TikTok Sign Up and Account Registration in detail

TikTok Sign Up and Account Registration  The steps to create an account on Tik Tok application for Android and ios. Download and introd...

Blog Archive