Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Decision Control Statement in C

In the case of decision control statements in C language (nested if and if-else), a group of
available statements will get executed in case the conditions we have are true. The execution of
the else part statements won’t occur whenever these available conditions happen to be false.

The decision-making statements would require that the programmer handling the program
provides the program with the specification of single or multiple conditions that the program gets
to test or evaluate. It also needs to specify a statement or various other statements that will get
executed in case the determined condition is false.

Table of Contents

● Use Of Decision Control Statement In C


● Types Of Decision Control Statement In C
● Summary Of Decision Control Statement In C
● The If Statement In C Language
● Syntax
● Use
● Example
● The If-Else Statement In C Language
● Syntax
● Use
● Example
● The Nested If Statement In C Language
● Syntax
● Example
● Practice Problems On Decision Control Statement In C
● FAQs

Use of Decision Control Statement in C


In a programming language, we must perform various tasks based on the conditions available to
us. For instance, let us consider an online website. Whenever someone enters the wrong
password or ID, it will display an error page, while if they enter the correct credentials, then the
website will display them a welcome page. Thus, there might be some type of logic here that
checks the available conditions (password and ID). If the conditions return to be true, then it will
perform the assigned task (in this case, displaying the welcome page). Or else, it will perform a
very different task (in this place, displaying the error page).

When we use the decision control statements, we get to control the flow of any program with us
in such a way that it gets to execute some statements on the basis of an available condition’s
outcome (the condition can be both true or false).

Types of Decision Control Statement in C


We have three major types of decision control statements that we use in the C programming
language. These are:
● nested if statements
● if-else statements
● if statements

Summary of Decision Control Statement in C


In the table available below, we have the syntax for every decision control statement in C
language, along with its description:

Type of Syntax Description


Decision
Control
Statements in
C

if The Syntax is: The Description is:

if (condition x) In the case of such a statement, when the


available condition is true, there occurs an

{ Statements; } execution of a certain block of code.

if…else The Syntax is: The Description is:

if (condition x) In the case of such a statement, when the


available condition is true, then there occurs

{ Statement x; an execution of a certain group of

Statement y; } statements. In case this available condition


turns out to be false, there occurs an
execution of the statement specified in the
else
else part.

{ Statement a;
Statement b; }
nested if The Syntax is: The Description is:

if (condition x){ When the condition x is true, we get a


Statement x; } resultant statement x. When it is false, then
the program automatically checks for the

else_if(condition condition y. When condition y is true, there

y) occurs an execution of the statement y. When


the condition y also fails, then the final
execution of the else part occurs.
{ Statement y; }

else Statement Z;

The if Statement in C Language


The execution of the statements available inside an if block will only happen when the condition
defined by this statement turns out to be true. In case the available condition is false, the
compiler will skip the available statement that gets enclosed in the body of the if statement. One
can have as many numbers of if statements in a program (in C language) as we want.

Syntax:
The Syntax available for the if statement will be:

if (condition x)

// A block of statements for the C program

// The execution of these statements will only happen when this condition turns to be
true

There will be an execution of the statements available inside the body of the if block whenever
the given condition returns to be true. In case this condition returns to be false, then the program
will skip the statements that we have inside the if block.

Use:
This statement is one of the very simplest forms of decision control statements. We often use it
in the cases of simple decision-making cases in the C language.
Example:
Let us look at an example of a program that will ultimately print the highest number.

#include <stdio.h>

void main()

int x;

printf(“We enter any number:”);

scanf(“%d”, &x);

printf (“The number entered here is %d”, x);

if (x>100)

printf (“Hi. You have entered a comparatively higher number.”);

Here, in this program, the compiler will accept a number provided by the user as an input and will
generate an output accordingly. Here, we have also given the expression n>100. It means that if
the number given by the user is higher than the value of 100, then only we will get the message
as the output. Else, this statement will be skipped once and for all.

Thus,

Case #1:

If x = 300,

The output generated here will be:

Hi. You have entered a comparatively higher number.

Case #2:

If x = 50,

The compiler will skip the statement, “Hi. You have entered a comparatively higher number. ”

The if-else Statement in C Language


We have two blocks of statements in the case of this type of decision control statement- one if
the available condition is true and another one when this condition is false. When the statement
available in the if body results to be true, then the statement inside that block will surely execute.
In case it is false, then there will be an execution of the statement inside the else block. It means
that the else statement cannot exist in a program if there is no if statement.

Syntax:
The syntax available for the if-else statement will be:
if (condition x)

// The statements present inside the body of if

else {

// The statements present inside the body of else

When the statement present in the if condition returns true, then the program will execute the
statement that is present inside the body of this block. Here, the compiler will skip the statement
that is present inside the body if the else block.

Conversely, when the condition present inside the if block is false, then the compiler will skip the
block and will execute the statement present in the else block.

Use:
Various times when a user writes that if a condition/ expression is true, then the compiler must
take an action. Else, if that statement is false, then the compiler will take a different action. Thus,
we can include an action for either of the conditions or include just a single statement for the
true condition and no action for any other condition. We use the if-else statement here.

Example:
Let us look at an example of a program that we use in case of a leap year.

#include <stdio.h>

void main()

int year;

printf(“We will enter a year here:”);

scanf(“%d”, &yr);

if ((year%4 == 0) && ((year%100 !=0) || (year%400==0)))

printf(“Yes. The provided year is a leap year. Great job…!!!!”);

else

printf (“No. The provided year is not a leap year. Try again….!!!”);

Case #1:

year = 1893
The output generated here will be:

We enter a year: 1893

No. The provided year is not a leap year….!!!

Case #2:

year = 1564

The output generated here will be:

We enter a year: 1564

Yes. The provided year is a leap year. Great job…!!!!

The nested if Statement in C Language


The nested if statement is fairly useful in case a user needs to evaluate multiple types of
conditions. Here, the if block is present to define a condition (or an expression). The resultant
statement will get executed on the basis of the result of this expression (when the condition is
true or false).

Syntax:
The syntax available for the nested if statement will be:

if (condition x)

{ Statement x; }

// The C Statement for this condition

else if(condition y)

// The C Statement for this condition

{ Statement y; }

else

Statement z;

default:

// The C Statement for this default condition;

When the condition available in the if block is true, there will be an execution of the provided
statement. When it is false, the program will check the next condition. If it is true, the execution
of the statement associated with this condition will occur. In case this one is also false, then
there will be a default execution of a final statement. Here, we may have as many types of cases
as possible. However, there must be just one type of default statement.
Example:
Let us look at an example of a program that we use for finding out about two variables that might
be greater than or smaller than each other.

Case #1:

#include <stdio.h>

int main()

int a=40,b=20;

if (a>b) {

printf(“The number a is greater than the number b”);

else if(a<n) {

printf(“The number a is less than the number b”);

else {

printf(“The number a is equal to the number b”);

The output generated here would be:

The number a is greater than the number b

Case #2:

#include <stdio.h>

int main()

int a=20,b=40;

if (a>b) {

printf(“The number a is greater than the number b”);

else if(a<n) {

printf(“The number a is less than the number b”);

}
else {

printf(“The number a is equal to the number b”);

The output generated here would be:

The number a is less than the number b

Case #3:

#include <stdio.h>

int main()

int a=30,b=30;

if (a>b) {

printf(“The number a is greater than the number b”);

else if(a<n) {

printf(“The number a is less than the number b”);

else {

printf(“The number a is equal to the number b”);

The output generated here would be:

The number a is equal to the number b

Practice Problems on Decision Control Statement in C


1. Take a look at the following program:

int main()

int p=50, q=50;

if (p == q)

{
printf(“The number p and the number q are equal.”);

Output:

The number p and the number q are equal.

Is the output:

A. True

B. False

C. Both. It depends.

Answer – A. True

2. What would be the output of the following program?

#include <stdio.h>

int main()

int p=80, q=120;

if (p>q) {

printf(“The number p is greater than the number q”);

else if(p<n) {

printf(“The number p is less than the number q”);

else {

printf(“The number p is equal to the number q”);

A. The number p is greater than the number q

B. The number p is less than the number q

C. The number p is equal to the number q

D. Compile time error

Answer – B. The number p is less than the number q

3. Take a look at the following program:


#include <stdio.h>

int main()

int age;

printf(“Please enter your age here:”);

scanf(“%d”,&age);

if(age >=18)

printf(“Okay. You are not eligible for voting today.”);

else

printf(“Sorry. You are still not eligible for voting today.”);

return 0;

Look at the pair of inputs and outputs. Which of these are true?

1. Please enter your age here: 12

Sorry. You are still not eligible for voting today.

2. Please enter your age here: 22

Okay. You are not eligible for voting today.

3. Please enter your age here: 35

Okay. You are not eligible for voting today.

4. Please enter your age here: 09

Sorry. You are still not eligible for voting today.

A. 1, 2, and 3

B. 3, 4

C. 1, 2, and 4

D. 1, 2, 3, and 4

Answer – D. 1, 2, 3, and 4
FAQs

What is the difference between the if statement and the if-else


statement?

The if statement is one of the very simplest forms of decision control statements. We often use
it in the cases of simple decision-making cases in the C language.

Syntax:

if (condition x)

{ Statements; }

The Description is:

In the case of such a statement, when the available condition is true, there occurs an execution
of a certain block of code.

if…else

Various times when a user writes that if a condition/ expression is true, then the compiler must
take an action. Else, if that statement is false, then the compiler will take a different action. Thus,
we can include an action for either of the conditions or include just a single statement for the
true condition and no action for any other condition. We use the if-else statement here.

Syntax:

if (condition x)

{ Statement x; Statement y; }
else

{ Statement a; Statement b; }

The Description is:

In the case of such a statement, when the available condition is true, then there occurs an
execution of a certain group of statements. In case this available condition turns out to be false,
there occurs an execution of the statement specified in the else part.

You might also like