C if-else statement

If-else Statement in C Programming

We can use if-else statement in c programming so that we can check any condition and depending on the outcome of the condition we can follow appropriate path. We have true path as well as false path.

Syntax :

if(expression)
   {
   statement1;
   statement2;
   }
 else
   {
   statement1;
   statement2;
   }
next_statement;

Explanation :

  • If expression is True then Statement1 and Statement2 are executed
  • Otherwise Statement3 and Statement4 are executed.

Sample Program on if-else Statement :

void main()
{
int marks=50;
 if(marks>=40)
   {
   printf("Student is Pass");
   }
 else
   {
   printf("Student is Fail");
   }
}

Output :

Student is Pass

Flowchart : If Else Statement

Consider Example 1 with Explanation :

Consider Following Example -

int num = 20;
if(num == 20)
    {  
    printf("True Block");
    }
else
    {
    printf("False Block");
    }

If part Executed if Condition Statement is True.

if(num == 20)
    {  
    printf("True Block");
    }

True Block will be executed if condition is True.
Else Part executed if Condition Statement is False.

else
    {
    printf("False Block");
    }

Consider Example 2 with Explanation :

More than One Conditions can be Written inside If statement.

int num1 = 20; 
int num2 = 40;
if(num1 == 20 && num2 == 40)
    {  
    printf("True Block");
    }

Opening and Closing Braces are required only when “Code” after if statement occupies multiple lines. Code will be executed if condition statement is True. Non-Zero Number Inside if means “TRUE Condition”

If-Else Statement :

if(conditional)
{  
//True code
}
else
{
//False code
}

Note :

Consider Following Example -

int num = 20;
if(num == 20)
    {  
    printf("True Block");
    }
else
    {
    printf("False Block");
    }
  1. If part Executed if Condition Statement is True.
  2. if(num == 20)
        {  
        printf("True Block");
        }
    

    True Block will be executed if condition is True.

  3. Else Part executed if Condition Statement is False.
  4. else
        {
        printf("False Block");
        }
    
  5. More than One Conditions can be Written inside If statement.
  6. int num1 = 20; 
    int num2 = 40;
    if(num1 == 20 && num2 == 40)
        {  
        printf("True Block");
        }
    
  7. Opening and Closing Braces are required only when “Code” after if statement occupies multiple lines.
  8. Code will be executed if condition statement is True.
  9. Non-Zero Number Inside if means “TRUE Condition”

Program Flow of If-Else Statement :