Decision control statement

Decision Control Statements

1. if statement

The statements inside if body executes only when the condition defined by if statement is true. If the condition is false then compiler skips the statement enclosed in if’s body. We can have any number of if statements in a C program.

Syntax :-

if (condition)

{

     /* statements to be executed if condition is true. */

}

2. if-else statement

In this decision control statement, we have two block of statements. If condition results true then if block gets executed else statements inside else block executes. else cannot exist without if statement. In this tutorial, I have covered else-if statements as well.

Syntax :-

if (condition)

{

    /* statements to be executed if condition is true */

}

else

    /* statement will execute if the condition is false */

}

3. Nested if else

You can use one if or else if statement inside another if or else if.

Syntax :-

if (condition1)

{

   // Executes when condition1 is true

         if (condition2)

   {

   // Executes when condition2 is true

   }

}

4. else if ladder

Here, we can check multiple test conditions. it is also known as multiple if else statement, it checks test conditions one by one.

Syntax :-

if(test_condition1)

{

      //block1;

}

else if(test_condition2)

{

       //block2;

}

.

.

else

{

      //else block;

}


NOTE :-
 If test_condition1 is true then block1 will be executed, if it is false then test_condition2 will be checked, if test_condition2 is true then block2 will be executed, if it is false then next condition will be checked. If any of the test conditions is not true then else block will be executed. 

5. Switch statement

The switch case statement is used when we have multiple options and we need to perform a different task for each option.

Syntax :-

switch(variable)

{

case 1:

   //execute your code

break;

case n:

   //execute your code

break;

default:

   //execute your code

break;

}


6. break statement

In the switch statement, break is use to terminate the case. In the loops, break is use to instantly terminate the loop and program control transfer to the out of the loop.

Syntax :-

//loop or switch case

           break ;

7. continue statement

The continue statement skips the current iteration of the loop and continues with the next iteration. Continue can only be executed in a loop.

Syntax :-

//loop statements 

      continue;

8. goto statement

The goto statement allows us to transfer control of the program to the specified label.

Syntax :-

goto label;

........

label:

statement;

 
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free