DECISION MAKING AND LOOPING IN C
PROGRAMMING
DECISION-MAKING STATEMENTS IN C
Decision-making statements allow the program to execute specific blocks of code based on
conditions. These conditions evaluate to either true or false, enabling control over the program's
flow.
1. SIMPLE IF STATEMENT
The f statement evaluates a condition and executes a block of code only if the condition is true.
Syntax:
if (condition)
// Code to execute if the condition is true
How It Works
1. The condition is evaluated.
2. If the condition is true (non-zero), the code inside the block is executed.
3. If the condition is false (zero), the code inside the block is skipped.
Example: #include <stdio.h>
int main()
{
int number = 10;
if (number > 0)
{
printf("The number is positive.\n");
}
return 0;
}
Output: The number is positive.
2. IF-ELSE STATEMENT
The if-else statement provides two possible paths of execution:
One block for when the condition is true.
Another block for when the condition is false.
Syntax:if (condition)
// Code to execute if the condition is true
else
// Code to execute if the condition is false
How It Works
1. The condition is evaluated.
2. If the condition is true, the code in the if block is executed.
3. If the condition is false, the code in the else block is executed.
Example: #include <stdio.h>
int main()
int number = -5;
if (number > 0)
printf("The number is positive.\n");
}
else
printf("The number is not positive.\n");
return 0;
Output: The number is not positive.
3. NESTED IF STATEMENT
A nested if statement is an if or if-else statement inside another if or else block. It allows testing
multiple conditions in a hierarchical structure.
Syntax: if (condition1)
if (condition2)
// Code to execute if both conditions are true
else
// Code to execute if condition1 is true but condition2 is false
else
// Code to execute if condition1 is false
}
How It Works
1. Condition1 is evaluated first.
2. If condition1 is true, the nested if or else is evaluated.
3. If condition1 is false, the outer else block (if present) is executed.
Example: #include <stdio.h>
int main()
int number = 5;
if (number > 0)
if (number % 2 == 0)
printf("The number is positive and even.\n");
Else
printf("The number is positive and odd.\n");
else
printf("The number is not positive.\n");
return 0;
}
Output: The number is positive and odd.
4. ELSE-IF LADDER
The Else-If Ladder Is Used To Test Multiple Conditions Sequentially. It Provides An Efficient
Way To Write Code For Multiple Condition Checks.
Syntax:
if (condition1
// Code to execute if condition1 is true
else if (condition2)
// Code to execute if condition2 is true
else if (condition3)
// Code to execute if condition3 is true
else
// Code to execute if none of the conditions are true
How It Works
1. Conditions are evaluated from top to bottom.
2. The first true condition's block is executed.
3. If none of the conditions are true, the else block (if present) is executed.
Example: #include <stdio.h>
int main()
int marks = 85;
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: F\n");
return 0;
}
Output: Grade: B
5. SWITCH STATEMENT
The switch statement is used when multiple values of a single variable or expression are
compared against predefined cases.
Syntax:switch (expression)
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
...
default:
// Code to execute if no case matches
How It Works
1. The expression is evaluated.
2. The program jumps to the case that matches the value of the expression.
3. The break statement prevents execution from falling through to the next case.
4. The default block is executed if no case matches.
Example: #include <stdio.h>
int main()
{
int day = 3;
switch (day)
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
return 0;
Output: Wednesday
Comparison of Decision-Making Statements
Statement Use Case Advantages Disadvantages
Can become complex with
if Single condition checks. Simple and flexible.
many conditions.
Two mutually exclusive
if-else Easy to implement. Limited to two outcomes.
conditions.
Hierarchical or dependent Becomes hard to read for
Nested if Handles complex logic.
conditions. deep nesting.
Multiple conditions with one Readable for many Slower for a large number of
else-if
output. conditions. conditions.
switch Multiple conditions based on Efficient and readable for Limited to discrete values (no
Statement Use Case Advantages Disadvantages
a single variable. discrete values. ranges).
Key Notes
Use if statements for flexible and general-purpose condition checks.
Use switch for comparing a single variable against multiple discrete values.
Choose the decision-making statement that provides the clearest logic and
readability for your program.
LOOP STATEMENTS IN C
Loops are control structures used in C programming to execute a block of code multiple times,
either based on a condition or a fixed number of iterations. Loops reduce code redundancy and
are essential for efficient programming.
1. Classification of Loop Statements
While Loop in C
The while loop is a control flow statement in C that allows code to be executed repeatedly
based on a given condition. It is an entry-controlled loop, meaning the condition is checked
before the loop body executes. If the condition evaluates to true (non-zero), the loop executes
the block of code. If it evaluates to false (zero), the loop terminates.
Syntax: while (condition)
// Code to execute repeatedly
Condition: A boolean expression that determines whether the loop continues.
The loop executes repeatedly until the condition becomes false.
How it Works
1. Step 1: The condition is evaluated.
2. Step 2: If the condition is true, the loop body executes.
3. Step 3: After executing the loop body, the condition is evaluated again.
4. Step 4: The process repeats until the condition is false.
Flowchart
1. Start → Check Condition → If True → Execute Loop Body → Repeat.
2. If False → Exit Loop → End.
Examples
Example 1: Simple Counter
Print numbers from 1 to 5 using a while loop.
#include <stdio.h>
int main()
int i = 1; // Initialization
while (i <= 5) // Condition
printf("%d\n", i); // Print current value of i
i++; // Increment
return 0;
Output: 1
5
Example 2: Sum of Natural Numbers
Calculate the sum of numbers from 1 to n.
#include <stdio.h>
int main()
int n, sum = 0, i = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n)
sum += i; // Add i to sum
i++; // Increment i
printf("Sum = %d\n", sum);
return 0;
Input/Output:
Enter a positive integer: 5
Sum = 15
Example 3: Reverse Digits of a Number
Reverse the digits of a given number using a while loop.
#include <stdio.h>
int main()
{
int num, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0)
remainder = num % 10; // Get last digit
reverse = reverse * 10 + remainder; // Build reversed number
num /= 10; // Remove last digit
printf("Reversed Number = %d\n", reverse);
return 0;
Input/output: Enter an integer: 1234
Reversed Number = 4321
Example 4: Infinite Loop
A while loop runs infinitely when the condition always evaluates to true.
#include <stdio.h>
int main()
while (1)
{ // Always true
printf("This is an infinite loop.\n");
return 0;
}
Note: Use break to exit an infinite loop.
Example 5: Using break in a while Loop
Exit the loop when a specific condition is met.
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
if (i == 5) {
b reak; // Exit the loop when i equals 5
printf("%d\n", i);
i++;
return 0;
Output: 1
Example 6: Using continue in a while Loop
Skip the current iteration when a condition is met.
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
if (i == 3) {
i++; // Increment to avoid infinite loop
continue; // Skip the rest of the iteration
printf("%d\n", i);
i++;
return 0;
Output: 1
Common Errors in while Loops
1. Infinite Loops:
o Forgetting to update the loop variable leads to infinite execution.
int i = 1;
while (i <= 5)
printf("%d\n", i);
// Missing i++
}
2. Logical Errors:
Incorrect condition can result in skipped or extra iterations.
int i = 1;
while (i = 5)
{ // Mistaken assignment instead of comparison
printf("%d\n", i);
Advantages of while Loop
Ideal for indeterminate loops, where the number of iterations is not known in advance
(e.g., reading user input until a specific value is entered).
Simple and flexible for dynamic conditions.
Disadvantages of while Loop
Higher chance of creating infinite loops if the loop variable is not updated properly.
May be less concise than a for loop for cases with a known iteration count.
DO-WHILE LOOP IN C
The do-while loop is a control flow statement in C that allows code to be executed repeatedly
based on a condition. Unlike the while loop, the do-while loop is an exit-controlled loop,
meaning the condition is checked after the loop body is executed. This guarantees that the loop
body executes at least once, regardless of the condition.
Syntax: do
// Code to execute
while (condition);
Code block: Executes at least once, even if the condition is initially false.
Condition: A Boolean expression evaluated after each iteration. If it evaluates to true,
the loop continues.
How it Works
1. Step 1: Execute the block of code.
2. Step 2: Evaluate the condition.
3. Step 3: If the condition is true, repeat Step 1.
4. Step 4: If the condition is false, exit the loop.
Flowchart
1. Start → Execute Block → Check Condition.
2. If Condition is true, repeat execution.
3. If Condition is false, exit loop.
Examples
Example 1: Basic Usage
Print numbers from 1 to 5 using a do-while loop.
#include <stdio.h>
int main()
int i = 1; // Initialization
do
printf("%d\n", i); // Print the value of i
i++; // Increment
}
while (i <= 5); // Condition
return 0;
Output: 1
Example 2: User Input Validation
Keep asking the user for a positive number until they provide one.
#include <stdio.h>
int main() {
int num;
do {
printf("Enter a positive number: ");
scanf("%d", &num);
} while (num <= 0); // Repeat until a positive number is entered
printf("You entered: %d\n", num);
return 0;
Input/Output: Enter a positive number: -3
Enter a positive number: -7
Enter a positive number: 5
You entered: 5
Example 3: Sum of Digits
Calculate the sum of the digits of a given number using a do-while loop.
#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
do {
digit = num % 10; // Get the last digit
sum += digit; // Add it to the sum
num /= 10; // Remove the last digit
} while (num != 0); // Repeat until all digits are processed
printf("Sum of digits = %d\n", sum);
return 0;
Input/output: Enter a number: 1234
Sum of digits = 10
Example 4: Factorial Calculation
Calculate the factorial of a given number.
#include <stdio.h>
int main() {
int num, factorial = 1, i = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
do {
factorial *= i; // Multiply by i
i++; // Increment i
} while (i <= num); // Continue until i > num
printf("Factorial = %d\n", factorial);
return 0;
Input/output: Enter a positive integer: 5
Factorial = 120
Example 5: Infinite do-while Loop
A do-while loop can run infinitely if the condition is always true.
#include <stdio.h>
int main() {
int i = 1;
do {
printf("This is iteration %d\n", i);
i++;
} while (1); // Infinite loop because the condition is always true
return 0;
Note: Use break to exit the loop in practical scenarios.
Example 6: Using break in a do-while Loop
Exit the loop prematurely when a specific condition is met.
#include <stdio.h>
int main() {
int i = 1;
do {
if (i == 5) {
break; // Exit the loop when i equals 5
printf("%d\n", i);
i++;
} while (i <= 10);
return 0;
Output: 1
Example 7: Using continue in a do-while Loop
Skip the current iteration when a specific condition is met.
#include <stdio.h>
int main() {
int i = 1;
do {
if (i == 3) {
i++; // Increment to avoid infinite loop
continue; // Skip the rest of this iteration
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
Output: 1
Comparison with while Loop
Feature do-while Loop while Loop
Condition
After executing the loop body. Before executing the loop body.
Evaluation
Execution May not execute if the condition is
Executes at least once.
Guarantee false.
When the loop body must execute at least When the loop body should execute
Use Case
once (e.g., menu-driven programs). only if the condition is true.
Advantages of do-while Loop
1. Ensures the loop body is executed at least once.
2. Useful for scenarios where an action needs to be performed and then validated (e.g., input
validation).
Disadvantages of do-while Loop
1. May execute unnecessarily if the condition is not valid but is checked after execution.
2. Less commonly used than the for and while loops, making code potentially harder to
read.
FOR LOOP IN C
The for loop is a control flow statement in C used to repeat a block of code a specific number of
times. It is particularly useful when the number of iterations is known in advance. The for loop
allows for concise initialization, condition checking, and increment/decrement in a single
statement.
Syntax:
for (initialization; condition; increment/decrement)
// Code to execute repeatedly
Initialization: Typically used to initialize the loop control variable (e.g., int i = 0).
Condition: The condition is evaluated before each iteration. The loop continues as long
as the condition is true.
Increment/Decrement: This part updates the loop control variable (e.g., i++ or i--).
How it Works
1. Step 1: Initialization is executed only once before the loop starts.
2. Step 2: The condition is evaluated. If it is true, the loop body executes.
3. Step 3: After executing the body, the loop control variable is updated (increment or
decrement).
4. Step 4: The condition is checked again. If true, the loop repeats from step 2. If false, the
loop ends.
Flowchart
1. Initialize control variable.
2. Check condition:
o If true, execute the loop body.
o If false, exit the loop.
3. Update the control variable.
4. Repeat from step 2 if condition is still true.
Examples
Example 1: Basic for Loop
Print numbers from 1 to 5 using a for loop.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) { // Initialization, Condition, Increment
printf("%d\n", i); // Print current value of i
}
return 0;
}
Output: 1
2
3
4
5
Example 2: Sum of Natural Numbers
Calculate the sum of numbers from 1 to n.
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i; // Add i to sum
}
printf("Sum = %d\n", sum);
return 0;
}
Input/Output: Enter a positive integer: 5
Sum = 15
Example 3: Factorial Calculation
Calculate the factorial of a given number.
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i; // Multiply by i
}
printf("Factorial = %d\n", factorial);
return 0;
}
Input/Output: Enter a positive integer: 5
Factorial = 120
Example 4: Reverse a Number
Reverse the digits of a number using a for loop.
#include <stdio.h>
int main() {
int num, reversed = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = num; i != 0; i /= 10) {
int digit = i % 10; // Get the last digit
reversed = reversed * 10 + digit; // Build the reversed number
}
printf("Reversed Number = %d\n", reversed);
return 0;
}
Input/Output:
Enter a number: 1234
Reversed Number = 4321
Example 5: Multiplication Table
Generate the multiplication table of a given number.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= 10; i++) {
printf("%d * %d = %d\n", num, i, num * i);
}
return 0;
}
Input/Output:
Enter a number: 5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Example 6: Nested for Loop
Generate a multiplication table using nested for loops.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
printf("%d ", i * j); // Print product of i and j
}
printf("\n"); // New line after each row
}
return 0;
}
Output: 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Advantages of for Loop
1. Compact syntax: All parts (initialization, condition, and increment) are in one place,
making it easy to understand.
2. Perfect for definite loops: Ideal when the number of iterations is known beforehand.
3. Control: Allows fine control over the loop variable.
Disadvantages of for Loop
1. Not ideal for indeterminate loops: If the number of iterations is not known, a while loop
is better.
2. Complex conditions: The initialization, condition, and increment can become too
complex for some situations.
Comparison with Other Loops
Feature for Loop while Loop do-while Loop
Before the loop Before the loop After the loop
Condition
body (entry- body (entry- body (exit-
Evaluation
controlled). controlled). controlled).
Executes based May not execute
Execution Executes at least
on a known if condition is
Guarantee once.
iteration count. false.
When the When the
When the loop
number of number of
Use Case must execute at
iterations is iterations is
least once.
known. unknown.
Break and Continue Statements in C
The break and continue statements are control flow statements in C that alter the normal flow of
a loop. These statements allow greater flexibility in managing loop execution.
. 1. break Statement
The break statement is used to exit from a loop or switch statement prematurely. When a break is
encountered, the control exits the current loop or switch block and moves to the first statement
following the loop or switch.
Syntax: break;
How It Works
1. The break is usually used with conditional statements.
2. When the break statement is executed, the program immediately exits the loop or switch.
3. Control is transferred to the statement immediately following the loop or switch.
Examples
Example 1: break in a for Loop
Exit the loop when the condition is met.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i equals 5
printf("%d\n", i);
return 0;
Output: 1
Example 2: break in a while Loop
Exit the loop based on a condition.
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
if (i == 5) {
break; // Exit the loop when i equals 5
printf("%d\n", i);
i++;
return 0;
Output: 1
Example 3: break in a switch Statement
Terminate a switch case block.
#include <stdio.h>
int main() {
int num = 2;
switch (num) {
case 1:
printf("Case 1\n");
break;
case 2:
printf("Case 2\n");
break; // Exit the switch block
case 3:
printf("Case 3\n");
break;
default:
printf("Default case\n");
return 0;
Output: Case 2
2. CONTINUE STATEMENT
The continue statement is used to skip the current iteration of a loop and immediately proceed
to the next iteration. It doesn't terminate the loop but skips the remaining code in the loop body
for the current iteration.
Syntax: continue;
How It Works
1. The continue statement is typically used with conditional statements.
2. When continue is executed:
o For for loops: The control jumps to the increment/decrement step.
o For while and do-while loops: The control jumps to the condition check.
Examples
Example 1: continue in a for Loop
Skip the current iteration if a condition is met.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the iteration when i equals 3
printf("%d\n", i);
return 0;
Output: 1
Example 2: continue in a while Loop
Skip the iteration when a condition is true.
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
i++;
if (i == 3) {
continue; // Skip the iteration when i equals 3
printf("%d\n", i);
return 0;
Output: 2
Example 3: Skip Even Numbers
Use continue to print only odd numbers.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
printf("%d\n", i);
return 0;
}
Output: 1
Differences Between break and continue
Feature break continue
Effect on Skips the current iteration and continues with
Terminates the loop entirely.
Loop the next.
Used to skip part of a loop and proceed to the
Usage Used to exit a loop or switch.
next iteration.
Control moves out of the loop or
Control Flow Control moves to the next iteration.
switch.
Examples combining break and continue
Using both break and continue in a loop.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 3) {
continue; // Skip when i equals 3
if (i == 8) {
break; // Exit the loop when i equals 8
}
printf("%d\n", i);
return 0;
Output: 1
Key Points
1. break:
o Completely exits the loop.
o Commonly used with conditional statements to terminate loops or switch cases.
2. continue:
o Skips the current iteration and moves to the next.
o Frequently used to bypass specific cases in loops.
Both break and continue add flexibility and control to loops, enhancing their functionality.
ARRAYS AND STRINGS
Arrays – definition
Arrays in C
An array in C is a collection of elements of the same data type stored in contiguous memory
locations. Arrays allow you to store multiple values under a single variable name, accessed using
an index.
Key Features
1. Fixed Size: The size of an array is defined during its declaration and cannot be changed
at runtime.
2. Homogeneous Elements: All elements in the array must be of the same data type (e.g.,
int, float, char).
3. Indexing: Array elements are accessed using a zero-based index. The first element is at
index 0, the second at index 1, and so on.
Syntax: data_type array_name[size];
data_type: The type of elements stored in the array (e.g., int, float, char).
array_name: The name of the array.
size: The number of elements the array can hold.
Types of Arrays
1. One-Dimensional Array: A single row or column of elements.
2. Multi-Dimensional Array: Arrays with more than one dimension, such as 2D or 3D
arrays.
Declaring and Initializing Arrays
1. Declaring an Array
int numbers[5]; // An integer array with 5 elements
2. Initializing an Array
int numbers[5] = {1, 2, 3, 4, 5}; // Initialize with values
3. Accessing Array Elements
printf("%d", numbers[0]); // Access the first element
One-Dimensional Array
Example 1: Declare and Initialize
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
// Print array elements
for (int i = 0; i < 5; i++) {
printf("Element at index %d: %d\n", i, numbers[i]);
return 0;
Output: Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Example 2: Sum of Array Elements
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
printf("Sum of elements: %d\n", sum);
return 0;
Output: Sum of elements: 15
Two-Dimensional Array
A two-dimensional array is like a table with rows and columns.
Syntax: data_type array_name[rows][columns];
Example: Declare and Access 2D Array
#include <stdio.h>
int main() {
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Print 2D array elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("Element at [%d][%d]: %d\n", i, j, matrix[i][j]);
return 0;
Output: Element at [0][0]: 1
Element at [0][1]: 2
Element at [0][2]: 3
Element at [1][0]: 4
Element at [1][1]: 5
Element at [1][2]: 6
String as Character Array
Strings in C are arrays of characters terminated by a null character (\0).
Example: String Array
#include <stdio.h>
int main() {
char name[] = "Hello";
printf("String: %s\n", name);
printf("First character: %c\n", name[0]);
return 0;
Output: String: Hello
First character: H
Key Points
1. Memory: Arrays are stored in contiguous memory locations.
2. Indexing: Starts from 0 and goes up to size - 1.
3. Boundary: Accessing elements beyond the array size leads to undefined behavior.
Advantages of Arrays
1. Efficient Data Storage: Stores multiple values of the same type in a single variable.
2. Indexed Access: Easy to retrieve and modify elements using their index.
3. Static Memory Allocation: Fixed size ensures efficient memory management.
Disadvantages of Arrays
1. Fixed Size: Cannot grow or shrink dynamically.
2. Homogeneity: Can only store elements of the same type.
3. Overhead: Extra care needed for boundary conditions to avoid errors.