Conditional Flow Control structures in C

Conditional Flow Control Structures in C

There may be a situation where we have to choose one of the alternative paths depending upon the result of some condition. Condition is an expression evaluating to true or false. This is know as Decision-making statement.
There are different forms of decision making control structure:

  • if
  • if – else
  • Nested-if
  • Switch 

[1] if statement
if statement is the most basic decision making statement. If the condition is true then the next statement or the statements inside the parenthesis are executed.

Syntax

if (condition)
{
statement;
}

Conditional Flow Control Structures in C

Example
if(n>0)

printf(“Number is positive”);

[2] if-else statement

If the condition is true then the next statement or the statements inside the parenthesis are executed or the else part is executed.

Syntax

if (condition)
{
statement;
}
else
{
statement;
}

Example
if(n>0)

printf(“Number is positive”);
else
printf(“Number is negative”);

[3] Nested if statement

Nested if statements mean an if statement inside another if statement.

Syntax

if (condition)
{
if (condition)
{statement;}
else{ statement;}
}
else
{
if (condition)
{statement;}
else{ statement;}
}

 

Example
if (a != b)

{
printf(“a is not equal to b\n”);
//Nested if else
if (a > b)
{
printf(“a is greater than b\n”);
}
else
{
printf(“b is greater than a\n”);
}
}
else
{
printf(“a is equal to b\n”);
}

[4] Switch statement

This control statement allows us to make a decision from the number of choices is called as Switch case statement. It is a multi-way decision making statement.

Syntax

switch(expression)
{
case value1: block1; break;
case value1: block1; break;
case value1: block1; break;
.
.
.
default: default statement;
}

 

Example
switch(ch)
{
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’:
printf(“Vowel”);
break;
default:
printf(“Consonant”);
}

If you like the post Conditional Flow Control Structures in C, please share your feedback!

also see

C Programming language
Go Programming language
Linked List Array
Simplification Queue
DBMS Reasoning
Aptitude HTML
Previous articleSplit nodes of a linked list into two halves
Next articleLoop Control Structures in C

LEAVE A REPLY

Please enter your comment!
Please enter your name here