0

Loops and conditions

Hello can someone help me to understand what loops and how they work.

18th Jun 2025, 12:15 PM
Manuel Otu
Manuel Otu - avatar
5 Respostas
+ 4
Hi Manuel Otu , you haven't written which language you'd like to learn in, so I'll give you a general idea. Say you wanted to output a message 3x. You could write the line of code 3x and that would work. It takes a bit more effort though. You can use loops to repeat the code for you. They work by having something to check, like the amount of times it has repeated. When that amount is reached, the loop stops.
18th Jun 2025, 12:31 PM
Ausgrindtube
Ausgrindtube - avatar
+ 4
A loop is just a way to repeat an action over and over either for a set number of times or until something specific happens. Example: Climbing Stairs If you're climbing 20 stairs: You take one step. Then another. You repeat “step up” until you reach 20 stairs). You don’t plan each individual step, you just repeat the action until a condition is met (reaching the top). That’s a loop.
18th Jun 2025, 1:15 PM
Vaibhav
Vaibhav - avatar
+ 3
As you started "Coding Foundations", I will use Python code to explain. In general, a loop means the same operation got repeated. It can be specific number of times (for loop), or stop the repetition when a condition is not True anymore (while loop). To print number from 0 to 5 For loop: for i in range(5): print(i) "i" is a throwaway variable, you can name it anything you want. The code means in the range of 5, aka 0, 1, 2, 3, 4, print the first number, then the second until the end. And each repetition, each number is assigned to "i" one at a time. While loop: x = 0 while x < 5: print(x) x = x + 1 In this example, there is a counter "x" which control the while loop. Each repetition the program does 2 things. A) print the variable "x"; B) increase x by 1. It will keep printing variable x "While x is less than 5". So after printing 0, x is increased by 1, thus 1. Since 1 is less than 5, it runs the loop. After printing 4, x, which is 4, is increased by 1 and become 5. Then in the next repetition, since 5 is not less than 5, the loop stopped.
18th Jun 2025, 12:41 PM
Wong Hei Ming
Wong Hei Ming - avatar
0
Thank you very much
18th Jun 2025, 1:04 PM
Manuel Otu
Manuel Otu - avatar
0
Loops are a fundamental concept in programming that allow you to execute a block of code repeatedly for a specified number of times or until a certain condition is met. They help automate repetitive tasks, making your code more efficient and easier to maintain. Types of Loops: 1. For Loop: Used to iterate over a sequence (like an array or list) or a range of values. It executes the code block for each item in the sequence. 2. While Loop: Continues to execute the code block as long as a specified condition is true. It stops when the condition becomes false. 3. Do-While Loop: Similar to a while loop, but it guarantees that the code block will be executed at least once before the condition is checked. How Loops Work: 1. Initialization: You set up the loop by defining the starting point or condition. 2. Condition Check:Before each iteration, the loop checks if the condition is met. If true, the code block inside the loop is executed. 3. Execution: The code block within the loop is execute https://sololearn.
18th Jun 2025, 5:49 PM
Mehrab