Easy Tutorial: Computer Programming for DUMMIES (not language specific) -- beginner friendly

Here is another tutorial for you guys! This tutorial is also not language specific. Once you learn programming in one language, you can kind of learn other languages very easily. Having that said, this tutorial specifically can be applied to most languages. It will be pretty much exactly the same for a lot of popular languages (C++, C, Java, etc...). This tutorial series is in C++, one of the easier languages to start off on. This will be part 5. If you want to catch up, check these out:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

This tutorial is about loops. Loops are bodies of code that perform a specific task multiple times. Programmers found that they may need to repeat specific tasks up to hundreds, or even thousands of times. Loops make this very easy! These may be a little bit confusing to someone who isn't used to programming, but I will walk you through it!

for() loops

for() loops are set up similar to an if statement (the word for, the parameters, then the body inside curly braces). Inside the parenthesis (after the word for), there are three parts. The first part is where you initialize variables (you do not have to have anything here as long as a semicolon is still present). The second part is the condition. This is just like an if statement; the body is executed if the condition evaluates to true. The loop will repeat until that condition is false. The third part is where you can update any variables. This is executed after every loop. Like the first part, you don't need to put anything here, just a semicolon. Here is what it looks like:

for (init; condition; increment)
{
        /*    do something    */
}

More specifically...

for (x = 1; x <= 10; x++)
{
        cout << x << " ";
}

Output will look like:

1 2 3 4 5 6 7 8 9 10

Side note: If you didn't know yet, the operator '++' will increment the value behind it by 1 (x in this case)

Let's walk through this loop:
-1. x is set equal to 1
-2. the loop checks to see if x is less than or equal to 10 (1 <= 10 --> true)
-3. the loop prints x and a space (1 )
-4. the loop increments x by 1 (x = 2)
-5. the loop checks to see if x is less than or equal to 10 (2 <= 10 --> true)
-6. the loop prints x and a space (2 )
-7. the loop increments x by 1 (x = 3)
.... this goes on....
-29. the loop checks to see if x is less than or equal to 10 (10 <= 10 --> true)
-30. the loop prints x and a space (10 )
-31. the loop increments x by 1 (x = 11)
-32. the loop checks to see if x is less than or equal to 10 (11 <= 10 --> false)
-33. the loop ends

Here is a program I wrote on for() loops to give you a better grasp:

#include<stdio.h>
#include<iostream>
using namespace std;

int main()
{
    cout << "Even numbers from 0 to 10: ";
    for(int x = 0; x < 11; x = x+2)
    {
        cout << x << " ";
    }
    cout << endl << endl;

    cout << "Odd numbers from 0 to 10: ";
    for(int x = 1; x < 11; x = x+2)
    {
        cout << x << " ";
    }
    cout << endl << endl;

    cout << "Counting backwards: ";
    for(int x = 10; x > 0; x--)
    {
        cout << x << " ";
    }
    cout << endl << endl;

    for(int x = 0; x < 5; x++)
    {
        cout << "for() loops repeat things!\n";
    }
    cout << endl << endl;

    return 0;
}

Here is the output:

[cmw4026@omega test]$ g++ for.cpp
[cmw4026@omega test]$ ./a.out
Even numbers from 0 to 10: 0 2 4 6 8 10

Odd numbers from 0 to 10: 1 3 5 7 9

Counting backwards: 10 9 8 7 6 5 4 3 2 1

for() loops repeat things!
for() loops repeat things!
for() loops repeat things!
for() loops repeat things!
for() loops repeat things!


[cmw4026@omega test]$

Note: You can increment forwards, backwards, by two, and more. Also, the '--' operator decrements x by one. One last important thing: C++ allows you to declare a variable inside the loop. That variable only exists in that loop.

for(int x = 0; x < 10; x++)

Some languages such as C do not allow that. You have to declare it outside like this:

int x;
for(x = 0; x < 10; x++)

while loops()

while() loops are similar to for loops. They are a bit simpler. It only has one parameter, and executes the body until that parameter is not true. This is what they look like:

while(true)
{
        /*    do something    */
}

Let's walk through an example:

int x = 0
while(x < 3)
{
        cout << x << " ";
        x++;
}

-1. the loop checks if x is less than 3 (0 < 3 --> true)
-2. the loop prints x and a space (0 )
-3. x increments by one (x = 1)
-4. the loop checks if x is less than 3 (1 < 3 --> true)
-5. the loop prints x and a space (1 )
-6. x increments by one (x = 2)
-7. the loop checks if x is less than 3 (2 < 3 --> true)
-8. the loop prints x and a space (2 )
-9. x increments by one (x = 3)
-10. the loop checks if x is less than 3 (3 < 3 --> false)
-11. the loop ends

do while

A 'do while' loop serves the exact same purpose as a while loop, except it always executes the body one time. This is what it looks like:

do
{
        /*    do something    */
} while(true);

More specifically...

do
{
        cout << "You are reading this AT LEAST one time!\n";
} while(x < 5);

Here is a program I wrote to give you a better idea:

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;

int main()
{
    int x = 0;
    cout << "Even numbers between 0 and 10: ";
    while(x != 12)
    {
        cout << x << " ";
        x = x + 2;
    }
    cout << endl << endl;
/////////////////////////////////////////////////////////////////// 
    x = 0;
    do
    {
        cout << "You should see this at least one time!\n\n";
    } while(x == 1);
///////////////////////////////////////////////////////////////////
    string s = "string";
    while(s == "string")
    {
        cout << "contents of s: " << s << endl;
        s = "somethin else";
    }


    return 0;
}

Here is the output:

[cmw4026@omega test]$ g++ while.cpp
^[[A[cmw4026@omega test]$ ./a.out
Even numbers between 0 and 10: 0 2 4 6 8 10

You should see this at least one time!

contents of s: string
[cmw4026@omega test]$

I hope this was useful to you! I want to point again out that I am using a Linux system to compile and run my programs. A lot of people use a program to write and test their code. This is cool for beginners because you don't have to compile it yourself every time just to see if something works! A good program I like is Eclipse. A lot of people like Visual Studio also. Leave any suggestions in the comments! Here is one more important thing you should always remember when working with loops:

Infinite Loops

You have to be careful with loops. If the condition of the loop never evaluates to false, then it will run forever!!! -- until you shut it off... Here is an example I made:

#include<iostream>
using namespace std;

int main()
{
    int x = 1;
    while(x != 0)
    {
        cout << "STOP THIS INFINITE LOOP!!!\n";
    }

    return 0;
}

Here is the output!

STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
STOP THIS INFINITE LOOP!!!
Sort:  

I really like your channel man, definitely following. Steem on, I look forward to seeing them!

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.028
BTC 64182.86
ETH 3531.12
USDT 1.00
SBD 2.53