|
|
|
|
Back to Bluesfear |
|
v.
logic and if statements
An
important part of programming is the ability to control the flow of a program.
What use is a program that cannot make any decisions? In this section we'll
discuss how logic is evaluated in C++ and how to use if statements, and also I
will bring up the switch statement.
Logic,
simply stated, is a decision which ends up as either true or false. Do I need a
haircut? True! Have I been writing this for too long? True! Am I a 1337 h4x0r?
False :( True and false are the logic of programming. One thing I should have
said before is that in most cases, false is 0 and true is 1 (if your compiler
doesn't support the keywords true and false). Now, in order to obtain a logic
result, we must have logic operators. We need to be evaluate a statement as
true or false.
The
main logic operators are:
·
==
- equal to
·
<
- less than
·
> -
greater than
·
<=
- less than or equal to
·
>=
- greater than or equal to
·
!=
- does not equal
Say we
had the following code...
#include
<iostream.h>
int
main(void) {
int x = 4;
int y = 6;
int
result1 = 0;
int
result2 = 0;
int
result3 = 0;
result1 = (x == y);
result2 = (x <= y);
result3 = (x >= y);
cout << result1 << endl
<< result2 << endl
<< result3 << endl;
return 0;
}
output
------
0
1
0
The
result of the logical test is placed in each variable. Now, logic can be
branched out to test for multiple boolean results using AND and OR operators.
The symbols for these in C++ are && for AND, and || for OR. An easy way
to think of AND and OR is as * and +. The result of a logic test is either 1 or
0 (true or false) and if you want to predict the result, use the operator to
test.
(5
< 6) && (4 < 2) && (3 == 3)
1
* 0 *
1
0
* 1
0
result
= 0
(1
== 1) || (5 < 7) && (6 > 8)
1
+ 1 *
0
1
+ 0
1
result
= 1
Consider
the following code snippet.
int
x = 4;
int
y = 6;
int
z = 5;
int
result1 = (z > x) && (z < y);
int
result2 = (x < z) || (y < z);
int
result3 = (y < z) || (x < z);
What
do you predict the results will be? The answer is that they all evaluate to
true. One important thing to note in this is that in sometimes the boolean
evaluation will be "short-circuited". In the case of AND, when as
soon as one test is false, the entire result is false and the rest of the tests
are not performed. This is important to know. Consider a modified version of
the above...
int
x = 4;
int
y = 6;
int
z = 5;
cout
<< x << " " << y << " " << z
<< endl;
int
result1 = (z < x) && (z++ < y);
cout
<< x << " " << y << " " << z
<< endl;
cout
<< result1 << endl;
What
do you expect the value of z to be? Theoretically, it should be 6 because there
is a ++ operator on z, but since (z > x) evaluates to false, and it is an
AND operation, it is clear that the whole statement will be false (since false
AND anything is still false). This is important to be aware of since if you
aren't, you can sometimes get unexpected results from your code and want to
beat your computer into a smoking scrap heap. Remember, this is something that
has to be taken in context. Sometimes it's not readily apparent what is going
to happen, take a pencil and paper and work though it for something complex.
Also, if you still don't understand boolean logic by now, do a search on the
internet, it's quite important.
Another
operator to note is the ! operator. This will negate the value of the
logic expression. For example, if you had (x == 4) evaluate to true (1), then
!(x == 4) would evaluate to false.
Now
that we know what boolean tests do, we can learn how to use if
statements to control the flow of our program. There are a few formats to the
if statement, lets start with the simplest.
if
(condition) command;
if
(condition) {
command1;
command2;
...
commandn;
}
The
condition is the logic tests we discussed above. Doing this, we can enable the
program to decide if we want to execute the following command(s) or not. Use
the block symbols ({}'s) to execute more than one line. Now, sometimes we might
want to make several different decisions based on the outcome of the first
decision. Consider...
if
(condition) command;
else
if (condition) command;
...
else
if (condition) command;
else
command;
if
(condition) {
command1;
command2;
...
commandn;
}
else
if (condition) {
command1;
command2;
...
commandn;
}
...
else
if (condition) {
command1;
command2;
...
commandn;
}
else
{
command1;
command2;
...
commandn;
}
This
will test the first condition, if that fails, it will test the second
condition, if that fails it will test the third and so on. If all of the above
fail then it will execute the else condition. You should be aware that the else
and else if aren't neccessary and you don't have to use the {}'s if you only
have one line to execute. An example might make things easier to understand.
Consider the following program which makes use of most of the things we have
learned so far.
#include
<iostream.h>
int
main(void) {
int x = 0;
int y = 0;
cout << "Please enter an
integer between 1 and 10: ";
cin >> x;
cout << "You entered: "
<< x << endl << endl;
if ((x
< 1) || (x > 10)) {
cout << "Your value for x
is not between 1 and 10!"
<< endl;
cout << "Ending
program!" << endl << endl;
// the
following line will be more apparent when we discuss
//
functions
return
0;
}
// % is an
arithmetic operator meaning MODULUS; that is, the
// remainder
when the value left of % is divided by the right.
// So below, y
is the remainder when x is divided by two.
y = x % 2;
if (y ==
0)
cout << "The number you
entered was EVEN!" << endl;
else
cout << "The number you
entered was ODD!" << endl;
if (x
<= 3)
cout << "Your number was
less than or equal three."
<< endl;
else if ((x > 3) && (x < 7))
cout << "Your number was
between 3 and 7." << endl;
else
cout << "Your number was
greater than or equal 7."
<< endl;
return 0;
}
One
last thing to say, if statements can be nested, that is you can put one
if statement inside another for multi-level decition making. The following is
an example of a nested if statements. Keep in mind that all the rules regarding
if statements and their blocks hold true.
int
x = 5, y = 4, z = 2;
if
(x == 5)
if (y ==
4) {
if (
z < 20)
cout << "We have
reached a fully true decision!" << endl;
cout << "Inside the y if
block!" << endl;
}
Sometimes
there are quite a lot of branches you with your program to take. In this case,
you might find it annoying to write your decision structure as a series of if
statements. For this, those wonderful chaps at the ANSI C Store have created
for you, the switch statement. The format of the switch statement is as
follows,
switch
( variable ) {
case 1:
command;
break;
case 2:
command;
break;
...
case n:
command;
break;
default:
command;
break;
}
Note
that you can perform a block of code within each case using {}'s. Also, a
statement can have multiple case labels. For example,
case
1:
case
2:
case
3:
case
4:
command;
case
5:
case
6:
command;
Each
case is the right hand side of a true test (==) in a logic statement. So "case 1:" would be the same as "if (variable == 1)". Also, the default case is what happens if none of the others
evaluate as true. Please note that the break command is at the end of every
case and is neccessary to preven the other cases from occuring. The break
command will be discussed in more detail when we consider loops.
Here
is the above example but using a switch statement instead to evaluate the range
of the number.
#include
<iostream.h>
int
main(void) {
int x = 0;
int y = 0;
cout << "Please enter an
integer between 1 and 10: ";
cin >> x;
cout << "You entered: "
<< x << endl << endl;
if ((x
< 1) || (x > 10)) {
cout << "Your value for x
is not between 1 and 10!"
<< endl;
cout << "Ending
program!" << endl << endl;
// the
following line will be more apparent when we discuss
//
functions
return
0;
}
// % is an
arithmetic operator meaning MODULUS; that is, the
// remainder
when the value left of % is divided by the right.
// So below, y
is the remainder when x is divided by two.
y = x % 2;
if (y ==
0)
cout << "The number you
entered was EVEN!" << endl;
else
cout << "The number you
entered was ODD!" << endl;
switch ( x
) {
case
1:
case
2:
case
3:
cout << "Your
number was less than or equal three."
<< endl;
break;
case
4:
case
5:
case
6:
cout << "Your
number was between 3 and 7." << endl;
break;
case
7:
case
8:
case
9:
cout << "Your
number was greater than or equal 7."
<< endl;
break;
}
return 0;
}
Make sure
you note that the case number is the right hand side of the equivalence test.
So for example, if x was of type char then the tests would be performed as something like this.
case
'a':
cout << "Some text"
<< endl;
break;
case
'b':
case
'c':
cout << "Some text"
<< endl;
break;
case
'd':
case
'e':
case
'f':
cout << "Some text"
<< endl;
break;
case
'g':
case
'h':
case
'i':
cout << "Some text"
<< endl;
break;
I just
thought I'd better clarify that because it looked a little ambiguous :)
Chapter
Conclusion
Well, that
concludes the C++ basics. There isn't really that much difference from C if
you've programmed in that before, probably quite a bit different from the
programming languages you've worked in. Either way, I think I've covered it
fairly well. The next chapter will cover Loops. In which we'll explore the
three main loop structures in C++, the for loop, the do loop, and the while
loop.
Written For: Omicron of http://www.bluesfear.com
Back to Bluesfear
Digital Art
2000, 2004© BlueSfear