I have problems understanding loops

Nov 6, 2020 at 6:32am
I learn c++ at the school and i have problems understanding loops, how can i learn them easier, please help
Last edited on Nov 6, 2020 at 6:43am
Nov 6, 2020 at 8:00am
Start simple. Use it to print hello world 7000 times.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
int main()
{
    int i = 7000;
    while(i > 0)
    {
        cout << i << ". Hello world | ";
        i --;
    }
}


And that's 6996 lines of code that you did not have to type.
Next try to apply it to code that you've already written that you think had things that were somewhat repetitive. The variable i is called a sentry variable and when it reaches the condition that we put in the while() argument, it will break the loop and the program continues from where it exits.
Really just start using loops in small ways and you'll pick it up pretty quickly from there.
There are a couple of loop types, but get comfortable with while() before moving on to the others.

For a much better explanation, check out this YT video by Bucky https://www.youtube.com/watch?v=KLKhsaOPnLk&list=PLAE85DE8440AA6B83&index=18
Last edited on Nov 6, 2020 at 8:18am
Nov 6, 2020 at 8:38am
Thank you
Topic archived. No new replies allowed.