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 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…

Advertisements

C++ Programming Series: Using Conditional Statements (Part 3)

In the last two previous posts, you got through the if, else and else-if-statements. The uncertain point is the difference between two if-statements and if-statement with an else-if-statement.

Two if-statements are not connected to each other. They don’t have any relation with each other. The compiler have to pass through all of these statements even if anyone of the conditions is true. Example shows three if-statements:

int value = 6;
if(value > 7)
  cout << "value is bigger than 7" << endl;
if(value > 5)
  cout << "value is bigger than 5" << endl;
if(value > 3)
  cout << "value is bigger than 3" << endl;

Output:

value is bigger than 5
value is bigger than 3

An if-statement with an else-if statement are connected to each other. If anyone of the conditions gets true, all others are skipped. Same example below with some corrections:

int value = 6;
if(value > 7)
  cout << "value is bigger than 7" << endl;
else if(value > 5)
  cout << "value is bigger than 5" << endl;
else if(value > 3)
  cout << "value is bigger than 3" << endl;

Output: (Now, there is no unnecessary outputs. It is pretty nonsense to say that value is bigger than 3 when we already said that value is bigger than 5)

value is bigger than 5

This is very important when it comes to getting user input and then, manipulating it for some change in the output. For example, there is a character variable or variable of type, char called key which takes the character (or letter) that you press on the keyboard and in return, print something for you. (Remember, put single inverted commas and not double, for a character or their variables only)

if(key == 'a')
  cout << "Alpha!" << endl;
else if(key == 'b')
  cout << "Beta!" << endl;
else if(key == 'c')
  cout << "Charlie!" << endl;
else if(key == 'd')
  cout << "Delta!" << endl;
else
  cout << "Unknown input..." << endl;

For such cases, it is better to use another conditional statement called as switch-statement. It is only good for comparison done with equality operator (i.e, ==) and numbers.

switch(key)
{
case 'a':
  cout << "Alpha!" << endl;
  break;
 
case 'b':
  cout << "Beta!" << endl;
  break;
 
case 'c':
  cout << "Charlie!" << endl;
  break;
 
case 'd':
  cout << "Delta!" << endl;
  break;
 
default:
  cout << "Unknown input..." << endl;
  break;
}

This code do the same thing as the above one. You may have figured out some of the things by yourself. But let me explain this.

After switch, we have to type in the variable in the round brackets unlike if-statement. Then, we need to put a case with the value that is to be compared with the variable. At the end of case-value pair, we need to put a colon. Then, put one to many lines of code before the ending flower bracket. What? Did we put a starting flower(or curly) bracket? No! So, that means… there is some other way to put an ending to it. The break keyword is only for switch and loop statements. It is used to skip all the codes below it and is used to skip the whole loop, not just a block (You will understand this later if not now). The default keyword is another case which acts like the else-statement and works only when all the above cases are false.

Want some shortcut to understand switch-statement? Here is the format:

switch( [variable] )
{
case [value]:
  // to be executed when variable's value is equal to that of case's value
  break;
  // ...
// [more cases]
  // ...
default:
  // to be executed when variable's value is not equal to any of the above case's value
  break;
}

Pretty long but looks useless! Nah! These are the basis of all beautiful things that you can imagine to do on the screen…