CPP 07 - While Loop

CPP 07 - While Loop
Di Posting Oleh : Simple Learning
Kategori : C++



Your computer is stupid. By now you realize it will not do anything useful until it is instructed step-by-step what to do. But it's fast. And what amazes me, it can do things over and over again without complaining. 

In this short post, let's make our computer do a dull thing a number of times using so called 'loop' mechanism. There is a number of ways we can perform a task number of times, so here is one of them:

while (expression) run_this_statement;

or, if there is more than one statement you want to execute, you can use the code inside the braces '{}':

while (expression) {
    run_this_statemet;
    run_that_statement;
    and_run_yet_another_statement;
}

Explanation
As long as the expression in the parentheses '()' is true, meaning non-zero value, the statement(s) will be executed. You typically use the 'while loop' when you do not know how many times you will be running the statements. I need to warn you however, that if you do not make the expression in the parentheses FALSE (zero) at certain point, your program will be looped forever. 

Take a look at the code below. The program is looped forever and can only be stopped by pressing CTRL-C (control and c keys simultaneously).

Pic. 1 - Source Code: ex007.1.cpp

Pic. 2 - Result's of Running the Program (Pic.1)

Explanation
Line 8-9
We define variables here.

Line 13-16
The 'while' keyword starts our loop. First, the 'while' statement checks if what's in the parenthesis is equal to ZERO (false) or NON-ZERO (TRUE). If true, all the code in the '{}' is executed top-to-bottom. Then program goes back to the top ('while' statement) and checks the expression in the parentheses is false (zero) or true (non-zero). If non-zero (true: in our code it is still 2), it executes all the commands in the braces '{}' again. This never ends because our variable in the parentheses 'loop_count' is always 2.

A quick fix to that endless loop of our code is to decrement the value of loop_counter variable every time we run the loop. Take a look at the code below:

Pic. 3 - Source Code: ex007.2.cpp

The last line in my 'while' loop is decremented by 1 each time the loop is run. That's why, program is only going to ask my name twice. 


Pic.4 - Result's of Running the Program (Pic.3)

First, loop_counter = 2. After running the code in '{}', the last line says:
loop_counter = loop_counter - 1;

Initial value of loop_counter = 2. This line is going to do simple math: 2 - 1 = 1. The result (1) is going to be assigned to the loop_counter variable, and the code jumps back up to the 'while' statement to check if the code should be run again. Since loop_counter is now 1 (truth), the code in braces is run again. 

The second run of the code will do the math: 1 - 1 = 0. The result(0 = false)) is  going to be assigned to the loop_counter, and the code jumps back up to the 'while' statement to check the value of the expression in the parentheses. Now, the value of loop_counter is zero (false). The code in '{}' is not run (program leaves the loop). The last statement is performed:

Line 20
return 0;
This will finish the program, and you get your command line prompt back.

Homework
Write a code that will ask the user: "How many planets does the Solar System have?
In response, the program should display 9 stars like this:
*********


0 Response to "CPP 07 - While Loop"

Post a Comment