Unit II – Control Structures
Programming in C
Decision Making – if Statement
Definition
The if statement is a conditional control statement that executes a block
of code only if the condition evaluates to true (non-zero).
If the condition is false (zero), the block is skipped.
Syntax of if statement
if (condition) {
// statements to be executed if condition is true//
}
Flow: Condition checked → if true → execute → end.
Example: if (a > b)
{
printf("a is greater");
}
Example program
#include <stdio.h>
int main()
{
int age = 20;
if (age >= 18)
{
printf("You are eligible to vote.\n");
}
return 0;
}
Output:
You are eligible to vote.
Rules for if statement
Condition must be written inside parentheses ( ).
Condition must return true (non-zero) or false (zero).
Curly braces { } are optional if only one statement is inside if.
Nested if (inside another if)
Nested if (inside another if)
The if statement can be placed inside another if for multiple conditions.
#include <stdio.h>
int main() {
int num = 25;
if (num > 0) {
if (num % 2 == 1) {
printf("The number is positive and odd.\n");
}
}
return 0;
}
Output:
The number is positive and odd
Applications of if Statement
• Checking eligibility (age, marks, etc.).
• Validating user input.
Decision Making in C – The if-else
Statement
Definition
The if-else statement is a conditional control statement that allows us to choose one of
two possible actions:
If the condition is true → execute the first block.
If the condition is false → execute the second block.
Syntax of if-else statement
if (condition) {
// statements executed if condition is true
} else {
// statements executed if condition is false
}
Flowchart of if-else
Condition
/ \
True False
/ \
[Statements1] [Statements2]
\ /
Checking Even or Odd
Example 1: Checking Even or Odd
#include <stdio.h>
int main () {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The number is Even.\n");
} else {
printf("The number is Odd.\n");
}
return 0;
}
Sample Output:
Enter a number: 15
The number is Odd.
Applications of if-else
• Checking eligibility (vote, exam, job).
• Simple comparisons (greater number, login validation).
• Binary decision-making in programs.
Decision Making in C – else-if Ladder
Definition
The else-if ladder is a chain of if-else statements where conditions
are checked sequentially from top to bottom until one condition
is true.
If a condition is true → its block executes and the rest are skipped.
If none are true → the final else executes.
Syntax
if (condition1) {
// statements for condition1
}
else if (condition2) {
// statements for condition2
}
else if (condition3) {
// statements for condition3
}
else {
// statements if none of the above conditions are true
Flowchart
Condition1?
/ \
Yes No
[Block1] Condition2?
/ \
Yes No
[Block2] Condition3?
/ \
Yes No
[Block3] [Else Block]
Example 1: Grading System
#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade: A\n");
}
else if (marks >= 75) {
printf("Grade: B\n");
}
else if (marks >= 50) {
printf("Grade: C\n");
}
else {
printf("Grade: Fail\n");
}
return 0;
}
Sample Output:
Enter marks: 78
Key Features of else-if ladder
• Conditions are checked in order from top to bottom.
• Only the first true condition executes, rest are skipped.
• The else part is optional, but usually included as default case.
• Used when there are multiple possible outcomes.
Applications of else-if ladder
• Grading systems.
• Menu-driven programs.
• Checking ranges of values (salary, marks, age).
• Decision-based classifications.
Decision Making in C – Nested if
Definition:
A nested if is an if statement inside another if (or else) block. It allows
testing of multiple conditions in a hierarchical manner.
Syntax of Nested if
if (condition1) {
if (condition2) {
// executes when both condition1 and condition2 are true
}
else {
// executes when condition1 is true but condition2 is false
}
}
else {
// executes when condition1 is false
}
Flowchart
Condition1?
/ \
Yes No
| |
Condition2? [Else Block]
/ \
Yes No
| |
[Block] [Else Block2]
Student Result
#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 50) {
if (marks >= 90) {
printf("Distinction\n");
} else {
printf("Pass\n");
}
} else {
printf("Fail\n");
}
return 0;
}
Sample Output:
Enter marks: 95
Distinction
Key Features of Nested if
• Multiple decisions can be handled step by step.
• Inner if is executed only if outer if is true.
• Can become complex and hard to read if too many are nested.
Applications
• Student result analysis (Pass/Fail/Distinction).
• Bank ATM (check PIN first → then balance
Decision Making in C – switch Statement
Definition
The switch statement is a multi-way decision-making statement that
executes one block of code among many possible options based on the
value of an expression.
Syntax of switch
switch (expression) {
case constant1:
// statements
break;
case constant2:
// statements
break;
...
default:
// statements if no case matches
}
Rules of switch
• Expression inside switch must be an integer, character, or constant
expression.
• Each case must have a unique constant value.
• break is used to exit the switch after executing a case.
• If break is omitted, fall-through occurs (next cases execute until a break).
• default is optional but recommended as a fallback.
Flowchart of switch
switch(expression)
|
+--------+---------+
| | |
case1 case2 caseN
| | |
[Statements][Statements]...
| | |
break break break
|
[default block if no match]
Example 1: Calculator
#include <stdio.h>
int main() {
int a, b, choice;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Sum = %d\n", a + b); break;
case 2: printf("Difference = %d\n", a - b); break;
case 3: printf("Product = %d\n", a * b); break;
case 4: printf("Quotient = %d\n", a / b); break;
default: printf("Invalid choice!\n");
}
return 0;
}
Sample Output:
Enter two numbers: 10 5
1. Addition
2. Subtraction
3. Multiplication
4. Division
Advantages of switch over if-else
• More readable for multiple choices.
• Execution is faster in some cases (as it uses jump table internally).
• Easy to maintain and debug.
Limitations of switch
• Expression must be integer or character (no strings, floats, doubles).
• Cases must be constant values, not variables or ranges.
• Not suitable for complex conditions (use if-else ladder instead).
Applications of switch
• Menu-driven programs (ATM, calculator).
• Selecting options in a program.
• Day/month classification.
Looping in C – while Loop
Definition
The while loop executes a block of statements repeatedly as long as the given
condition is true.
The condition is checked before each iteration.
If condition is false at the beginning, the loop body is skipped entirely.
Syntax of while loop
while (condition) {
// statements to be repeated
}
Flowchart of while Loop
Condition?
|
True
|
[Loop Body Statements]
|
Repeat
|
False
|
Exit
Working
• Condition is evaluated first.
• If condition is true → loop body executes.
• After execution, condition is checked again.
• This continues until the condition becomes false.
• If condition is false initially, loop body is not executed even once.
Example 1: Print Numbers from 1 to 5
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Output:
1
2
3
4
5
Key Features of while Loop
• Entry-controlled loop (condition is checked first).
• May execute zero times if condition is false initially.
• Useful when the number of iterations is not fixed in advance.
Applications
• Input validation (keep asking until correct input is given).
• Repetitive calculations (sum, factorial).
• Iterating until a condition is met (like end of file in file handling).
Looping in C – do-while Loop
Definition
The do-while loop is a control structure that executes the loop body first, and then checks
the condition.
If condition is true → loop continues.
If condition is false → loop terminates.
This ensures the loop executes at least once.
Syntax of do-while Loop
do {
// statements
} while (condition);
⚠️Note: semicolon (;) is mandatory at the end of the while statement.
Flowchart
[Loop Body]
|
Condition?
/ \
True False
| |
Working
• Loop body executes first without checking condition.
• Then condition is evaluated.
• If condition is true → body repeats.
• If condition is false → loop ends.
Example : Print Numbers from 1 to 5
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
Output:
1
2
3
4
5
Key Features of do-while Loop
[Link]-controlled loop (condition checked after body).
[Link] at least once, even if condition is false initially.
[Link] for menu-driven and interactive programs.
Applications
•Repeated user interaction (ATM, menu, calculators).
•Input validation (ask until correct input is given).
•Programs that must run at least once before checking a condition.
Difference Between while and do-while
Feature while Loop (Entry-controlled) do-while Loop (Exit-controlled)
Condition checking Before loop body After loop body
Executes at least once Not guaranteed Always executes once
Use case When 0 or more iterations possible When at least 1 iteration required
Looping in C – for Loop
Definition
The for loop is an entry-controlled loop that allows us to execute a block
of code a fixed number of times.
It combines initialization, condition, and increment/decrement in a
single line.
Syntax of for Loop
for (initialization; condition; update) {
// loop body
}
Initialization → executed once at the beginning (e.g., int i = 1;).
Condition → tested before every iteration. If false, loop ends.
Update → executed after each iteration (e.g., i++ or i--).
Flowchart of for Loop
Initialization
|
Condition True?
/ \
Yes No
| |
[Loop Body] Exit
|
Update
|
Repeat
Working
• Initialization happens first (only once).
• Condition is checked. If true → loop body executes.
• After loop body, update is performed.
• Condition is checked again → cycle continues.
Example 1: Print Numbers from 1 to 5
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
Output:
1
2
3
4
5
Key Features of for Loop
• Best when the number of iterations is known.
• Compact form (initialization, condition, update in one line).
• Supports multiple initialization and updates.
Applications
• Printing series of numbers.
• Generating tables, sums, and factorials.
• Iterating over arrays.
• Nested loops (patterns, matrices).
Looping in C – Nested Loops
Definition
A nested loop is a loop placed inside another loop.
The outer loop controls the number of rows/iterations.
The inner loop executes completely for each single iteration of the outer
loop.
Syntax of Nested Loop
for (initialization; condition; update) {
for (initialization; condition; update) {
// inner loop statements
}
// outer loop statements
}
👉 Similarly, we can nest while inside for, do-while inside while, etc.
Flowchart of Nested Loop
Outer Loop Start
|
Outer Condition True?
/ \
Yes No → Exit
|
[Inner Loop Start]
|
Inner Condition True?
/ \
Yes No
| |
[Inner Body] |
| |
Update Inner|
| |
Repeat -----+
|
Update Outer
|
Repeat Outer
Example 1: Printing a Square Pattern
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 3; i++) { // Outer loop
for (j = 1; j <= 3; j++) { // Inner loop
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
***
***
***
Key Features of Nested Loops
• Inner loop completes all iterations for each single iteration of outer
loop.
• Time complexity increases (O(n²), O(n³), etc.).
• Can use any combination (for inside while, do-while inside for, etc.).
Applications
• Printing patterns (stars, numbers, alphabets).
• Matrix operations (addition, multiplication, transpose).
• Nested menu systems.
• Simulations with multiple variables
Jumping Statements in C – break Statement
Definition
The break statement is a control statement used to exit immediately from a loop or
switch statement.
It transfers control to the statement immediately after the loop/switch.
It is mainly used to stop execution early when a certain condition is satisfied.
Syntax of break
break;
Flowchart of break
Start Loop
|
Condition True?
/ \
Yes No → Exit Loop
|
Check break condition?
/ \
Yes No
Using break in a Loop
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 6) {
break; // stop loop when i = 6
}
printf("%d\n", i);
}
return 0;
}
Output:
1
2
3
4
5
👉 Loop stopped when i == 6.
Key Features of break
• Used inside loops and switch statements.
• Immediately stops loop execution when encountered.
• Helps to avoid unnecessary iterations.
Applications
• Exiting from loops early (when search result is found).
• Preventing fall-through in switch cases.
• Breaking out of infinite loops when a condition is satisfied.
Jumping Statements in C – continue
Statement
Definition
The continue statement is used inside loops to skip the remaining statements of the
current iteration and directly go to the next iteration of the loop.
In for loop → control goes to the update statement.
In while / do-while → control goes to the condition checking part.
Syntax
continue;
Flowchart of continue
Start Loop
|
Condition True?
/ \
No Yes
Exit |
Check continue?
/ \
Yes No
Jump to update [Execute loop body]
Example 1: Skip Printing 5
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
continue; // skip when i=5//
}
printf("%d\n", i);
}
return 0;
}
Output:
1
2
3
4
6
7
8
9
10
👉 5 is skipped.
Key Features of continue
• Used inside loops only (for, while, do-while).
• Skips remaining code in current iteration.
• Loop does not stop, only moves to next iteration.
Applications
• Skipping unwanted values in iteration.
• Filtering values (skip negative numbers, skip zero, skip odd numbers).
• Improving efficiency by avoiding unnecessary code execution.
Jumping Statements in C – goto
Statement
Definition
The goto statement is used to transfer control to a labeled statement in the
same function. Execution immediately jumps to the specified label.
Can jump forward (to a label below) or backward (to a label above).
Syntax
goto label;
/* some code */
label: statement;
Flowchart of goto
Start
|
Check condition?
|
Yes -> goto label → Jump to Label Code
|
Example 1: Forward Jump
#include <stdio.h>
int main() {
int x = 1;
if (x == 1) {
goto skip; // jump forward
}
printf("This line will be skipped\n");
skip:
printf("Jumped using goto!\n");
return 0;
}
Output:
Jumped using goto!
Key Features of goto:
Transfers control unconditionally to a label.
Can be forward or backward jump.
Must be within the same function.
Applications:
Breaking out of deeply nested loops.
Error handling (jump to error block).
Quick exit in certain conditions.
Limitations / Disadvantages:
Makes code hard to read and debug.
Can create spaghetti code (complex jumps).
Replaced by structured statements (break, continue, functions, loops).
👉 Hence, use is discouraged in modern programming.
Feature break Continue goto
Purpose Exit loop/switch Skip current iteration Jump to a label
Usage Loops + switch Loops only Anywhere in a function
Control flow Forward only Forward only Forward/Backward (anywhere)
Recommendation Safe, widely used Safe, widely used Discouraged (unstructured)