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

In the previous posts, we learned very beautiful things of which the most beautiful is game development.

We have covered if-statement, if-else statement, if-else-if-statement, switch-statement, while-statement and do-while statement but there is another very important and most frequently used conditional statement called as the for-statement. It is a bit more complex but neat and short.

Following is the code to which you are already familiar with:

#include <iostream>
using namespace std;

int main()
{
  int loopCounter = 0; //Declaring and initializing a loop counter

  while(loopCounter < 10) //Condition (related with loop counter)
  {
    //All looping codes

    loopCounter++; //Changing loop counter
  }

  return 0;
}

Now, here is the code which is exactly the same as the above code but is neat and short:

#include <iostream>
using namespace std;

int main()
{
  //for(Declaring and initializing loop counter;
  //Condition (related with loop counter);
  //Changing loop counter)
  for(int loopCounter = 0; loopCounter < 10; loopCounter++)
  {
    //All looping codes
  }

  return 0;
}

for loop requires two semi-colon. Leaving them empty i.e for( ; ; ), will cause an infinite loop. Here is the format of for-statement for a quick overview:

for( Initializing variable(s); Condition(s); Changing variable(s) )
{ -Codes to be executed- }

Remember that each variable has a limited scope after which it is destroyed or deallocated from the memory and can not be used after that. Its scope is limited only to its block (or space between any two flower brackets). Hence, there is a definite difference between the loopCounter variables of while and for loops mentioned above.

The one used in the while-statement can be called after the while-statement’s block. The other used in the for-statement can not be called after the for-statement’s block.

It will not effect anything if loopCounter++; or ++loopCounter; is used in the for-statement.

Getting used to such small differences will lead to a better programming style, lesser errors and of course, better programming. Now, you have to use for-statement(s), cout << "*";, cout << " "; and cout << endl; in the best way possible to have that output:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Good luck! And come back if there are some doubts…

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

Argghhh! When will the topic of conditional statement gets over? Well, we have to be patient while learning something. I hope that you might have done the home task of the previous post. Just keep trying to code things up and try to implement small ideas with your skill, you will see yourself learning and improving!

Below is some similar looking code that we discussed in the previous post:

#include <iostream>
using namespace std;

int main()
{
  //Guess the number game!
  while(value != 7)
  {
    cout << "Enter the value: ";

    int value;
    cin >>; value;
  }

  cout << endl << "Horray! You guessed the number!";

  return 0;
}

Yeah! This time we programmed a very simple game! Guess the number! Sounds cool… Right? No! Why not? At least, we can call this our first GAME! At least, we started learning game development!

Try to run that code! Is it running fine? No! What the compiler said? Oh! It looks very hard… Somewhere, in the output log, it might be written that:

'value': undeclared identifier

Now, what it means? it means that the variable, value is not known to the compiler. Just see the code. The condition in the while-statement comes before the value being declared. Solution can be this:

#include <iostream>
using namespace std;

int main()
{
  //Guess the number game!
  cout << "Enter the value: ";

  int value;
  cin >> value;

  while(value != 7)
  {
    cout << "Enter the value: ";

    //Say No to: int value; Reason: already declared above.
    cin >> value;
  }

  cout << endl << "Horray! You guessed the number!";
  return 0;
}

Even better and a very neat solution is this:

int main()
{
 //Guess the number game!
 int value;
 do {
 cout << "Enter the value: ";
 cin >> value;
 } while(value != 7); //Semicolon is needed here!

 cout << endl << "Horray! You guessed the number!";

 return 0;
}

The code in the do block is first executed even if the condition in the while is not true. Then, it will only be repeated if the condition remains true. Very simple! Also, in this way, the condition will not get the garbage value of uninitialized but declared value as the do code give some value to value i.e initializes value.

Looping plays a key role in game development. Such a loop in which program updates logic and screen of a game is called game loop. Game loop is the heart of any game. A game loop may looks like:

bool quit = false;
while(!quit) //same as "quit == false"
{
  //> > > Polling Events
  //if(quit event happens)
  //  quit = true;
  //else if(key == arrow x)
  //  move player to direction = x;

  //> > > Updating Game Logic
  //if(enemy hits player)
  // player hp--;
  //else if(player hits enemy)
  // enemy hp--;

  //> > > Updating Screen
  //clear screen
  //render enemy
  //render player
  //update screen
}

In future, we will get to this point. But for now, this is a minimal view of a general video game.

Let us talk about another home task. This time it is not a joke! Make a program that takes interview of a user having such questions that have only two answers i.e Yes or No. On the basis of Yes or No of a question, change the route of questioning. In this way, make a hierarchy or tree kind of thing. If possible, comment that code here so, that I can review it! Also, try some experiments with the code in this post and understand it. To learn something like programming, it is necessary to be interested in it…

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

In the last few previous posts, we learned some of the conditional statements, user input and basic arithmetic. Now, time to use some bits of all of them in one place!

Just like if-statements, there is another conditional statement called as while-statement. Below is a very simple example of this:

while(true)
{
  cout << "Looping..." << endl;
}

When you run this code, you might be wandering that why the program is not stopping? Well, it’s because it is an infinite loop. After the while keyword, there is a condition just like that of if-statement. The code in the braces, will keep repeating itself while the condition after the while is true. Ofcourse, you got the meaning! It is just like a recurring if-statement.

We can do some interesting things with it like this:

int loopCounter = 0;
while(loopCounter < 10)
{
  cout << "Looping " << ++loopCounter << endl;
}

When we run this code, we see that the word “Looping” along with a increasing number, is printed 10 times. The increasing number starts to print from a value, 1. It will start from a value, 0, with postfix increment operator(loopCounter++). We can also do loopCounter = loopCounter + 1; or loopCounter += 1; at the end or start of the braces of while-statement.

Can you think of some program that uses while-statement along with getline(user input)? Password input! Check out the code below:

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string password = "kabrakadabra";
  string entry = "NULL";

  while(entry != password)
  {
    cout << "Enter your password: ";
    getline(cin, entry);
  }

  cout << "Correct password!" << endl;

  return 0;
}

What if we want to say wrong password each time it is entered wrong! Then, below the getline, we can add:

if(entry != password)
{
  cout << "Wrong password! Please try again!" << endl;
}

Don’t you think that it’s perfect? Woohoo! we are going to be true programmers in the future, don’t we? Yes, but only if you do few experiments with it as a home task!