Conditional Instructions in C - C tutorials


Conditional Instructions in C - C tutorials

conditional instructions in c : On this page all data about conditional instruction in C explained in details. Below all data are mentioned of C language. Sometime we want to watch comedy videos on special day like if Sunday on youtube or any other broadcast.

Sometime we have order a junk food if friends birthday celebration in college. All of these decision depends on conditions are mating to each other. In C language is also same things we must able to execute instruction if the conditions will met to each others.conditional instructions in c.

Decision making instructions in C :conditional instructions in c

If – else statement

Switch statement

 

If statement     :

If statement is used check the conditions of any program hence If also known as conditional statement. “If statement execute if the condition is true. If the “if“ statement comes in program many time then we have to use curly braces { } . Here in case the of “ if “ false then then Compiler continue the or skip the line and will go to another line.

Statement executes set of commands if conditions is true and its syntax is ….

If(condition)     

     Statement;         

Example :

#include<stdio.h>

#include<conio.h>

void main()    

{

    int m;

   printf (“ enter a number: ”);

   scanf(“%d”,&m);

 

   If (m>10)

     Printf(“ number is grater”);

}

 

 

Output:

Enter a number:12

Number is greater         

In this example the if condition becomes true hence the compiler executes the line and show the output.

 

If – else statement :

The if else statement is bidirectional conditional control statement that is contained of one condition and two possible action. Condition may be true or false whereas, non-zero value considered as true and zero value as false. If the condition met true then single or block of statement will executed otherwise another  single or block of statement will be executed.

If the “if” conditions is true then the block of if statement will be executed and if the “if” condition is false compiler direct goes to else statement and then else statement will be perfectly executed.

The syntax of if-else statement :

        If(condition)

           {

   Statement1;

   Statement2;

}

                  

                   Else

               {

   Statement1;

Statement2;

    }

Else statement will not executed or allowed without use of if statement. In syntax we have to compulsory must be used one if statement with multiple else statement or one else statement.

Example :

   /* To check a number is eve or odd */ 

 

#include<stdio.h>

#include<conio.h>

void main()

{

          int n;

          printf (“enter a number:”);

          sacnf (“%d”, &n);

 

         If (n%2==0)

            printf (“even number”);

        else

            printf(“odd number”);

}

 

 

Output: enter a number:121

odd number

 

In this example the if condition is checked but the condition is not satisfied or true then the compiler goes direct to else statement and the statement to be executed.    

Nesting  of  if – else statement :-

Nesting is the one-by-one or again and again condtions will be checked by compiler is called nesting of condtions. When there are another if else statement in if-block or else-block, then it is called nesting of if-else statement.  

To be get certain conditions or output which is arounded by logic of programming the nested conditions is best way to get the output of these conditions.

Syntax is :-

 

      if (condition)

     { 

        If (condition)

          Statement1;

    else

        statement2;

    }

     Statement3;

      

Nesting of  If….else LADDER :

 

In this type of nesting there is an if else statement in every else part except the last part. If condition is false control pass to block where condition is again checked with its if statement.

 

Syntax is :-

 

      if (condition)

         Statement1;

        else if (condition)

             statement2;

        else if (condition)

             statement3;

     else

        statement4;

 

In this type of statements the condition firstly checked in “if” if the conditions is true then the compiler will executes the statement directly but if the “if” conditions is false then compiler will goes to “else if” statement while conditions will not become true the all else if conditions checked by compiler. If the all conditions are false then the compiler will goes to direct on else statement then the else statement will executed.

 

This process continue until there is no if statement in the last block. if one of the

condition is satisfy the condition other nested “else if” would not executed.                       

But it has disadvantage over if else statement that, in if else statement whenever the condition is true, other condition are not checked. While in this case, all condition are checked.

 



conditional instructions in c

 













No comments

This website is only for educational purpose, it is not related to any organization. This website helps to beginners in programming to learn fast.

Powered by Blogger.