The Decision Control Structure ( if statement and if else statement)

EmbLogic RCD Labs
2 min readMay 8, 2020

We all need to alter our actions I the face of changing circumstances. If the weather is fine, then I will go for a stroll. If the pathway is busy, I would take a diversion. If she says no, I would look elsewhere. You can notice that all these decisions depend on some condition being met.

C language too must be able to perform different sets of actions on the circumstances. In fact, this is what makes it worth its salt. C has three major decision making instructions — The If statement, the if-else statement, and the switch statement.

The if Statement

Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:

if ( this condition is true )

execute this statement ;

The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true, then the statement is not executed; instead the program skip past it.

More generally, we can say that the syntax of an if statement is;

if ( expression )

statement

where expression is any expression and statement is any statement.

Example program for if statement in C :

In “if” control statement , respective block of code is executed where condition is true.

#include<stdio.h>

int main ( )

{

int m = 40, n = 40 ;

if ( m==n)

{

printf (“m and n are equal”) ;

}

}

Output :

m and n are equal

The if — else Statement

The if statement by itself will execute a single statement, or a group of statements , when the expression following if evaluates to true. It does nothing when the expression evaluate to false. Can we execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false? Of course! This is what is the purpose of the else statement that is demonstrated I the following example:

if ( n > 0 )

average = sum / n ;

else

{

printf (“ can’t compute average\n”) ;

average = 0 ;

}

The first statement on block of statements is executed if condition is true, and the second statement or block of statement ( following the keyword else ) is executed if the condition is not true. In this example, we can compute a meaningful average only if n is greater than 0 ; otherwise , we print a message saying that we cannot compute the average. The general syntax of an if statement is therefore

if ( expression )

statement <sub>1</ sub>

else

statement<sub>2</ sub>

( where both statement <sub>1</ sub> and statement <sub>2</ sub> may be lists of statement enclosed in braces ).

--

--

EmbLogic RCD Labs
0 Followers

EmbLogicTM is a technology design house and professional training company, providing research, competency development and customised training solutions.