unknowncheats uc-forum.com ucdownloads ucdownloads.com

Go Back   UC-Tutorials - Multiplayer Game Hacking and Cheat Tutorials > Programming > C and C++

- Sponsored Advertisement -
http://www.myfpscheats.com/


Reply
 
Thread Tools Display Modes
  #1  
Old 12-23-2006, 06:52 AM
zero_tolerance zero_tolerance is offline
Senior Member
 
Join Date: Dec 2006
Posts: 289
Default Beginners C++ Lesson 5

By Turv

Lesson 5
-----------------------


Boolean and Logic Operators


Ok, so hopefully we all know what a boolean variable is, if not its a variable that has 2 possible values, 1 or 0, true or false.

Logic operators are compairson operators.

First we will start with Boolean operators. Some of which include:
Code:
Equal To : 		==
Greater Than : 		>
Less Than : 		<
Equal Or Greater : 	>=
Equal Or Less : 	<=
Not Equal To : 		!=
Explaining the way these work is really hard.
So I will just jump to examples.
Code:
#include <iostream.h>

int j = 5;
int i = 3;
bool output;

int main()
{
	output = (j == i);
	cout<<output<<endl;

	output = (j > i);
	cout<<output<<endl;

	output = (j == 5);
	cout<<output<<endl;

	output = (i != 132);
	cout<<output<<endl;

	return 0;
}
Now, what you should see as output is
0
1
1
1

The reasoning is that if the operators will return a 1 or a 0 depending if the statement is true or false... The first one we notice that j does not equal i so theirfor it returns 0.
And all the rest of the statements are true.

Secondly we will start with Logic Operators... We will look at 3, but these three are the very most important...
Code:
AND          &&
OR            ||
INVERSE   !
First the && operator.
Both the left side of the && must equal one for the output to be one.

example. (using the code above [under the last cout<<])
Code:
if((j == i) && (i < 12))
{
     cout<<"Output is true"<<endl;
}
else
{
     cout<<"Output is false"<<endl;
}
We can do this logicly that this will say that the output is false, mainly because (j == i) returns 0, and (i > 12) returns true, but for an AND statement to work both the left side and right side must return true (1).

Now, we change the && to ||, meaning its now an OR statement.
We run it, and guess what, now it says "Output is true", which is true ... Reasoning because the way and OR statement works is that if the left side or rightside return 1, then the whole statement will return 1.


Now for my favorite , The inverse operator. I find it is the very most useful.

so, what it does is negates (I dont know if thats a word) the value. So say if (i == j) returns 0, you can do !(i == j) and it still returns 0, but then it will change it to 1 because of the inverse. Same if it returned 1, it would change it to 0.

So, back to the example above.
Code:
if(!(j == i) && (i < 12))
{
     cout<<"Output is true"<<endl;
}
else
{
     cout<<"Output is false"<<endl;
}
We will notice that it says "Output is true". Which you should know why.


Now, wasnt that exciting and interesting


Once again a special thanks to Azorbix
Reply With Quote
Reply

  • Submit Thread to Digg
  • Submit Thread to del.icio.us
  • Submit Thread to StumbleUpon
  • Submit Thread to Google
  • Submit Thread to Facebook
  • Submit Thread to My Yahoo!
  • Submit Thread to MySpace
  • Submit Thread to Twitter
  • Submit Thread to Reddit

Tags
beginners, lesson

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT. The time now is 04:58 AM.