Loops Notes in C
Loops Notes in C
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.
WHILE LOOP
- Simple loop.
- Codes happen after the condition
- Used when you don’t know how many times the loop will be executed.
- Indefinitely
#include <iostream>
int main() {
int i = 1;
while (i <= 5) {
std::cout << "Number: " << i << std::endl;
i++;
}
return 0;
}
Explanation:
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}
#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;
}
- 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.
do {
// Statements to be executed
} while (condition);
Example:
#include <iostream>
int main()
{
int i = 1; // Initialize the counter variable
return 0;
}
Explanation:
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>
int main()
{
string password = "hihello";
string guess;
do
{
cout << "Guess the password: ";
cin >> guess;
} while (guess != password);
return 0;
}
- 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.