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

Loops Notes in C

The document discusses different types of loops in C++ including while loops, for loops, and do-while loops. It provides examples of each loop type and explains the basic structure and logic of the initialization, condition, and update components of loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Loops Notes in C

The document discusses different types of loops in C++ including while loops, for loops, and do-while loops. It provides examples of each loop type and explains the basic structure and logic of the initialization, condition, and update components of loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Loops Notes in C++

Introduction for loops – when things are repeated.

Loop concept:
I – initialization: keep track of where we are (starts with variable) mo hatag ta ug value sa isa ka
variable.

C – condition: the loop will continue to execute as long as the condition is true.

U – update: increase or decrease of your loop

WHILE LOOP
- Simple loop.
- Codes happen after the condition
- Used when you don’t know how many times the loop will be executed.
- Indefinitely

Example of while loop:

#include <iostream>

int main() {
int i = 1;
while (i <= 5) {
std::cout << "Number: " << i << std::endl;
i++;
}
return 0;
}
Explanation:

- We start by initializing a variable i to 1. Mao ni ang Initialization


- The while loop continues to execute as long as the condition i <= 5 is true. Kani ang Condition
- Inside the loop, we print the value of i and then increment it by 1 using i++. Kani ang Update

Ang naa sa example sa babaw kay una ang integer value sa i = 1 then gi sundan sa while loop with the
condition of (i <= 5) then gi sundan sa code na mo print out sa word na “Number” + value sa i, mao ni
ang I print sa screen. after sa code mao ang update which is mo increase or decrease ba. Sa case sa
example kay increase man so ang I butang sa update kay i++;.

FOR-LOOP
- Used when you know how many times the loop will be executed
- Counted
The syntax of for-loop:
for (initialization; condition; update) {
// block of code to be executed}

Example of a for loop

#include <iostream>

int main() {
// Using a for loop to print numbers from 1 to 5
for (int i = 1; i <= 5; ++i) {
std::cout << i << " ";
}
std::cout << std::endl;

return 0;
}

Explanation about the loop above:

- To start a program we include the <iostream> header to enable the input/output operations.
Note that the header we put above will enable us to do certain operations to our programs.
- Next is to declare a “main” function as the entry of the program.
- Inside sa “main” function, we use a kind of loop. In this example, we use for loop to iterate over
the numbers from 1 to 5. And what does iterate means? Iterate means is to read, run and
repeats a block of code until a certain condition will be meet.
As for the main structure of a loop:
 We initialize first with i=1 as to indicate that mag start and i sa 1.
 After initialization, we set a condition which is i<=5, which means that the loop will
continue as long as the condition is true and that is i will be less than or equal to 5.
 The we will identify the update which will be an increment of decrement. As for the
example, we will increment i++ which means that at the start. The value of i which is
1 will increase until the condition will be met which is i<=5;
- After the this we will use std::cout<<std::endl; , cout<<endl; , or cout<<”\n”; . naa ana nila ang
pwede gamiton depending on the directive or variable declaration na gibutang nimo.
Do while loop
- Code executed at least once and then the condition will be checked.
- always executes its body at least once, and then repeats as long as a specified condition is true.

Syntax for a do-while-loop:

do {

// Statements to be executed

} while (condition);

Example:

#include <iostream>

int main()
{
int i = 1; // Initialize the counter variable

// A do-while loop that prints numbers from 1 to 5


do {
std::cout << i << " ";
i++; // Increment the counter variable
} while (i <= 5); // Check the loop condition

std::cout << std::endl;

return 0;
}

Explanation:

- first is to include <iostream> to enable input/output functions sa program


- I determine nato ang “main” function para sa entry sa atong program.
- Next will be the structure sa do while loop:
 Mag input ta una sa do{} kani na curly braces. After sa curly braces usa ta mo input
ug while();

do
{
}while();
 You will write your initialization before the do while loop but inside the “main”. As
for the example, int (integer) i = 1; so it means that the starting will be 1.
 Sa sulod sa do anha nimo I butang ang imong code. Sa case sa example sa ibabaw is
ang code is std::cout << i << " "; , which means to print the value of i and after sa
code is this i++; , which an update sa imong code. Makita nato ang value sa i kay
increasing by determining the update mao na ang ++ or --.
 Last is the condition of your code. As for the example above, while (i <= 5); the code
will run until the condition will be met which means ug ang value sa i<=5 then mo
stop na ang code.

Note: this example kay similar sa uban na klase sa loop so I will give another example that best describes
the do while loop.

#include <iostream>
#include <string>

using namespace std;

int main()
{
string password = "hihello";
string guess;

do
{
cout << "Guess the password: ";
cin >> guess;
} while (guess != password);

return 0;
}

sa example sa ibabaw, ang code nangayo sa password which is ang “hihello” .

I’ll explain the code:

- Same sa uban na code it starts with the header and declaration jud
- Sa structure sa do while loop sa babaw is simple lang:
 First is above sa “do” where dira nimo ibutang imong mga declarations or
initializations, as for the example above kay ang declaration are the password and
guess which is a string type function.
 Next is the “do” where you write your code para mao ang I run una usa I check ang
condition. And isa sa sulod sa “do” is the “while” loop.
 Sulod sa while loop is the condition that need ma meet and that is if
guess!=password. If guess is not equal sa declaration sa password then the code will
run again to ask the password.

So what do you mean sa process do while loop man? Sa example above is simple lang. the code will run
and then check if ang condition is true ba. So if the condition is true then the code will run again then
check sa condition after mag run. Guess!=password; while guess is not equal sa value sa password mag
run balik gihapon ang imong code until ma meet na ang value sa password.

You might also like