DECISION-MAKING STATEMENTS IN C

Decision-Making Statements in C Programming Language allows you to make the decision based on certain condition to determine the order in which statement has to be executed, or repeat a group of statements until certain specified conditions are met.

Decision-Making Statements are also called Conditional Statements.

DECISION-MAKING STATEMENTS IN C
Flowchart of DECISION-MAKING STATEMENTS IN C

In C programming language there are following decision-making statements:

1. if statement

It executes the statement if the boolean condition is true.

In if statement, a single statement can be included without enclosing it into curly braces { }

Syntax of If statement

if( expression )
{
 statement 1;//Statement to be executed when the condition is true
}
 statement 2;

2. if…else statement

It executes the statement(s) followed by if -statement if the boolean condition is true otherwise it will execute the statement(s) followed by the else statement.

Syntax of If-Else statement

 if( expression )
{
 statement 1;//Statement to be executed when the condition is true
}
else
{
 statement 2;//Statement to be executed when the condition is false
}

3. nested if statements

It allows the use of conditional statement(s) inside another conditional statement.

Syntax of Nested If statement

 if( expression )
{
  if( expression1 )
   {
     statement 1;//Statement to be executed when the condition is true 
   }
  else 
   {
     statement 2;Statement to be executed when the condition is false
   }
}
else
{
 statement 3;Statement to be executed when the condition is false
}

4. switch statement

It allows you to test the variable for equality with respect to the value list.

Syntax of Nested If statement

switch(n) // n is any variable
{ 
case 1: 
statement(s);
break; 
case n: 
statement(s);
break; 
default: 
statement(s);
break; 
}

5. nested switch statements

It has one switch statement inside another switch statement.

Syntax of Nested If statement

switch(n) 
{
case 1: 	
statement(s);
switch(ch2) 
{
case 1:
statement(s);
break;
case n:
statement(s);
}
break;
case 2:
statement(s);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *