Conditional Branching in C - Part 1-1
Conditional Branching in C - Part 1-1
CONTROL STATEMENTS IN C
Such statements by which we determine the flow of a program are called Control Statements or
Decision control statements. In any programming language, various tasks need to be done
depending on the condition and to do such tasks we use Control Statements or Decision-making
statements. Control Statements help us control the flow of the program. Through Control
Statements we determine how the program will move from one statement to another.
In C Programming language we have different types of decision control statements.
If statements
Loop Statements
Switch statement
Conditional Operator Statement
Goto Statement
if statements
There are the following variants of if statement in C language.
if statement
if-else statement
if else-if ladder
Nested if
*if statement
The if statement is used to check some given condition and perform some operations
depending upon the correctness of that condition. It is mostly used in the scenario where we
need to perform the different operations for the different conditions. The syntax of the if
statement is given below.
if(expression){
//code to be executed
}
Example:
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}
*if else Statement
In this decision control statement, we have two blocks of statements. One block is inside if
and the other block is inside else.
If, If condition is true, then the statement inside the if block is executed, otherwise the
statement inside the else block is executed.
Syntax
if (Condition)
{
True block of statements
}
Else
{
False block of statements
}
Example:
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}
*else if ladder
else if ladder control statement is also like if-else statement but there is a slight difference in
it that inside else block one or more if-else statement conditions are present.
Syntax
if (condition1)
{
// These statements would execute if the condition1 is true
}
else if (condition2)
{
// These statements would execute if the condition2 is true
}
else if (condition3)
{
// These statements would execute if the condition3 is true
}
.
.
else
{
*Nested if else
When inside the if statement or else statement one or more if else statement, then it is called
Nested
Syntax
if (condition) {
// Nested if else inside the body of "if"
if (condition2) {
// Statements inside the body of nested "if"
}
else {
// Statements inside the body of nested "else"
}
}
else {
// Statements inside the body of "else"
}
Example:
#include <stdio.h>
int main() {
// variable to store the given number
int n;
//take input from the user
scanf("%d", &n);
//if else condition to check whether the number is even or odd
if (n % 2 == 0) {
//the number is even
printf("Even ");
//nested if else condition to check if n is divisible by 4 or not
if (n % 4 == 0) {
//the number is divisible by 4
printf("and divisible by 4");
} else {
//the number is not divisible by 4
printf("and not divisible by 4");
}
} else {
//the number is odd
printf("Odd ");
//nested if else condition to check if n is divisible by 3 or not
if (n % 3 == 0) {
//the number is divisible by 3
printf("and divisible by 3");
} else {
//the number is not divisible by 3
printf("and not divisible by 3");
}
}
return 0;
}
*Switch statement
When we have a lot of conditions and we have to run a statement matching that condition, then
we use the switch case statement. With the help of Switch Case Statement We can do all the
work done by if-else
Syntax
Switch (expression)
{
Case label1:
Statement (S);
Break;
Case label2:
Statement (S);
Break;
Case label3;
Statement (s);
Break;
….
Case labelN:
Statement (s);
Break;
Default:
Statement (s);
}
Example:
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
Comparison Between switch and if-else:
A switch statement is usually more efficient than a set of nested ifs. Deciding whether to use
if-then-else statements or a switch statement is based on readability and the expression that the
statement is testing.
1. Check the Testing Expression: An if-then-else statement can test expressions based on
ranges of values or conditions, whereas a switch statement tests expressions based only on
a single integer, enumerated value, or String object.
2. Switch better for Multi way branching: When compiler compiles a switch statement, it
will inspect each of the case constants and create a “jump table” that it will use for
selecting the path of execution depending on the value of the expression. Therefore, if we
need to select among a large group of values, a switch statement will run much faster than
the equivalent logic coded using a sequence of if-else. The compiler can do this because it
knows that the case constants are all the same type and simply must be compared for
equality with the switch expression, while in case of if expressions, the compiler has no
such knowledge.
3. if- else better for Boolean values: If-else conditional branches are great for variable
conditions that result into a Boolean, whereas switch statements are great for fixed data
values.
4. Speed: A switch statement might prove to be faster than ifs provided number of cases is
good. If there are only few cases, it might not affect the speed in any case. Prefer switch if
the number of cases are more than 5 otherwise, you may use if-else too. If a switch
contains more than five items, it’s implemented using a lookup table or a hash list. This
means that all items get the same access time, compared to a list of if’s where the last item
takes much more time to reach as it has to evaluate every previous condition first.
5. Clarity in readability: A switch looks much cleaner when you have to combine cases. Ifs
are quite vulnerable to errors too. Missing an else statement can land you up in havoc.
Adding/removing labels is also easier with a switch and makes your code significantly
easier to change and maintain.
Definition Depending on the condition in the 'if' The user will decide which statement is to
statement, 'if' and 'else' blocks are be executed.
executed.
Expression It contains either logical or equality It contains a single expression which can be
expression. either a character or integer variable.
Evaluation It evaluates all types of data, such as It evaluates either an integer, or character.
integer, floating-point, character or
Boolean.
Sequence of First, the condition is checked. If the It executes one case after another till the
execution condition is true then 'if' block is break keyword is not found, or the default
executed otherwise 'else' block statement is executed.
Default If the condition is not true, then by If the value does not match with any case,
execution default, else block will be executed. then by default, default statement is
executed.
Editing Editing is not easy in the 'if-else' Cases in a switch statement are easy to
statement. maintain and modify. Therefore, we can say
that the removal or editing of any case will
not interrupt the execution of other cases.
Speed If there are multiple choices If we have multiple choices then the switch
implemented through 'if-else', then the statement is the best option as the speed of
speed of the execution will be slow. the execution will be much higher than 'if-
else'.