Topic_07 : "if-else" - AI's beginning point

Hi

 

It was a long time back, since I made any update in my blog.

 

This time, I'm introducing "if-else" clause in C++ programming.

 

Of course, it is very senseless to teach AI(Artificial Intelligence) to a learner of C/C++ programmer.  But, still, this page has it's very existence.

 

Ever played a 3D game, where you will be surrounded by enemies?

 

Ever thought how computers are able to take a decision if the enemy should attack you or run away from you??  All this decision making capabilities are a part of a bigger branch of computer science, known as Artificial Intelligence.  (This happened to be my favorite subject in my B.Tech, and also, the one and only subject, which I failed in my B.Tech)

 

 

OK!!!  Here is the tutorial....

 

Suppose that, you are interested in writing a program, that is capable of making comments on the user's decisions.  For example, consider the following program........

 

# include <iostream>

void main( )
{
    int decision;

    std::cout << "A Lion is 10 feet away from you";

    std::cout << "Enter 1. and press enter to fight with it";

    std::cout << "Enter 2 and press enter to run away";

 

    std::cin >> decision;

 

    if(decision < 2)

       std::cout << "Vow!! you are a brave person!!"

}

 

Got any idea about the "if" word in our program????

 

In fact, we should check if the value of decision is equal to one or not.  Since I has not introduced the comparison operator so far, I happened to use the less than operator.

 

A more correct looking program should use a line like..

 

if(decision == 1)

 

instead of

 

if(decision < 2)

 

in the above program.

 

Note that, the "==" operator is different from "=" operator.

 

If we would like to do more than one line of work in our program, after an if condition, we can do so by grouping them in between flower brackets.  For example..

 

# include <iostream>

void main( )
{
    int decision;

    std::cout << "A Lion is 10 feet away from you";

    std::cout << "Enter 1. and press enter to fight with it";

    std::cout << "Enter 2 and press enter to run away";

 

    std::cin >> decision;

 

    if(decision < 2)

    {

       std::cout << "Vow!! Either you are a brave person!!"

       std::cout << "Or U r over estimating your strength";

    }

}

 

Suppose that the user selected not to fight with the lion.  In such a case, we will handle the "ELSE" part of the condition.

This is something like... "If it rains, I don't water the plants; else I need to water the plants".  Here is another example....

 

# include <iostream>

void main( )
{
    int decision;

    std::cout << "A Lion is 10 feet away from you";

    std::cout << "Enter 1. and press enter to fight with it. ";

    std::cout << "r other number & press enter to run away";

 

    std::cin >> decision;

 

    if(decision == 1)

    {

       std::cout << "Vow!! you are a brave person!!  "

       std::cout << "Or u r over estimating your strength";

    }

    else

    {

       std::cout << "Hehehehe!!! It's a toy lion.  ";

       std::cout << "You don't seem to be the bravest person";

    }

}

 

Please note that, an if block can exist, without an else block, but an else block can never exist, without an if block.

 

It is also possible to have a group of decisions.  For example..

 

# include <iostream>

void main( )
{
    int decision;

    std::cout << "A Lion is 10 feet away from you";

    std::cout << "Enter 1. and press enter to fight with it";

    std::cout << "Enter 2 and press enter to run away";

 

    std::cin >> decision;

 

    if(decision == 1)

    {

        std::cout << "It's weight is 500 kgs.  ";

        std::cout << "Would you still like to fight????"

        std::cout << "Enter 1. and press enter to fight with it";

        std::cout << "Enter 2 and press enter to run away";

 

       std::cin >> decision;

 

       if(decision == 1)

       {

          std::cout << "Are you Tarzaan????\n";

          std::cout << "or Are you the HULK????";

       }

 

    }

}

 

It is not rare to have a situation, where we need to compare a value with a list of values.  In such case, we need to have a huge sequence of "if-else" phrases.  Here is an example...

 

 

# include <iostream>

void main( )
{
    int decision;

    std::cout << "There is a huge huge coconut tree.\n";

    std::cout << "And there are three animals, nearby.\n";

    std::cout << "1. King Kong.\n";
    std::cout << "2. Monkey.\n";
    std::cout << "3. Gorilla.\n";
    std::cout << "Which will get the banana first\n";

    std::cout << "Enter 1, 2 or 3 & press enter\n";
    std::cin >> decision;

    if(decision == 1)
        std::cout << "U think that strength helps!!";
    else if(decision == 2)
        std::cout << "U think that being fast helps!!";
    else if(decision == 3)
        std::cout << "U think that being clever helps!!";

}

In such cases, you can use an alternative for multiple if-else combinations.  Here is an example.....

# include <iostream>

void main( )
{
    int decision;

    std::cout << "There is a huge huge coconut tree.\n";

    std::cout << "And there are three animals, nearby.\n";

    std::cout << "1. King Kong.\n";
    std::cout << "2. Monkey.\n";
    std::cout << "3. Gorilla.\n";

    std::cout << "Which will get the banana first\n";
    std::cout << "Enter 1, 2 or 3 & press enter\n";
    std::cin >> decision;

    switch(decision)
    {
       case 1:
        std::cout << "You are a stupid.";
       break;

       case 2:
           std::cout << "You are a fool.";
       break;

       case 3:
          std::cout << "You are an idiot.";
       break;
    }
   std::cout << "Coconut trees don't produce bananas";

}


It is also possible that, the user might have entered a number other than 1, 2 or 3.  It is possible to handle such cases, by adding a default case.  Here is an example.....

# include <iostream>

void main( )
{
    int decision;

    std::cout << "There is a huge huge coconut tree.\n";

    std::cout << "And there are three animals, nearby.\n";

    std::cout << "1. King Kong.\n";
    std::cout << "2. Monkey.\n";
    std::cout << "3. Gorilla.\n";
    std::cout << "Which will get the banana first\n";

    std::cout << "Enter 1, 2 or 3 & press enter\n";
    std::cout >> decision;

    switch(decision)
    {
       case 1:
        std::cout << "You are a stupid.";
       break;

       case 2:
           std::cout << "You are a fool.";
       break;

       case 3:
          std::cout << "You are an idiot.";
       break;

       default:
          std::cout << "OOOPS!!!! Sorry Sorry!!\n";

         std::cout << "In fact there was another animal\n";
          std::cout << "Sorry once again for ignoring you\n";            
       break;
    }
   std::cout << "Coconut trees don't produce bananas";

}

NOTE: a break after each case mentions the end of the block.  Try removing break in the above program and check what happens.