Programming in C 3rd Unit
Programming in C 3rd Unit
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)
How It Works
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.
2. IF-ELSE STATEMENT
Syntax:if (condition)
else
How It Works
int main()
if (number > 0)
else
return 0;
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)
else
else
How It Works
int main()
int number = 5;
if (number > 0)
if (number % 2 == 0)
Else
else
return 0;
}
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
else if (condition2)
else if (condition3)
else
How It Works
3. If none of the conditions are true, the else block (if present) is executed.
int main()
printf("Grade: A\n");
printf("Grade: B\n");
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:
break;
case value2:
break;
...
default:
How It Works
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.
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
Key Notes
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.
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.
How it Works
Flowchart
Examples
#include <stdio.h>
int main()
int i = 1; // Initialization
i++; // Increment
return 0;
Output: 1
5
Example 2: Sum of Natural Numbers
#include <stdio.h>
int main()
int n, sum = 0, i = 1;
scanf("%d", &n);
while (i <= n)
i++; // Increment i
return 0;
Input/Output:
Sum = 15
#include <stdio.h>
int main()
{
int num, reverse = 0, remainder;
scanf("%d", &num);
while (num != 0)
return 0;
A while loop runs infinitely when the condition always evaluates to true.
#include <stdio.h>
int main()
while (1)
{ // Always true
return 0;
}
#include <stdio.h>
int main() {
int i = 1;
if (i == 5) {
printf("%d\n", i);
i++;
return 0;
Output: 1
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
if (i == 3) {
printf("%d\n", i);
i++;
return 0;
Output: 1
1. Infinite Loops:
int i = 1;
while (i <= 5)
printf("%d\n", i);
// Missing i++
}
2. Logical Errors:
int i = 1;
while (i = 5)
printf("%d\n", i);
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).
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
Flowchart
Examples
#include <stdio.h>
int main()
int i = 1; // Initialization
do
i++; // Increment
}
while (i <= 5); // Condition
return 0;
Output: 1
Keep asking the user for a positive number until they provide one.
#include <stdio.h>
int main() {
int num;
do {
scanf("%d", &num);
return 0;
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() {
scanf("%d", &num);
do {
return 0;
Sum of digits = 10
#include <stdio.h>
int main() {
do {
factorial *= i; // Multiply by i
i++; // Increment i
return 0;
Factorial = 120
#include <stdio.h>
int main() {
int i = 1;
do {
i++;
return 0;
#include <stdio.h>
int main() {
int i = 1;
do {
if (i == 5) {
printf("%d\n", i);
i++;
return 0;
Output: 1
#include <stdio.h>
int main() {
int i = 1;
do {
if (i == 3) {
printf("%d\n", i);
i++;
return 0;
Output: 1
Condition
After executing the loop body. Before executing the loop body.
Evaluation
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.
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:
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);
Input/Output:
Enter a number: 1234
Reversed Number = 4321
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.
. 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
Examples
#include <stdio.h>
int main() {
if (i == 5) {
printf("%d\n", i);
return 0;
Output: 1
#include <stdio.h>
int main() {
int i = 1;
if (i == 5) {
printf("%d\n", i);
i++;
return 0;
Output: 1
#include <stdio.h>
int main() {
int num = 2;
switch (num) {
case 1:
printf("Case 1\n");
break;
case 2:
printf("Case 2\n");
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
Examples
#include <stdio.h>
int main() {
if (i == 3) {
printf("%d\n", i);
return 0;
Output: 1
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
i++;
if (i == 3) {
printf("%d\n", i);
return 0;
Output: 2
#include <stdio.h>
int main() {
if (i % 2 == 0) {
printf("%d\n", i);
return 0;
}
Output: 1
#include <stdio.h>
int main() {
if (i == 3) {
if (i == 8) {
}
printf("%d\n", i);
return 0;
Output: 1
Key Points
1. break:
2. continue:
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.
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. Declaring an Array
2. Initializing an Array
int numbers[5] = {1, 2, 3, 4, 5}; // Initialize with values
One-Dimensional Array
#include <stdio.h>
int main() {
return 0;
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
#include <stdio.h>
int main() {
int sum = 0;
sum += numbers[i];
return 0;
Two-Dimensional Array
#include <stdio.h>
int main() {
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
return 0;
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
#include <stdio.h>
int main() {
First character: H
Key Points
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.
Disadvantages of Arrays