Vision and dreams are the blueprints of soul and achievements.
-Mohammed Ahmed F

Introduction to Conditional Statements in C

Conditional Statements

IF Statements:

An if statement consists of a boolean expression followed by one or more statements.

Syntax:

if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}

IF....Else Statements:

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax:

if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}

If the boolean expression evaluates to true then the if block of code will be executed otherwise else block of code will be executed.

Nested If Statements:

Syntax:

if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
}

You can nest else if...else in the similar way as you have nested if statement.

Switch Statement:

Syntax:

switch(expression)
{
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}

Nested Switch Statements:

Syntax:

switch(ch1)
{
case 'A':
printf("This A is part of outer switch" );
switch(ch2)
{
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* case code */
}
break;
case 'B': /* case code */
}

Share this

Related Posts

Dear User,

Thank you for your comment. Hope you like this blog. Kindly share us on Social Media so others can be updated.

-Chief Administrative Officer.