Programming Practice Week3
Programming Practice Week3
Week 3: Fundamentals of
Programming
Presented by: Isaac Muckson Sesay
Week Objectives
• - Understand basic programming concepts.
• - Learn to work with variables and data types.
• - Explore control structures like loops and
conditionals.
• - Develop simple programs using these
fundamentals.
Introduction to Variables
• - Variables store data values.
• - Examples:
• - `int age = 25;` (integer)
• - `float height = 5.9;` (decimal)
• - `char grade = 'A';` (character)
• - `string name = "John";` (text)
• - Choose meaningful variable names.
Data Types
• - Basic data types:
• - Integer (`int`)
• - Floating-point (`float`, `double`)
• - Character (`char`)
• - Boolean (`true`/`false`)
• - Composite types:
• - Arrays
• - Strings
• - Understand type compatibility and conversions.
Input and Output
• - Input:
• - `scanf()` or similar methods to read user
input.
• - Example: `scanf("%d", &age);`
• - Output:
• - `printf()` or similar methods to display
output.
• - Example: `printf("Age: %d", age);`
Conditional Statements
• - Use conditionals to control program flow.
• - Examples:
• - `if (age > 18) { printf("Adult"); }`
• - `else { printf("Minor"); }`
• - `if-else if-else` for multiple conditions.
• - Nested conditionals and logical operators.
Loops in Programming
• - Loops repeat a block of code:
• - `for` loop: Used for a known number of
iterations.
• Example: `for (int i = 0; i < 10; i++) {}`
• - `while` loop: Repeats while a condition is true.
• Example: `while (x > 0) {}`
• - `do-while` loop: Ensures the block runs at least
once.
• - Avoid infinite loops.
Example Program: Sum of Numbers
• ```c
• int sum = 0;
• for (int i = 1; i <= 10; i++) {
• sum += i;
• }
• printf("Sum: %d", sum);
• ```
Debugging Basics
• - Common errors:
• - Syntax errors: Missing `;`, mismatched `{}`.
• - Runtime errors: Dividing by zero, invalid
input.
• - Logic errors: Incorrect calculations.
• - Use debugging tools or print statements to
find issues.
Practice Exercise 1
• - Problem: Write a program to calculate the
average of 5 numbers.
• - Steps:
• 1. Declare variables.
• 2. Use a loop to get user inputs.
• 3. Calculate and display the average.
Practice Exercise 2
• - Problem: Check if a given number is prime.
• - Steps:
• 1. Input a number.
• 2. Use a loop to check divisibility.
• 3. Output whether the number is prime.
Wrap-Up and Q&A
• - Summary of programming fundamentals.
• - Questions or challenges faced?
• - Prepare for Week 4: Functions and Modular
Programming.