Decision-Making Statements in C

Decision-Making Statements in C

In this section of the C programming tutorial, we will learn about the Decision-Making Statements in C programming language: its types & syntax.

What are Decision-Making Statements in C programming?

Decision-making statements in the C programming language allow you to make the decision based on specific condition to determine the order in which statement has to be executed, or repeat a group of statements till particular conditions are met.

Please note that Decision-making statements are also known as conditional statements.

Types of Decision-Making Statements in C

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

  1.  if statements
  2.  if-else statements
  3. nested if statements
  4. switch statement
  5. nested switch statements

Let’s have a look at all the decision making statements along with its syntax one by one.

1. if statement

It executes the statement within its body if the stated boolean condition is true. In if statement, a single statement can be added without inserting 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 a 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 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 *

Shopping cart