Unit 3 Control Flow
CONTROL FLOW
The conditional statements (also known as decision control
structures) such as if, if else, switch, etc. are used for decision-
making purposes in C programs.
They are also known as Decision-Making Statements and are used to
evaluate one or more conditions and make the decision whether to
execute a set of statements or not. These decision-making
statements in programming languages decide the direction of the
flow of program execution.
Need of Conditional Statements
There come situations in real life when we need to make some
decisions and based on these decisions, we decide what should we
do next. Similar situations arise in programming also where we need
to make some decisions and based on these decisions we will
execute the next block of code. For example, in C if x occurs then
execute y else execute z. There can also be multiple conditions like in
C if x occurs then execute p, else if condition y occurs execute q, else
execute r. This condition of C else-if is one of the many ways of
importing multiple conditions.
1. if in C
The if statement is the most simple decision-making statement. It is
used to decide whether a certain statement or block of statements
will be executed or not i.e if a certain condition is true then a block of
statements is executed otherwise not.
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
,Unit 3 Control Flow
}
Here, the condition after evaluation will be either true or false. C if
statement accepts boolean values – if the value is true then it will
execute the block of statements below it otherwise not. If we do not
provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default
if statement will consider the first immediately below statement to
be inside its block.
Flowchart of if Statement
Flow Diagram of if Statement
Example of if in C
C
// C program to illustrate If statement
#include <stdio.h>
,Unit 3 Control Flow
int main()
{
int i = 10;
if (i > 15) {
printf("10 is greater than 15");
}
printf("I am Not in if");
}
Output
I am Not in if
2. if-else in C
The if statement alone tells us that if a condition is true it will
execute a block of statements and if the condition is false it won’t.
But what if we want to do something else when the condition is
false? Here comes the C else statement. We can use
the else statement with the if statement to execute a block of code
when the condition is false. The if-else statement consists of two
blocks, one for false expression and one for true expression.
Syntax of if else in C
if (condition)
{
// Executes this block if
// condition is true
}
else
{
, Unit 3 Control Flow
// Executes this block if
// condition is false
}
Flowchart of if-else Statement
Example of if-else
// C program to illustrate If statement
#include <stdio.h>
int main()
{
int i = 20;
CONTROL FLOW
The conditional statements (also known as decision control
structures) such as if, if else, switch, etc. are used for decision-
making purposes in C programs.
They are also known as Decision-Making Statements and are used to
evaluate one or more conditions and make the decision whether to
execute a set of statements or not. These decision-making
statements in programming languages decide the direction of the
flow of program execution.
Need of Conditional Statements
There come situations in real life when we need to make some
decisions and based on these decisions, we decide what should we
do next. Similar situations arise in programming also where we need
to make some decisions and based on these decisions we will
execute the next block of code. For example, in C if x occurs then
execute y else execute z. There can also be multiple conditions like in
C if x occurs then execute p, else if condition y occurs execute q, else
execute r. This condition of C else-if is one of the many ways of
importing multiple conditions.
1. if in C
The if statement is the most simple decision-making statement. It is
used to decide whether a certain statement or block of statements
will be executed or not i.e if a certain condition is true then a block of
statements is executed otherwise not.
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
,Unit 3 Control Flow
}
Here, the condition after evaluation will be either true or false. C if
statement accepts boolean values – if the value is true then it will
execute the block of statements below it otherwise not. If we do not
provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default
if statement will consider the first immediately below statement to
be inside its block.
Flowchart of if Statement
Flow Diagram of if Statement
Example of if in C
C
// C program to illustrate If statement
#include <stdio.h>
,Unit 3 Control Flow
int main()
{
int i = 10;
if (i > 15) {
printf("10 is greater than 15");
}
printf("I am Not in if");
}
Output
I am Not in if
2. if-else in C
The if statement alone tells us that if a condition is true it will
execute a block of statements and if the condition is false it won’t.
But what if we want to do something else when the condition is
false? Here comes the C else statement. We can use
the else statement with the if statement to execute a block of code
when the condition is false. The if-else statement consists of two
blocks, one for false expression and one for true expression.
Syntax of if else in C
if (condition)
{
// Executes this block if
// condition is true
}
else
{
, Unit 3 Control Flow
// Executes this block if
// condition is false
}
Flowchart of if-else Statement
Example of if-else
// C program to illustrate If statement
#include <stdio.h>
int main()
{
int i = 20;