In this article, we are going to understand the Flow Control Statements in C Language.
Any valid combination of constants, variables, and operators constitutes an expression. Some of the examples are:
x + y
x++
x + (y + z)
When an expression is followed by a semicolon (;) it becomes a simple statement. Below are a few examples of simple statements:
x = x + y;
x++;
A collection of statements surrounded by an opening brace { and a closing brace } form a compound statement or a block.
Below is an example for a block of statements:
{
int x, y = 17;
x = y * y * y;
printf("%d %d", x, y);
}
The variables declared inside a block are visible only inside that block and cannot be accessed outside that block. (It is also called visibility or scope of the variable)
C provides a special statement to control the execution of one or more statements depending on a condition. Such statements are called control statements or control-flow statements.
C Flow Control Statements:
The control-flow statements are of three types:
- Selection Statement – is a statement whose execution results in a choice being made as to which of two or more paths should be followed.
- if construct
- if-else construct
- if-else-if construct
- Nested if-else-if construct
- switch-case construct
- Iterative Statement – is a statement that executes a set of statements repeatedly depending on a condition.
- while loop
- do-while loop
- for loop
- Unconditional Control Statement – is a statement which transfers control-flow to some other section of the program without the need for a condition.
- break statement
- continue statement
- goto statement
if construct:
The general form of if statement is:
if (condition)
{
statement-1;
statement-2;
....
....
....
statement-n;
}
The if construct is a selective statement, the statements within the block are executed only once when the condition evaluates to true, otherwise the control goes to the first statement after the if construct.
If only one statement is presented in the if construct then there is no need to specify the braces {, } i.e., if braces are not specified for the if construct, by default the next immediate statement is the only statement considered for the if construct.
Let’s consider an example:
Below given code prints “Online Tutorials Point” only when the number is a multiple of 5:
#include<stdio.h>
int main() {
int a;
printf("Enter a number : ");
scanf("%d", &a);
if (a % 5 == 0) {
printf("Online Tutorials Point\n");
}
}
Output:
Enter a number : 500
Online Tutorials Point
...Program finished with exit code 0
In the above code, a % 5 == 0 is the condition, which verifies whether the number is a multiple of 5. Only if the condition returns 1 (true) when the control enters into the if – block and executes the statement.
if-else construct:
The if statement tells a program to execute a certain section of code only if a particular test evaluates to true.
if (expression) {statement}.
Below given code is an example of code that uses an if statement:
int a = 1500;
if(a > 1000) {
printf("Passed in the examination");
}
An if statement will execute its block only when the condition evaluates to 1 (true).
We can also conditionally execute another block when the condition evaluates to 0 (false) using the else construct. The else construct must be attached to an if, hence together they are referred to as an if-else construct.
The if-else statement provides two different paths of execution depending on the result of the condition.
Below is the general syntax for the if-else statement:
if (expression) {
statement-1;
} else {
statement-2;
}
Let’s consider an example:
int a = 1500;
if(a > 1000) {
printf("Passed in the examination.\n");
} else {
printf("Failed in the examination.\n");
}
In the above code, a > 1000 is the condition, which verifies whether the student Passed the examination. Only if the condition returns 1 (true) when the control enters into the if – block and executes the statement. Or else, it enters into the else-block and executes the statement.
Nested if-else construct:
When an if-else construct appears as a statement within another if-block or an else-block, it is referred to as nesting of an if-else construct.
Below is the general syntax for the nested if-else statement:
if (expression_1) {
if (expression_2) {
if (expression_3) {
statement_1;
} else {
statement_2;
}
} else {
statement_3;
}
}
In the above syntax, the statement_2 will be executed only when the conditions in expression_1, expression_2, and expression_3 evaluate to 1 (true).
Let’s consider an example:
The below-given code finds the largest integer among the given three integers.
#include<stdio.h>
int main() {
int p, q, r;
printf("Enter any three numbers : ");
scanf("%d%d%d", &p, &q, &r);
if (p > q) {
if (p > r) {
printf("The largest number is %d.\n", p);
} else {
printf("The largest number is %d.\n", r);
}
} else {
if(q > r) {
printf("The largest number is %d.\n", q);
} else {
printf("The largest number is %d.\n", r);
}
}
Output:
Enter any three numbers : 100
1000
512
The largest number is 1000.
...Program finished with exit code 0
if-else-if construct:
The if-else-if construct extends the if-else construct by allowing to chain multiple if constructs as shown below:
if (expression_1) {
statement_1;
} else if (expression_2) {
statement_2;
} else if (expression_3) {
statement_3;
} else if (expression_4) {
statement_4;
} else {
statement_5;
}
As shown in the above syntax, multiple if constructs can be chained to any length. The else construct which appears at the end is optional, and if it is to be included it has to be only at the end.
The if-else-if construct is used whenever we have multiple mutually exclusive if conditions that work on the same input.
In an if-else-if construct, the conditions are evaluated from top to bottom. Whenever a condition evaluates to true (1), the control enters into that if-block and after that, the control comes out of the complete if-else-if construct ignoring all the remaining if and else constructs that may exist below the currently satisfied if-block.
For example, if the condition in the expression_2 is the first condition to evaluate to true after executing statement_2 the control comes out of the complete if-else-if construct.
Let us consider a sample code given below which prints if the given character is an alphabet or a digit.
#include <stdio.h>
void main() {
char ch;
printf("Enter a character : ");
ch = getchar();
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
printf("Given character %c is an alphabet\n", ch);
}
if (ch >= '0' && ch <= '9') {
printf("Given character %c is a digit\n", ch);
}
}
In the above code, when an alphabet is passed as an input both the if-blocks are executed even though the conditions are mutually exclusive. Meaning, if the first if construct is satisfied then the given input is an alphabet, and the second if construct which verifies for it being a digit needs not to be evaluated.
In such cases, we should not write independent if-blocks as shown in the above code and instead use the if-else-if construct as shown in the below code.
#include <stdio.h>
void main() {
char ch;
printf("Enter a character : ");
ch = getchar();
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
printf("Given character %c is an alphabet\n", ch);
} else if (ch >= '0' && ch <= '9') {
printf("Given character %c is a digit\n", ch);
}
}
Let’s consider another example:
The given below code uses an if-else statement to check whether the given integer is even or odd.
Use if-else statement and print “__ is an even number”:
- if a year is divisible by 2, then it is an even number
Otherwise, print “__ is an odd number”.
#include<stdio.h>
void main() {
int num;
printf("Enter any number : ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is an even number.\n", num);
} else {
printf("%d is an odd number.\n", num);
}
}
References:
Happy Learning 🙂