Bold Aesthetic Creative Games

An Indie Game Studio Developing Bold, Aesthetic & Creative Video Games

C++ Programming Series: Using Conditional Statements (Part 7, Last)

C++ Programming Series: Using Conditional Statements (Part 7, Last)

In this post, we are going to learn the keyword, break. Are we not familiar with this keyword before? Yes, you got it right. We already learned it when we learned the switch-statement where we use break keyword after every case’s code.

The break keyword is only used for stopping loops and ending a switch statement. Now, we know that looping can only be done by while-statement and for-statement. So, break keyword is only used within switch-statement, while-statement(do-while as well) and for-statement.

Here, in the main function, it is used with all three conditional statements.

int value;
cin >> value;

switch(value)
{
    case 2:
        cout <<; "Alpha" << endl;
        break;

    //more cases

    default:
        cout << "Out of my mind!" << endl;
        break;
}

while(true) //an infinite loop
{
    if(value > 100)
    {
        cout << "break keyword makes while finite from infinite" << endl;
        break;
    }
    else
    {
        cout << "while looping " << value << endl;
    }

    value += 25;
}

for(value = 0; value < 100; value += 10) //a finite loop, value is already declared
{
    if(value >= 40)
    {
        cout << "exiting for loop when value gets bigger or equal to 40" << endl;
        break;
    }
    else
    {
        cout << "for looping " << value << endl;
    }
}


If value inputted, is 2 then, output will be:

2
Alpha
while looping 2
while looping 27
while looping 52
while looping 77
break keyword makes while finite from infinite
for looping 0
for looping 10
for looping 20
for looping 30
exiting for loop when value gets bigger or equal to 40

Try to understand the code with the reference to the output. What do you get? The break keyword stops the loop before the condition of the statement is falsified.

Now, there is another keyword for controlling loops. It is called continue. It do not stops the loop but it actually, skips the code after the continue keyword and then, returns to continue looping. The continue keyword is used in while-statement(do-while as well) and for-statement.

In the main function:

int i = 0;
do {
    i++;
    if(i == 5)
        continue;

    //code below continue will not run when i is equal to 5
    cout << "while looping " << i << endl;
} while(i < 10);

for(i = 0; i < 20; i++)
{
    if (i % 2 == 0) //if i is an even number
        continue;

    //code below continue will not run when i is an even number
    cout << "for looping " << i << endl;
}

We will not see, “while looping 5” and “for looping -even number-” because of the continue keyword.

Now, we already know how to compare things of the same type. Below is an example of comparing two floats.

float fl1 = 0.1;
float fl2 = 0.1;

if (fl1 == fl2)
    cout << "Floats are equal!" << endl;
else
    cout << "Nah! unequal..." << endl;

if (fl1 == 0.1)
    cout << "True" << endl;
else
    cout << "False" << endl;

if (fl2 == 0.1)
    cout << "True" << endl;
else
    cout << "False" << endl; 


It looks normal but its output is unusual.

Floats are equal!
False
False

Can you think of some reason for the fl1 and fl2 not equal to 0.1 but both are equal to each other? No. Just try to make one. It is actually, a simple one.

I ask you, is 0.1 with which we are comparing floats, a float or a double? It is a double. We are comparing a float to a double. Float has precision of 7 decimal places whereas double has precision of 15 decimal places. There is a rule in C++ and that is, comparing types of different precision will often result in unexpected output.

That doesn’t mean that comparing long int and short int will give unexpected output because both long int and short int can have smallest positive value, 1. But float can have smallest positive value, 0.000001 whereas double can have smallest positive value, 0.00000000000001 i.e both have different precision.

Now, we may need some good reason to understand it. And there is a good reason for that! This reason is based on the fact that when a variable is not initialized (or given some value), it will have the garbage value or the value present in the memory at that instance. To compare a float with a double, the float is converted to a double, temporarily i.e its precision will become the same as that of double.

In other words, for the above case, when we initialized float, its value is:

0.100000

But when it is converted to double while comparing it with a double, its value is not initialized for decimal places after 7 and so, decimal places after 7th one, is filled with garbage values. Its value may become:

0.10000020483009 (consider 20483009 as garbage values)

Now, I ask you, is the value 0.10000020483009 equals 0.10000000000000? Of course, they are not equal! I hope that you got it right.

The solution for the above case is really simple and you may know that. Just put an f after double values so, that compiler recognizes it as a float instead of a double.

float fl1 = 0.1f;
float fl2 = 0.1f;

if (fl1 == fl2)
    cout << "Floats are equal!" << endl;
else
    cout << "Nah! unequal..." << endl;

if (fl1 == 0.1f)
    cout << "True" << endl;
else
    cout << "False" << endl;

if (fl2 == 0.1f)
    cout << "True" << endl;
else
    cout << "False" << endl;


Now, we get a proper output!

Floats are equal!
True
True

Phew! At last, ‘Using Conditional Statements’ came to an end! Really, it looks like a series in a series! But it is an extremely important topic! We must know and understand every inch of this topic otherwise we can’t proceed well…

Leave a Reply

Your email address will not be published. Required fields are marked *