Professional Documents
Culture Documents
Decision Control Statement in C
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
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).
{ Statement a;
Statement b; }
nested if The Syntax is: The Description is:
else Statement Z;
Syntax:
The Syntax available for the if statement will be:
if (condition x)
// 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;
scanf(“%d”, &x);
if (x>100)
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,
Case #2:
If x = 50,
The compiler will skip the statement, “Hi. You have entered a comparatively higher number. ”
Syntax:
The syntax available for the if-else statement will be:
if (condition x)
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;
scanf(“%d”, &yr);
else
printf (“No. The provided year is not a leap year. Try again….!!!”);
Case #1:
year = 1893
The output generated here will be:
Case #2:
year = 1564
Syntax:
The syntax available for the nested if statement will be:
if (condition x)
{ Statement x; }
else if(condition y)
{ Statement y; }
else
Statement z;
default:
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) {
else if(a<n) {
else {
Case #2:
#include <stdio.h>
int main()
int a=20,b=40;
if (a>b) {
else if(a<n) {
}
else {
Case #3:
#include <stdio.h>
int main()
int a=30,b=30;
if (a>b) {
else if(a<n) {
else {
int main()
if (p == q)
{
printf(“The number p and the number q are equal.”);
Output:
Is the output:
A. True
B. False
C. Both. It depends.
Answer – A. True
#include <stdio.h>
int main()
if (p>q) {
else if(p<n) {
else {
int main()
int age;
scanf(“%d”,&age);
if(age >=18)
else
return 0;
Look at the pair of inputs and outputs. Which of these are true?
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
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; }
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; }
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.