Introduction to decision and loops


1.   Decisions and loops

Now that we have covered the basics of programming we will look and learn how to add decision-making capabilities to you C++ program. You will also learn how to make programs repeat a set of actions until a specific condition is met. This procedure in math and programming is called iteration procedure because the action in the program is repeating itself until the value of a variable is reached. This will enable you to handle variable amounts of input, as well as make validity checks on the data that you read in. Decision and loops are very important because they will enable you to write a program which can adapt their actions depending on the input data. So if you enter specific value of a variable the program will adapt and make decisions depending on this variable.

1.1.   Command block

The parts of a program which conditionally execute or execution of a program is repeating are grouped into command blocks. These command block contains one or more commands which are bounded by a pair of curly braces {}. From the outside this block behaves as a single entity. You have already met with the command block, and this block is the function main ( ).The command block is usually written indented. This retraction/indention is made exclusively for visibility.  
Very important feature of a block is that the variables are declared in block are only visible inside this block. Let’s look the following code:
#include <iostream>
using namespace std;

int main ( ) {
{
    int a = 1;
    cout << a << endl;
}
return 0;
}






 // ERROR: variable a doesn't exist



In this code we have declared a integer a and we have assigned value of 1 to this integer. If you debug this code the debugging of the code should be successful and the program should work unless you have made some syntax error while typing this program code.  This declaration of variable a is made inside the block of a function main so the program code is correct. Now let’s look another example.
#include <iostream>
using namespace std;

int main ( ) {
{
    int a = 1;
}
    cout << a << endl;
return 0;
}










If you type this code and start debugging this code the program will give you an error message because the variable a, which you are trying to display, doesn’t exist. The conclusion is that you have to declare the local variable inside the command block. The advantage of this procedure is that you can declare the local variable inside the block with the same name for example a which is declared outside the block. Then the local variable which is defined inside this command block will obstruct the homonymous variable which is declared earlier outside this command block. Let’s look another example.
#include <iostream>
using namespace std;

int main ( ) {
    int a = 5;
   {
        int a = 1;
        cout << a << endl;
    }
    cout << a << endl;
return 0;
}







// will display 1

// will display 5

The first command for displaying will retrieve a local variable a = 1, since this variable has the advantage over the same variable a = 5, which is declared before the command block. Upon leaving the block local variable a is lost, and the first variable a = 5 is now available. If we declare one more homonymous variable after the command block it would result in error because we already have the first variable.  
Share on Google Plus