Lecture 02 - Fundamentals of Programming BENG24COE1-5
Lecture 02 - Fundamentals of Programming BENG24COE1-5
OF
PROGRAMMING
BENG24COE1-5
22nd Oct, 2024
1. Conditional Structures in Programming
2. Looping in Programming
Conditional Structures in
Programming
Conditional structures allow a program to make decisions and execute specific blocks of code
based on certain conditions. The most common conditional structures are if, else if, and else
statements.
Conditional structure….
if (condition) {
// Block of code to be executed if the condition is true
} else if (another_condition) {
// Block of code to be executed if the previous condition is false and this
condition is true
} else {
// Block of code to be executed if all conditions are false
}
Looping in Programming
Loops are used to repeat a block of code multiple times. There are three primary types of loops in
C++:
1. for loop
2. while loop
3. do-while loop
FOR LOOPING
For looping….
The for loop is used when the number of iterations is known in advance.
Example:
1. Definite Looping, Ideal for situations where the number of iterations is known beforehand.
2. Compact Structure, Initialization, condition, and update are all handled in a single line, making
it easy to read.
3. Easily Track Iteration, since the loop variable is usually declared in the loop header, it’s easy to
manage and track each iteration.
Disadvantages:
4. Limited Flexibility, Less flexible if the number of iterations is determined during runtime
(compared to a while loop).
5. May Cause Overhead, Overhead from initialization, condition checking, and updating the loop
variable in each iteration.
While looping
The while loop continues to execute as long as a specified condition remains true.
while (condition) {
}
While looping
#include <iostream>
int main() {
int count = 1;
count++;
} return 0;
}
While looping
Advantages:
1. Indefinite Looping, Best suited for cases where the number of iterations is not known in
advance and depends on a condition during execution.
2. Simplicity, Simpler in cases where the initialization or update step can’t be easily grouped with
the condition (e.g., user input-driven loops).
Disadvantages:
3. Risk of Infinite Loop, If the loop condition never becomes false, the loop can run indefinitely,
which may lead to freezing or crashing the program.
4. External Initialization, The loop control variable has to be initialized and updated outside of the
loop header, making the code more prone to errors if forgotten.
Do while looping
The do-while loop is similar to the while loop, but it guarantees that the code will be executed at
least once, even if the condition is false.
do {
// Code to be executed
} while (condition);
Do while looping
#include <iostream>
int main() {
int count = 1;
do {
count++;
return 0;
Do while looping
Advantages:
1. Guaranteed Execution, The loop body is executed at least once, even if the condition is false,
making it useful in cases where an initial action must always be taken.
2. Simple User Interaction, Often used for user-driven programs (e.g., asking for input), ensuring
the program runs at least once before deciding whether to continue.
Disadvantages:
3. Can Be Misleading, Since the loop runs at least once, it can lead to incorrect results if the
condition is false on the first check.
4. Harder to Read, Since the condition is checked at the end, it may make it slightly harder to
understand compared to a while or for loop where the condition appears upfront.
Bibititi and Morogoro Rd Junction
P. O. Box 2958
Dar-es-salaam, Tanzania
rector@dit.ac.tz
Prepared by: