Conditional Statements – Decision Statements – if, else

Conditional Statements in C

C programs are executed in a sequence, but we can control the execution of program by using any control mechanism by which we can compare things and come to a decision. This involves using some operations called Relational Operators and conditional statements called if-else and loops. We use fundamental operators to compare two values in our C programs. The relational operators compare two values of any basic data type and return true or false after comparison.

These relational operators compare two values and return true or false after comparison. We can use the relational operators to compare values of any basic data type, so all we need is to modify the behavior of the program.

In C, there is another operator called ternary operator which is an expression instead of a statement i.e. you can have it on the right-hand side (RHS) of a statement. So you can write certain code statements more concisely.

The rest of the article is divided into the following main sections:

The if Statement

The if statement by itself will execute a single statement or a group of statements when the condition following if is true.

If the condition is false then a group of statements can be executed using else statement

The simple example of an if statement is:

if (varName == 20)
  printf ("Value of the variable is 20");

We can also use the code block to specify the statements to be pre-executed if the given condition is true i.e.

if (varName == 20){
   printf ("Value of the variable is 20");
   printf ("Print what ever you want!!!");
}

The general form of if statement looks like this:

if (this condition is true)
execute this statement;

Here 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 true, then the statement is executed. It the condition is not true then the statement is not executed instead the program skips past it. The condition in C is evaluated using C’s relational operators. The relational operators help us to build expression which are either true or false

This expression is true if

  X == Y X is equal to Y
  X != Y X is not equal to Y
  X  > Y X is greater than Y
  X <= Y X is less than or equal to Y
  X >= Y X is greater than or equal to Y

Demonstration of if statement in C

#include <stdio.h>

int main( )
{
 int num;
 printf("Enter a number less than 10 \n");
 scanf("%d", &num);
 
 if(num <= 10){
     printf("The number is less than 10 \n");
 }
}

Multiple statements within an if statement

If it is desired that more than one statement is to be executed if the condition is satisfied, then such statements must be placed within pair of braces. for e.g.

The following program demonstrate that if year of service greater than 3 then a bonus of Rs. 2500 is given to employee. The program illustrates the multiple statements used within if

#include <stdio.h>

int main( )
{
 int bonus, cy, yoj, yos;
 yos = 0;
 cy = 2018;
 yoj = 2013;

 yos = cy - yoj;
 if(yos > 3)
 {
  bonus = 2500;
  printf("Bonus = Rs. %d", bonus);
 }
}

The if-else Statement

The if statement by itself will execute a single statement or a group of statements when the condition following if is true. it does nothing when the condition is false. It the condition is false then a group of statements can be executed using else statement. The following program illustrates this

#include <stdio.h>

int main( )
{
 float bs, gs, da, hra;
 bs = 23000;
 
 if(bs < 1500){
  hra = bs * 10 / 100;
  da = bs * 90 / 100;
 }
 else{
  hra = 500;
  da = bs * 98 / 100;
 }
 
 gs = bs+hra+da;
 printf("gross salary = USD. %f ", gs);
}

Nested if – else statement

It we write an entire if – else construct within the body of the if statement or the body of an else statement. This is called ?nesting? of if. For e.g.

if (condition)
{
if (condition)
 {
  do this;
 else
 {
  do this;
  and this;
 }
}
else
 do this;

What are the examples of Conditional Operators?

Condition operators are listed here. You can read more about conditional operators in our guide on Operators in C.

  1. < Less than
  2. > Greater than
  3. == Equal to
  4. <= Less than or equal to
  5. >= Greater than or equal to
  6. != Not equal to

If you want to further understand conditional statements,  then study the following C Programs:

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post