Wednesday, July 21, 2010

C++: the "if" statement - helping computers make decisions

How can we have a different response from a computer if we type something?
How can we allow a computer to distinguish between negative and positive
numbers? It all comes down to two words: conditonal statements. In this
context, a "conditional statement' is the code that allows a computer to run
part of a program that correspond to that particular condition. For
instance, if we ask our computers to tell us if a number is odd or even, it
would give different responses (run different parts of a program) depending
on if we type odd or even numbers.

The starting point of our discussion begins with the word "if". We ask all
sorts of questions with the word "if". For instance, we would ponder, "if I
eat now, I won't get hungry," or "if I fix a bug in a program, a new bug
would emerge." The second question would be more relevant here, since we are
dealing with a silicon chip to do our bidding (I personally call it
"fighting between myself and my computer"). C++ is not an exception (well,
if we say so), and the word "if" is used as a keyword to denote conditional
statement.

For example, suppose if we want to see if a person is allowed to vote based
on his or her age. A psudo code (the English version) would look like:
First, declare an integer variable called "user_age." Then have the user
enter his or her age. if the age is 18 or older, tell the user that he or
she is eligible to vote (Source: 26th amendment to the United States
Constitution). If not, tell the user that he or she is not eligible.
This is called an algorithm - a precise way of doing things to perform a
task. The thing to be evaluated is the user's age and the condition is if
the number is above or equal to 18 or not.

Can you guess how this is done in C++? it would look something like this:
// The program to test user's age for vote eligibility.
int user_age; // The age of the user.
cout << "Please type your age:" << endl;
cin >. user_age;
// Now, the conditional statement...
// If the age is older or equal to 18:
if (user_age >= 18)
{
cout << "You are eligible to vote." << endl;}
// If not...
else
{
cout << "Sorry, you cannot vote." << endl;}

Notice what we have here - almost the same as English version. First, the
program asks the user to type his or her age. Depending on the number the
user typed, it would display the first message if the age is greater-than or
equal (<=) to 18, otherwise it would print out the second message. We only
need these two conditions since we are dealing with one condition - if a
number is greater than or equal to something or less than that.

So, according to syntax, an if statement would have an else statement. There
are variants of this, including testing for one than one condition or having
no 'else" statement at all in the middle (just bunch of if's before the
ending else). For instance, suppose if we ask the user to type a number and
tell the computer to print out different responses based on the following
conditions:
* If a number is positive, say, "Greater than jackpot."
* If the number is negative, say, "less than jackpot."
* If a user types zero (0), say, "jackpot!"
So, if we turn this into C++, a variant of the "voter age" code would come
out, something like:
// The Jackpot program to see if a user types zero or not.
// The usual question and input phase. For this one, we'll use an integer
called "user-guess".
if (user_guess < 0)
{
cout << "Well, less than jackpot." << endl;}
else if (user_guess > 0)
{
cout << "Well, more than jackpot." << endl;}
else
cout << "Jackpot!!!" << endl;

There are reasons why i wrote this kind of code. First, it is advisable to
put braces ({, }) when writing the portion of the code that will run when
that condition is met e.g. see the first two conditions. For one-liners like
the third condition, you don't really need to, but i think for clarity, try
using braces as you've seen (especially if you are using braille devices and
screen readers). Second, for more than one condition, put "else if" in front
of the subsequent conditions (exception being the last one, which you can
just put "else").

There is a slight variant of this code without the middle else statements,
like:
if (first condition)
{
// run the code associated with it.}
if (second condition)
{
// Run the associated code.}
if (subsequent condition)
{
// Run that portion of program.}
else
{
// Something entirely new...}
Or a variation of these three versions.

The conditional statements are used to "branch", or run a different segment
of a program based on a condition. There is another way of doing that which
is slightly more compatible with English way of writing conditions. This is
called "switch" statement, which is another form of if using cases. But for
sake of space and time, I will not go over this one in detail - I'll mention
it towards the end, as the switch statement is related to the next topic...
// JL

No comments:

Post a Comment