0% found this document useful (0 votes)
0 views

Repetition Control Structures

The document provides an introduction to repetition control structures and arrays in C++, detailing the types of loops (for, while, do-while) and their appropriate use cases. It also covers array declaration, initialization, accessing elements, and common operations, while highlighting common mistakes to avoid. The document includes examples and learning objectives for students in the Department of Electrical Engineering at Aliko Dangote University.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Repetition Control Structures

The document provides an introduction to repetition control structures and arrays in C++, detailing the types of loops (for, while, do-while) and their appropriate use cases. It also covers array declaration, initialization, accessing elements, and common operations, while highlighting common mistakes to avoid. The document includes examples and learning objectives for students in the Department of Electrical Engineering at Aliko Dangote University.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Department of Electrical Engineering

Aliko Dangote University of Science and Technology, Wudil

Repetition Control Structures in C++ and Arrays


An Introduction to Loops and Arrays in C++

Department of Electrical Engineering

First Semester
2024/2025

1
ELE2309 COMPUTER PROGRAMMING 2024/2025
Learning Objectives

By the end of this lecture, students will be able to:


• Understand the concept of repetition control structures.
• Learn the types of loops in C++.
• Implement loops using for, while, and do-while.
• Identify appropriate use cases for each loop type.
• Avoid common mistakes in loop implementation.
What is a Repetition Control
Structure?
• A loop is a programming construct that repeats a set of instructions
until a condition is met.
• Loops help in reducing code redundancy and improving efficiency.
• Types of loops in C++:
• for loop
• while loop
• do-while loop
The for Loop
Definition:
• A for loop is used when the number of iterations is known beforehand.
• It consists of three parts:
• Initialization: Declares and initializes the loop variable.
• Condition: Determines whether the loop continues.
• Update: Modifies the loop variable after each iteration.
Syntax:
for(initialization; condition; update) {
// Code to execute
}
Example:
for(int i = 1; i <= 5; i++) {
cout << "Iteration " << i << endl;
}
The while Loop
Definition:
• The while loop executes as long as the condition remains true.
• Best when the number of iterations is unknown in advance.
Syntax:
while(condition) {
// Code to execute
}
Example:
int i = 1;
while(i <= 5) {
cout << "Iteration " << i << endl;
i++;
}
The do-while Loop

Definition:
• Similar to the while loop but guarantees execution at least once.
• The condition is checked after the execution of the loop body.
Syntax:
do {
// Code to execute
} while(condition);
Example:
int i = 1;
do {
cout << "Iteration " << i << endl;
i++;
} while(i <= 5);
Loop Control Statements
1. break Statement
• Terminates the loop execution immediately.
Example:
for(int i = 1; i <= 5; i++) {
if(i == 3) break;
cout << i << endl;
}
2. continue Statement
• Skips the current iteration and moves to the next one.
Example:
for(int i = 1; i <= 5; i++) {
if(i == 3) continue;
cout << i << endl;
}
Choosing the Right Loop

Loop Type When to Use

for loop When the number of iterations is known.

When the number of iterations is unknown but


while loop
depends on a condition.

When you must execute at least once, even if the


do-while loop
condition is false.
Common Mistakes in Loops

• Infinite loops: Forgetting to update the loop variable.


• Incorrect loop conditions: Using the wrong comparison operators.
• Off-by-one errors: Starting or ending the loop incorrectly.
• Using the wrong loop type for the given scenario.
Example of an Infinite Loop:
int i = 1;
while(i <= 5) {
cout << "Iteration " << i << endl; // i is not updated
}
Example: Sum of First N
Numbers
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter a number: ";
cin >> n;
for(int i = 1; i <= n; i++) {
sum += i;
}
cout << "Sum: " << sum << endl;
return 0;
}
Nested for Loops
• Definition:
• A loop inside another loop is called a nested loop.
• The inner loop runs completely for every single iteration of the outer loop.
• Syntax:
for(int i = 1; i <= outer_limit; i++) {
for(int j = 1; j <= inner_limit; j++) {
// Code to execute
}
}
Example:
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 2; j++) {
cout << "Outer: " << i << ", Inner: " << j << endl;
}
}
Output:
Outer: 1, Inner: 1
Outer: 1, Inner: 2
Outer: 2, Inner: 1
Outer: 2, Inner: 2
Outer: 3, Inner: 1
Summary
• Loops allow repetition in programming to improve efficiency.
• for loops are used when the number of iterations is known.
• while loops are used when the number of iterations is unknown.
• do-while loops ensure execution at least once.
• Nested loops allow working with multi-dimensional data.
• Use break and continue statements for better control.
• Avoid infinite loops and off-by-one errors.
Arrays Learning Objectives

By the end of this lecture, students will be able to:


• Understand what an array is in C++.
• Declare and initialize a 1D array.
• Access and modify elements in an array.
• Perform basic operations on arrays.
• Understand common mistakes and best practices when using arrays.
What is an Array?

• An array is a collection of elements of the same data type stored in


contiguous memory locations.
• It allows multiple values to be stored using a single variable name.
• Arrays provide efficient access to elements using an index.
Declaring and Initializing an
Array
• Declaration Syntax:
data_type array_name[array_size];
Examples:
int numbers[5]; // Declaring an integer array of size 5
char letters[10]; // Declaring a character array of size 10
Initialization Methods:
int numbers[5] = {1, 2, 3, 4, 5}; // Explicit initialization
int values[] = {10, 20, 30}; // Size inferred automatically
int emptyArray[5] = {0}; // All elements set to 0
Accessing and Modifying Array
Elements
• Accessing Elements: Array elements are accessed using their index,
which starts from 0.
• Modifying Elements: Values can be changed by assigning a new value
to an index.
• Example:
int numbers[5] = {10, 20, 30, 40, 50};
cout << numbers[2]; // Outputs 30
numbers[2] = 100; // Modifies the 3rd element
cout << numbers[2]; // Outputs 100
Traversing an Array

• Using a for Loop:


int numbers[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
• Using a while Loop:
int i = 0;
while(i < 5) {
cout << numbers[i] << " ";
i++;
}
Common Operations on Arrays

• Finding the Maximum Element


int arr[5] = {10, 50, 30, 20, 40};
int max = arr[0];
for(int i = 1; i < 5; i++) {
if(arr[i] > max)
max = arr[i];
}
cout << "Max: " << max;
• Calculating the Sum of Elements
int sum = 0;
for(int i = 0; i < 5; i++) {
sum += arr[i];
}
cout << "Sum: " << sum;
Common Mistakes With Arrays
• Accessing Out-of-Bounds Elements:
int arr[5] = {1, 2, 3, 4, 5};
cout << arr[10]; // Undefined behavior
• Forgetting to Initialize an Array:
int arr[5]; // Contains garbage values
• Using Incorrect Loop Conditions:
int arr[5] = {1, 2, 3, 4, 5};
for(int i = 1; i <= 5; i++) // Wrong! Should be i < 5
Example: Reversing an Array
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
cout << "Original: ";
for(int i = 0; i < 5; i++) cout << arr[i] << " ";
cout << "\nReversed: ";
for(int i = 4; i >= 0; i--) cout << arr[i] << " ";
return 0;
}
Output:
Original: 10 20 30 40 50
Reversed: 50 40 30 20 10
Summary

• Arrays store multiple values in contiguous memory locations.


• Elements are accessed using indexing (starting from 0).
• Loops help traverse and manipulate arrays efficiently.
• Be cautious of out-of-bounds errors and uninitialized arrays.
Solved Examples

Write a C++ program that takes an array of 50 floating-point numbers as


input. The program should calculate and display the average value of the
elements in the array, along with the count of elements above and below
the average.

You might also like