Control Flow: Statements and Blocks
Control statements are used to control the “Flow of
Control”.
It specifies the order in which the various
instructions/statements in a program should be
executed.
A statement is the smallest unit of execution in C.
Each statement ends with a semicolon (;).
Statements tell the compiler what action to perform
2 Types :
1 : Decision Making Control Statement : It allows
the computer to take a decision as to which
statement is to be executed next based on certain
conditions.
2 : Loop Control Statement : These are used to
execute a group of statements repeatedly.
1 : Decision Making Control Statements
or selection control statements
It allow a program to choose different paths
of execution depending on conditions
(true/false).
They control the flow of the program based on
logical decisions.
Types of Decision-Making Statements in C
1 : if statement
2 : if…else statement
3 : nested if statement
4 : else if ladder
5 : switch statement
1 : if statement
When the condition of if is true, The
statements present within the if block are
executed.
Otherwise if block is skipped by transferring
the control directly to the first statement after
the if block.
Executes a block of code only if the condition is
true.
1. Single Statement if
Syntax :
if (condition)
statement;
Example :
if (x > 0)
printf("Positive number");
2. Block (Multiple Statements) if
For multiple statements called as Compound Statements
Syntax :
if (condition) {
statement1;
statement2;
...
}
Example :
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive.\n");
printf("You entered: %d\n", num);
printf("Good job!\n");
}
Rules to be followed regarding if statement
1 : Condition must be inside
parentheses ().
2 : Condition should be a valid
logical/relational expression.
3 : No semicolon right after if(condition).
4 : Single statement doesn’t need { }
braces.
5 : Multiple statements must be enclosed
in { } braces.
Flowchart of if condition
2 : If else statement
It allows the program to execute one
block of code if a condition is true,
and another block of code if the condition
is false.
Syntax :
if (condition) {
// Block of statements if condition is TRUE
}
else {
// Block of statements if condition is FALSE
}
Syntax for multiple statements
if (condition) {
// Statement 1
// Statement 2
// Statement 3
}
else {
// Statement A
// Statement B
// Statement C
}
Example :
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 40) {
printf("Congratulations!\n");
printf("You have passed the exam.\n");
printf("Keep up the good work!\n");
}
else {
printf("Sorry!\n");
printf("You have failed the exam.\n");
printf("Better luck next time.\n");
}
Flowchart for if else
condition
3 : Nested If Statement
A nested if is an if statement placed inside another if or else
if.
It is used when you need to test multiple conditions
hierarchically (one inside another).
Syntax :
1 : Simple Nested if
if (condition1) {
if (condition2) {
// executes when condition1 AND condition2 are true
}
}
Example :
if (age >= 18) {
if (marks >= 50) {
printf("Eligible\n");
}
}
2 : Nested If Else Statement
Here, the inner if can also have
its own else.
Syntax : Example :
if (num > 0) {
if (condition1) { if (num % 2 == 0)
if (condition2) { printf("Positive
// executes if both
Even\n");
conditions are true
} else
else { printf("Positive
// executes if condition1 Odd\n");
true but condition2 false
}
}
}
else {
else { printf("Not
// executes if condition1 is Positive\n");
false }
}
3. Multiple Nested ifs (chain inside chain)
You can have multiple inner ifs inside a
block.
Syntax :
if (condition1) {
if (condition2) {
if (condition3) {
// executes if all three are
true
}
}
}
Example :
if (a > 0) {
if (b > 0) {
if (c > 0) {
printf("All numbers are positive\n");
}
else {
printf("a and b are positive, but c is not\n");
}
}
else {
printf("a is positive, but b is not\n");
}
}
else {
printf("a is not positive\n");
}
4. Nested if with else-if
ladder
Mixing nested if and else if ladder for multiple choices.
Syntax :
if (condition1)
{
if (condition2)
{
statement1;
}
else if (condition3)
{
statement2;
}
else {
statement3;
}
}
else {
statement4;
}
Example :
if (marks >= 40) {
if (marks >= 75) {
printf("Distinction\n");
}
else if (marks >= 60)
{
printf("First Class\n");
}
else
printf("Pass\n");
}
else {
printf("Fail\n");
}
Flow chart of Nesting of If Else
Statements
4 : Else if ladder
The else–if ladder is a decision-making
structure in C .
It is used when we need to check
multiple conditions one after another .
As soon as one condition is true, its
block executes and the rest are
skipped .
If none of the conditions are true, the
final else (if present) executes.
Syntax :
if (condition1) {
// block if condition1 is true
}
else if (condition2) {
// block if condition2 is true
}
else if (condition3) {
// block if condition3 is true
}
else {
// block if none of the above conditions are true
}
Example :
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade: A\n");
}
else if (marks >= 75) {
printf("Grade: B\n");
}
else if (marks >= 60) {
printf("Grade: C\n");
}
else if (marks >= 40) {
printf("Grade: D\n");
}
else {
printf("Grade: F (Fail)\n");
}
Flow Chart of else-if ladder
5 : Switch
The switch statement is a
decision-making structure like if–
else .
It is used when we want to
compare a variable/expression
with multiple constant values .
Each value is called a case .
The switch is often cleaner than
using a long else–if ladder.
Syntax :
switch (expression)
{
case constant1:
// code block
break;
case constant2:
// code block
break;
case constant3:
// code block
break;
default:
// code block if no case matches
}
Important Points expression
Inside switch must be an integer or
character type (not float, not string).
Each case value must be unique constant
(no variables).
break; is used to exit the switch after a
match .
If we don’t use break, execution
continues into the next case (fall-
through).
default is optional, but good practice.
Day of the Week program using switch
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1–7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Sunday\n");
break;
case 2: printf("Monday\n");
break;
case 3: printf("Tuesday\n");
break;
case 4: printf("Wednesday\n");
break;
case 5: printf("Thursday\n");
break;
case 6: printf("Friday\n");
break;
case 7: printf("Saturday\n");
break;
default: printf("Invalid day number\n");
}
return 0;
}
Simple Calculator using Switch
#include <stdio.h>
int main() {
int a, b, choice;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Choose operation: \n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Result = %d\n", a + b);
break;
case 2: printf("Result = %d\n", a - b);
break;
case 3: printf("Result = %d\n", a * b);
break;
case 4:
if (b != 0)
printf("Result = %d\n", a / b);
else
printf("Division by zero not allowed\n");
break;
default: printf("Invalid choice\n");
}
return 0;
}
Flow Chart of Switch