Looping, or iteration, is a program structure that causes repeated execution of a block of code. There are two types of loops:
Loops with a known number of iterations (For Loop)
Loops with an unknown number of iterations (While Loop)
For Loop
The For Loop is commonly used when the number of iterations is known. It can be declared using the keyword for, followed by the initialization of the starting value, a condition to stop, and the increment/decrement of the initial value. The syntax looks like this:
1
for (initialization; condition; increment/decrement) {}
#include<stdio.h> #include<iostream> #include<math.h> using namespace std;
intmain() {
// For // Syntax Type 1: for (int i = 0; i < 5; i++) { // 5 is the final value | When i is in the range of 0~4 cout << "This is iteration number " << i << endl; }
cout << endl;
// Syntax Type 2: for (int i = 3; i < 10; i++) { // 3 is the starting value, 10 is the final value | When i is in the range of 3~9 cout << "This is iteration number " << i << endl; }
cout << endl;
// Syntax Type 3: for (int i = 20; i >= 5; i -= 5) { // 20 is the starting value, 5 is the final value, and the difference is -5 | // When i is in the range of 20~4, reduce by 5 in each iteration cout << "This is iteration number " << i << endl; }
return0; }
Here’s another example of implementing a For loop to retrieve data from a list-type variable:
#include<stdio.h> #include<iostream> #include<math.h> using namespace std;
intmain() {
// For loops are usually used to retrieve data from list-type variables // They are used when you have data in the form of a list/array
// List/Array // A collection of data (usually of the same type, but can also be mixed) // Declare a list using {} and separate each data with a comma (,)
int list_int_numbers[6] = {10, 8, 6, 4, 2, 0};
for (int i = 0; i < sizeof(list_int_numbers)/sizeof(list_int_numbers[0]); i++) { cout << "This is data number " << i + 1 << " and it is: " << list_int_numbers[i] << endl; } return0; }
While Loop
The While Loop is a type of loop that is activated using a boolean condition. It is typically used when the number of iterations is not known in advance. There are two versions of the while loop:
Conditional Version
Keyword Version
The conditional version is used when you want to repeat a loop based on a condition that you define yourself. Its usage is quite similar to a conditional if, as shown in the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<stdio.h> #include<iostream> #include<math.h> using namespace std;
intmain() {
int i = 0; while (i < 5) { cout << "This is iteration number " << i << endl; i = i + 1; // i += 1; // i++; } return0; }
The keyword version relies on keywords to stop or continue the loop, which can be risky and may lead to an infinite loop if not used carefully.
#include<stdio.h> #include<iostream> #include<math.h> using namespace std;
#define TRUE 1 #define FALSE 0
intmain() {
// Version without Keywords int status = TRUE; int i = 0; int int_max; cout << "Enter the Maximum Number:" << ends; cin >> int_max;
while (status) { if (i < int_max) { cout << "This is iteration number " << i << endl; i += 1; } else { cout << "Program Stopped" << endl; status = FALSE; } }
// Version with Keywords i = 0; // Resetting i for the second version cout << "Enter the Maximum Number:" << ends; cin >> int_max;
while (TRUE) { if (i < int_max) { cout << "This is iteration number " << i << endl; i += 1; continue; } else { cout << "Program Stopped" << endl; break; } }
return0; }
Do While Loop
Similar to the While loop, the Do While loop functions differently. While a While loop checks the condition before executing the code block, a Do While loop executes the code block first and then checks the condition to determine whether to continue the loop.
Here’s an example of its usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<stdio.h> #include<iostream> #include<math.h> using namespace std;
#define SIZE_DO_WHILE 10
intmain() {
int index = 0;
do { // Code that will be executed first cout << "Iteration number " << index << endl; index++; // Increment to prevent an infinite loop } while (index < SIZE_DO_WHILE); // After executing the above code, the condition is checked
return0; }
Exercise
Complete the following exercises to test your understanding of Control Structures II.
EXERCISE 1
Create a program to filter odd and even numbers based on the previous module's exercise using a loop.
OUTPUT:
1 2 3 4 5 6 7 8 9 10
Enter any number: 5 (Input by User) The number 5 is odd Do you want to repeat? Y (Input by User)
Enter any number: 8 (Input by User) The number 8 is even Do you want to repeat? N (Input by User)
Program Stops Thank you for using my program ^^
EXERCISE 2
Create a program to filter triangles based on the previous module's exercise using a loop.
OUTPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Enter Side A: 5 (Input by user) Enter Side B: 5 (Input by user) Enter Side C: 5 (Input by user)
It is an Equilateral Triangle
Do you want to repeat? Y (Input by User)
Enter Side A: 5 (Input by user) Enter Side B: 5 (Input by user) Enter Side C: 7 (Input by user)
It is an Isosceles Triangle
Do you want to repeat? N (Input by User)
Program Stops Thank you for using my program ^^
EXERCISE 3
Create a function program to calculate the average grade based on the letter categories inputted, with the following rules:
A = 4.00 A- = 3.75 B+ = 3.50 B = 3.00 B- = 2.75 C+ = 2.50 C = 2.00 C- = 1.75 D = 1.50 E = 1.20
OUTPUT:
1 2 3 4 5 6
Enter Grade Category (Press Enter to Stop): A (Input by User) // 4 Enter Grade Category (Press Enter to Stop): B (Input by User) // 3 Enter Grade Category (Press Enter to Stop): B (Input by User) // 3 Enter Grade Category (Press Enter to Stop): C (Input by User) // 2 Enter Grade Category (Press Enter to Stop): (Input by User) //(4 + 3 + 3 + 2)/4 = 3 The average grade is 3.00 with a designation of B