Gw Temp

Menu

Tutorial - 'Comprehensive C++ Tutorial for Beginners - Part 3' by AzureFenrir

An item about Programming Languages posted on

Blurb

The third installment of AzureFenrir's excellent C++ for Beginners tutorials. If you are interested in learning C++, read this series.

Body

Comprehensive C++ Tutorial for Beginners - Part 3

Well, well, the series continues :). AzureFenrir isn't a lazy bastard after all...listen to Azure referring to himself in third-person!

Oops, looks like I just digressed.

This tutorial, the third of the series, will enlighten you with grand knowledge of boolean operations and selection structures. So...pack your thinking caps and prepare for programming Nirvana!

The concept of Boolean variables is easy. Boolean deals with two variables: 0 and 1, false and true. 0 means false, and 1 is true. This is very similar to how the computer makes decisions - yes and no, do one thing or do the other. Boolean values are commonly used through one of these six operators:


a == b
Returns a value of true (1) if a and b are equal. Returns false (0) otherwise.
[NOTE: This is not the same as the assignment (=) operator. To make comparisons, you MUST type two equal signs, not one!]

a != b
Returns a value of true (1) if a and b are NOT equal. Returns false (0) otherwise.

a > b
Returns a value of true (1) if a is larger than b. Returns false (0) otherwise.

a < b
Returns a value of true (1) if a is smaller than b. Returns false (0) otherwise.

a >= b
Returns a value of true (1) if a is larger than or equal to b. Returns false (0) otherwise.

a <= b
Returns a value of true (1) if a is smaller than or equal to b. Returns false (0) otherwise.


So...what the heck does that mean? Let us use an example to demonstrate:

bool temp;
temp = (3 > 5);


Remember bool variables from Part 1 of this tutorial? This code will store the value false (0) in temp, because 3 is not larger than five!!! (Well, maybe in other planets/dimensions, but we're talking about earth, our dimension).

However,

temp = (7 > 5);

Will store true (1) in temp, because 7 IS greater than five. How fun was that?

READER: Zzzzzzzzzzzzzzzzzz

Anyway, (Wake Up!) Boolean structures are used in many different ways in C++. In this tutorial, we will discuss the selection structures - basically, the if structures, the else structure, the switch structure, and the break statement. Don't worry, this is a beginners' tutorial, which means I'll walk you through these statements step-by-step.


The "if" Structure

The if structure is analogous to RM2K's FORK statements. Basically, it allows you to run a piece of code if it's conditions are met - that is, if it's inner code returns true (1).

You can use it like this:

if(condition)
{
    Insert what happens if the conditions are met here
}

Here's an example:

#include <iostream.h>

int main()
{
int ATARIAge = 0;

// Again, no offense meant to ATARI
cout << "ATARI's Age: ";
cin >> ATARIAge;

if(ATARIAge < 18)
{
cout << "ATARI is younger than 18!\n";
cout << "ATARI is young!\n";
}
}


When you run the program, you'll get something like:

ATARI's Age: _

Type in something smaller than 18 and watch the text appear! 'Tis magic, I tell ya, magic!!!

*Insert groans here* ©

Did you notice the braces { } that's just below the if statement? It looks just like the braces under the main function! Well, if you read my first tutorial (or if you know a lot of C++ and are reading this tutorial just for fun), then you know that the braces signals to main that all of the code under it belongs to it. The braces under the if statements serve the same purpose - they tell the if statement that it has exclusive control over everything inside the braces! That power-hungry bastard...

What happens if you omit the brackets, like this:

if(ATARIAge < 18)
cout << "ATARI is younger than 18!\n";
cout << "ATARI is young!\n";

Well, there's no run-time errors, and if you run this code and type in something smaller than 18, then it works regularly. However, run it again and type a large value (like 34)...oh no! The program still displayed:

ATARI is young!


When you removed the braces, only the first line of code, cout << "ATARI is younger than 18!\n";, belonged to the if statement. Since the if statement cannot control the second line, the second message will display, regardless of what you type.

So...just remember to always include braces, capiche?

If you've ever used RM2K (or any other scripting/programming language), you should be familiar with the concept of "else" statements. If you don't, well...they appear right after an if statement, and contains "stuff" that will run if the condition in the if statement is NOT met.

MWAHAHAHAHAHAHA!!! WHEN I PERFECT MY POWERFUL MUTANT ARMY, THE WHOLE WORLD IS MINE!

That proves that I belong in an asylum...anyway, the else statement MUST immediately follow the end brace } of an if statement. There cannot be ANYTHING (except comments [and some compiler directives]) between the if } and the else statement!!!

Here's an example of an else statement:

#include <iostream.h>

int main()
{
int ATARIAge = 0;

// Again, no offense meant to ATARI
cout << "ATARI's Age: ";
cin >> ATARIAge;

if(ATARIAge < 18)
{
cout << "ATARI is younger than 18!\n";
cout << "ATARI is young!\n";
} else {
cout << "ATARI is either older than or exactly 18!\n";
cout << "ATARI is experienced!\n";
}
}


I don't need to know ATARI's real age, so even if you know it, don't post it, capiche?

You an also have multiple if branches by using else if:

#include <iostream.h>

int main()
{
int ATARIAge = 0;

// Again, no offense meant to ATARI
cout << "ATARI's Age: ";
cin >> ATARIAge;

if(ATARIAge < 18)
{
cout << "ATARI is younger than 18!\n";
cout << "ATARI is young!\n";
} else if (ATARIAge <= 56) {
cout << "ATARI is between 18 and 56!\n";
cout << "ATARI is cool!\n";
} else {
cout << "ATARI is older than 56!\n";
cout << "ATARI is experienced!\n";
}
}


(Technical stuff: else if is not really a single C++ keyword. Rather, it's a combination of two keywords - if and else.)

Do you understand all this so far? Yes? Good! Now, I'll show you a few new boolean operators before ending the tutorial with the "switch" statement:


a && b (Boolean AND operator)
Returns true (1) if both a and b are true (1). Returns false (0) otherwise.

a || b (Boolean OR operator)
Returns true (1) if either a and b are true (1). Returns false (0) if neither are true.

!a (Boolean NOT operator)
Returns true (1) if a is false (0). Returns false (0) if a is true (1).
Basically, returns the opposite of a.


How do you use these? Elementary, my dear reader. a and b represent different expressions. For example:

(2 > 5) && (3 < 4)

Returns false (0). Although 3 is less than 4, 2 is NOT greater than 5. Because the && operator requires both sides to be true (1), it returns false (0). However:

(2 > 5) || (3 < 4)

Returns true (1). 3 is less than four, and because the || operator only requires one side to be true, this expression returns true (1).

Hopefully, those are easy concepts for you to grasp. Now, before I end this tutorial, I want to quickly talk about one more C++ selection structure, the switch keyword. The switch keyword is a unique selection structure. It accepts a variable instead of a boolean statement, and has multiple branches depending on what the variable contains. For example:

switch(variable)
{
case 1:
somecode
break;
case 2:
someothercode
break;
case 3:
somemorecode
break;
default:
defaultcode
}

If variable is equal to one, then the statement will run somecode. If it's equal to two, then the statement will run someothercode. If it's equal to three, then somemorecode will run, and if variable is anything else...well...then defaultcode will run.

Hopefully, this tutorial aided you in your quest to learn the great language of C++. If you really like this tutorial, please attempt to convince GW that kidnapping AzureFenrir is not recommended, and that MY TUTORIALS SHOULD BE REVERED BY...THE WORLD! MWAHAHAHAHAHAHAHAHA!!! GLOBAL DOMINATION!!!

[AzureFenrir is assassinated by masked GW members with Kotetsus]