+ 8
For loops please explain them in simple terms
I've been trying to get a clear understanding of for loops, can someone please explain how for loops work, I've tried looking at various codes so as to get clarity but alas i get even more confused for example I see those pyramids, the number, stars etc but cant figure it out how its done, in quizzes i just do guesswork but for me to be a good coder i need to know how its done and do it myself. if anyone has links to lessons that will clarify things for me please send the links. thank you all
11 Respuestas
+ 6
This is how i learned:
usuly, we need to use some variable to use 'if' statement
example:
a = 10
if a < 20:
print(a)
output(10)
in the case of 'for' loops you gone use 2 arguments, but just one will be some variable
example:
variable = [1,2,3,4,5]
for item in variable:
print(item)
what is a loop do? repeat!
#first time
for 1 in list:
print(1)
#second time
for 2 in list:
print(2)
This will print the values in the list variable.
You can print or can get the index of this values.
For this u gona use range(a, b) method
In this case:
variable = [1,2,3,4,5]
for index in range(0, len(variable)):
print(index)
#first time
for 0 in range(0, 5):
print(0)
#second time
for 1 in list:
print(1)
why is that? because indices of lists and tuples starts at 0 and goes to len(list)-1
+ 7
If you are confused with patterns, I suppose your main concern would pertain to nested for loops (or just loops in general). If you can give an example, or specify where you are stuck with handtracing, we would be able to help you with it.
I'm sure you understand how a for loop works.
for (initialization; condition; increment) {
// code execution
}
E.g.
for (int i = 0; i < 10; i++)
std::cout << "Hello Loops!" << std::endl;
The code above would print "Hello Loops!" for ten times. Things get trickier when the value of i is involved in the loop body:
for (int i = 0; i < 10; i++)
std::cout << i << std::endl;
This code would print values 0 to 9. Now, what happens if we nest two similar loops together?
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
The best way to understand what happens, would be to print the content of i and j for each loop iteration, until the loops are terminated.
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
std::cout << i << " " << j << std::endl;
Sample output:
0 0
0 1
0 2
0 3
// some 90+ lines later ...
9 7
9 8
9 9
You should be able to observe that the inner loop runs 100 (10*10) times exactly. This is because for each outer loop iteration, the inner loop runs 10 times. This forms the basis for a lot of pattern creation that you have mentioned. By understanding what happens in the loop, it shouldn't be too hard to make sense of why patterns form themselves.
+ 5
A for-loop consists of three main things.
1. The start value (i=0)
2. The condition (i<10)
3. An action. (i++)
Imagine you have a shopping list and you want to check if every wanted item (i) is in your cart.
i1 - Carrots
✓
i2 - Meat
✓
i3 - Rice
✓
i4 - Brocolli
✓
i5 - Garlic (Yep, is there)
✓
for (first item; til the last item; make a ✓)
Edit:
In Python it’s the same process but different syntax.
i i i i i
1| list=[1,2,3,4,5]
2| for i in list:
3| print(i)
————————————
Line 2| combines start value and condition
Line 3| The action that you want to apply
Hope I could help.
+ 4
Hlo everyone,im here to tell u that there is also one more kind of loop which is the variation of for loop for the iteration of array in c++,see the code below:
https://code.sololearn.com/cBqEMh7dECZJ/?ref=app
+ 3
Loop is a statement used to repeat a block of codes
+ 3
thanks a lot guys your explanations have helped clear my confusions
+ 3
Max . thankyou it helps.
+ 3
Hlo Buhle..if u have studied loops then i'll must say u should also know about what r foreach loops..so i have derived foreach loop in c++ using #define preprocessor directive
https://code.sololearn.com/cl5dRAoh6kaa/?ref=app
+ 1
Ayedun Olakunle A. Last time I was confused because they used x instead of i in Java.
The hard part is just the right Syntax. In Python no initialization and you use : instead of curly braces. PHP weirdly uses $ all of a sudden.
0
To be honest, I'm struggling with for loops it keeps confusing me everytime