Topic : C++ Tutorial
Author : Unknown
Page : << Previous 2  Next >>
Go to page :


else if ( if there is one ). Now, if any of the else if's are true, then it will execute the statements in that braket, then it will skip the rest. If they all evaluate to false, it will output what ever is in else. Now, you dont have to have an else, but if you dont, then it will check every if statement, regardless of what the other if's turn out to be. Now, you have to know about the operators...oh yes, there are MORE operators. :)


<
>
<=
>=
== ( two equal signs )
!=


Now, these are not to hard, but lets briefly go over each. the < and the > are self explanatory.

8 < 9   // Evaluates to true
8 < -9   // Evaluates to false
8 > 9   // Evaluates to false
8 > -9   // Evaluates to true


And the <= and >= are the same thing, but they take into account that the case could be equal to each other. For instance

8 <= 8   // Evaluates to true, 8 is equal to 8
8 <= -3   // Evaluates to false
8 >= 8   // Evaluates to true, 8 is equal to 8
8 >= 10   // Evaluates to false


However, be careful. Observe the following examples:

8 < 8   // Evaluates to false
8 > 8   // Evaluates to false


Now, for the final two operators, != and = =.
= = compares values to see if they are the same thing, for instance

8 = = 8  // Evaluates to true
8 = = -8  // Evaluates to false


!= test for the exact opposite, check to see if they are not the same, for instance:

8 != 8  // Evaluates to false, 8 is equal to 8
8 != 10  // Evaluates to true


Notice that I had to seperate the = =, so you could see that, and not this: ==
Something else to watch out for is dont try to do this:

int x;
cin>>x;  // Assume that 10 is inputted
if(x = 18)
{
   cout<<" The Number 18 "<<endl;
}


Now, no matter what was inputted for x, " The Number 18 " will ALWAYS be outputted, because you are assigning x to 18 ( only one = ), not comparing it, with = =. You have to be careful, to use a = = and not a = in a conditional statement.

Now, we have only a couple of more things to cover before we are done with conditional statements. For instance, we have the ! operator ( NOT ). Like I said before, a bool variable can only have two cases, true and false. The ! operator will just switch the value over to its opposite. For instance:

bool var = false;  // var is assigned to false
var = !(var);  // var is now assigned to true


So that is that, not to hard. Now, the last part of this tutorial of conditional statments are logic operators. Lets look at it like this, lets say that we have a conditional statement

if(x == 18)

Not too hard, but what if we have another variable, y for instance, and we also need that in our condition. That is where the && and || come into play. The && ( AND ) is if you want two or more things to be true, and the || ( OR ) would be if you wanted one or the other to be true. Lets look at an example:

x = 18;
y = 21;
if( (x == 18) && (y == 21) )  // Will evaluate to true
if( (x != 18) && (y == 21) )  // Will evaluate to false, first condition is false
if( (x == 18) && (y != 21) )  // Will evaluate to false, second condition is false
if( (x != 18) && (y != 21) )  // Will evaluate to false, both conditions are false
if( !(x == 18) )   // Will evaluate to false. Statement is true, but the ! makes it false


I hope that is not confusing, just look at it for a couple of seconds, and it will make sense, trust me. Lets look at the same thing, only with ||

x = 18;
y = 21;
if( (x == 18) || (y == 21) )  // Will evaluate to true
if( (x != 18) || (y == 21) )  // Will evaluate to true, second condition is true
if( (x == 18) || (y != 21) )  // Will evaluate to true, first condition is true
if( (x != 18) || (y != 21) )  // Will evaluate to false, both conditions are false


To clear some things up, what we just did was called Short Circuit Evaluation. Which is this... When we have a line of boolean variables, like this:
true || true && false
We can easily evaluate that.
Whenever you use &&, and there is a false, the whole statement becomes false.
Whenever you use ||, and there is a true, the whole statment becomes true.
We go from left to right, lets evaluate this statement.

true || true || false && true || false && true && true

Now, lets take this step at a time
true || true becomes true, both are true
true || false becomes true, true wins with ||
false && true becomes false, false wins with &&
true || false becomes true, true wins with ||
false && true becomes false, false wins with &&
true && true becomes true, both are true

Or, we could go by Short Circuit Evaluation, and see that the true || true becomes true, and thats it right there :)

And with that, we end conditional statements, and we are allowing you to continue on with your next lesson.


C++ - Loops


Well, we are just moving quite along aren't we ? We have covered conditional statements, and basic io stuff. Now, lets get a little more into the language by doing loops. Loops are a vital part of programming, just as is conditional statements. This is one thing that you will have to learn to become a successful programmer. So without further ado, lets continue. There are a number of different loops, and you would use all of them for different situations. First off, lets quickly review what a loop is for those that dont know. Lets say for example that you were going to work. Everyday, you go to work, do stuff, and come home. Then you do that the next day, and again the next day, and so on and so on. That is a loop, something that you do a certain number of times. There are a do{ ... }while loop, a for and a while loop. Lets go over each one
do{...}while() - This loop will at the very least execute once. It will perform any of the statements inside of the brakets. When it is done with those, it will check what is inside of the while(statement). The statement will be a boolean value. If it is true, it will perform what is in the loop again, and again until that while evaluates to false. This is basically if you want to do something at least once, then check the results after each run, and see if you want to continue.

while(){...} - This loop is the exact same as a do-while loop, except, there is no promise that it will execute at all. With the do-while loop, it will execute at least once, then check the while part, this will check the while part first, then if and only if that is true, it will start the loop and continue until it is false. This will be for checking results before the test starts, and then performing the work.

for - This is the only " outstanding " loop, because it looks nothing like the other two mentioned above. This is basically for something that you only want to loop for a certain amount of times. The syntax looks like this
for(variable;statement;increment)
It will, like the while, check the statement before it starts. And then it will continue as long as the statement is true.

Now that we have seen the basis of each loop, lets talk about a concept that is very very very bad to any programmer. Its called an " Infinite Loop ". Now, that is something that is self explanatory, but I will touch basis with it anyway, because it is so important. Lets take this example:

int x = 10;
do
{
x++;
}while(x > 5);


Now, if we think about this, after the first time it goes through the loop, x will be 11, then 12, then 13, 14, 15...and so on and so on. If you look at the statement, x wil ALWAYS be greater than 5 in this case, so it will do it over and over and over again. If

Page : << Previous 2  Next >>