Course Title : Computer Programming 1
Section :
Instructor : Mr. Mabud J. Amirul
Module 3
Day 1 Lesson Plan – If-Else Statements
in C
Introduction
In programming, not all tasks are linear—sometimes, a program must make decisions. For
example, checking whether a student has passed or failed, or whether a number is odd or
even. This is achieved using decision-making structures in C, such as if, if-else, and nested if
statements.
Learning Objectives
• Understand the purpose of decision-making in programs.
• Implement if and if-else statements in C.
• Write simple programs that apply conditional logic.
Discussion Part 1: What is Decision-Making in Programming?
A program sometimes needs to choose between two or more paths.
Example in real life:
• If it’s raining, take an umbrella.
• Else, wear sunglasses.
This is similar to how computers use if-else conditions.
Discussion Part 2: If Statement
Syntax:
if (condition) {
// code to execute if condition is true
}
Example:
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.\n");
}
Discussion Part 3: If-Else Statement
Syntax:
if (condition) {
// executes if condition is true
} else {
// executes if condition is false
}
Example:
int grade = 70;
if (grade >= 75) {
printf("Pass\n");
} else {
printf("Fail\n");
}
Discussion Part 4: Nested If
Example:
int grade = 74;
if (grade >= 90) {
printf("Excellent\n");
} else if (grade >= 75) {
printf("Pass\n");
} else {
printf("Fail\n");
}
Class Activity
1. Write a program that checks whether a number is positive or negative.
2. Modify the program to also check if the number is zero.
2.
3.
Sample Solution:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("Positive\n");
} else if (num < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
return 0;
}
Assessment
Short Quiz (5 pts)
1. What keyword is used in C for decision-making?
2. Write the syntax of an if-else statement.
3. What will be the output if grade = 50 in this code:
if (grade >= 75) printf("Pass");
else printf("Fail");
4. Can you use multiple else if statements in one program? (Yes/No)
5. True or False: An if statement can work without an else.
Code Exercise (10 pts)
Write a program that asks for a student’s grade and:
- Prints “Excellent” if grade ≥ 90
- Prints “Pass” if grade is 75–89
- Prints “Fail” if grade < 75
Day 2 Lesson Plan – Switch Statements
in C
Introduction
This lesson introduces the switch statement in the C programming language. The switch
statement is a control structure that allows a variable to be tested against a list of constant
values. It is often used for menu-driven programs and cases where multiple possible
outcomes exist based on a single expression.
Learning Objectives
• Explain the purpose of a switch statement in C.
• Differentiate between switch and if-else structures.
• Implement switch statements in C programs.
• Apply switch in creating simple menu-driven applications.
Discussion Part 1: What is a Switch Statement?
The switch statement allows selection of one block of code to be executed out of many
options. It is often cleaner than multiple else-if conditions.
Syntax of Switch Statement
switch (expression) {
case constant1:
// code block
break;
case constant2:
// code block
break;
...
default:
// default code block
}
Discussion Part 2: Example Program
#include <stdio.h>
int main() {
int choice;
printf("Enter a number (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You chose option 1\n");
break;
case 2:
printf("You chose option 2\n");
break;
case 3:
printf("You chose option 3\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Discussion Part 3: Difference Between If-Else and Switch
• If-Else: More flexible, can handle ranges and logical conditions.
• Switch: Cleaner and more readable when dealing with many constant values.
• If-Else is better for complex conditions, while switch is better for menu selections.
Class Activity
Hands-on Lab:
1. Write a program that uses switch to display the day of the week based on a number (1 =
Monday, 2 = Tuesday, etc.).
2. Modify the program to handle invalid inputs using the default case.
Sample Code:
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid input\n");
}
return 0;
}
Assessment
Short Quiz (5 pts)
1. What keyword is used to define each branch of a switch statement?
2. What is the purpose of the 'break' statement in a switch?
3. True or False: Switch statements can test ranges (e.g., x > 5).
4. Write the default case syntax for a switch statement.
5. Which is better for menu-driven programs: if-else or switch? Why?
Code Exercise (10 pts)
Write a menu-driven calculator program using switch that allows the user to:
- Enter two numbers
- Choose an operation: 1 = Add, 2 = Subtract, 3 = Multiply, 4 = Divide
- Display the result based on the selected option
- Display 'Invalid choice' for other inputs
Day 3 Lesson Plan – Loops in C (For,
While, Do-While)
Introduction
This lesson introduces loops in the C programming language. Loops allow a set of
instructions to be executed repeatedly until a condition is met. Students will learn how to
implement the three main types of loops in C: for, while, and do-while. These control
structures are essential for solving problems that require repetition, such as generating
tables, summing numbers, and creating calculators.
Learning Objectives
• Explain the purpose of loops in programming.
• Differentiate among for, while, and do-while loops.
• Write simple programs using each type of loop.
• Apply loops to solve computational problems (e.g., looping calculator, summing values,
printing tables).
Discussion Part 1: For Loop
The for loop is used when the number of iterations is known.
Syntax:
for (initialization; condition; increment) {
// statements
}
Example Code:
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
Discussion Part 2: While Loop
The while loop is used when the number of iterations is not predetermined, but depends on
a condition.
Syntax:
while (condition) {
// statements
}
Example Code:
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
Discussion Part 3: Do-While Loop
The do-while loop is similar to the while loop, but ensures the block of code runs at least
once.
Syntax:
do {
// statements
} while (condition);
Example Code:
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
Discussion Part 4: Choosing the Right Loop
• Use for loop when the number of iterations is known (e.g., printing 1 to 100).
• Use while loop when the iterations depend on a condition (e.g., keep asking until correct
input).
• Use do-while loop when you want the block to execute at least once (e.g., menu-driven
programs).
Class Activity
Hands-on Lab:
1. Write a program using a for loop to print the multiplication table of 5.
2. Create a program using a while loop to calculate the sum of numbers from 1 to 100.
3. Write a menu-driven calculator using a do-while loop that continues until the user
chooses to exit.
Sample Code for Multiplication Table:
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("5 x %d = %d\n", i, 5 * i);
}
return 0;
}
Assessment
Short Quiz (5 pts)
1. Which type of loop always executes at least once?
2. Write the syntax of a while loop.
3. What are the three parts of a for loop?
4. Write the output of: for (int i=1; i<=3; i++) printf("%d ", i);
5. True or False: A while loop may never execute if the condition is false initially.
Code Exercise (10 pts)
Write a program that:
- Prints numbers 1 to 10 using a for loop.
- Calculates the factorial of a number using a while loop.
- Implements a do-while loop that repeatedly asks the user for a number until they enter 0.