ChEaTdEViCe
WELCOME TO CHEAT DEVICE
GTAOnline popping here



 
HomeHomeCalendarGalleryFAQSearchUsergroupsRegisterLog in

Share | 
 

 Lesson 5 - if Statements

View previous topic View next topic Go down 
AuthorMessage
DarK_DemoN
Member
avatar

Points Points : 6144
Posts : 381
Reputation : 25
Join date : 2011-01-25
Location : Hacking Kingdom

PostSubject: Lesson 5 - if Statements   Tue Jan 25, 2011 12:54 pm

Lesson 5: If Statements

If you are reading this tutorial, then you are probably a programmer. Else, you aren't reading this tutorial. The preceding sentences were examples of if statements. If a condition is true then a certain generalization can be made about you, such as "IF you are reading this, you are probably a programmer". If the condition is not true then a generalization can still be made "Else, you aren't reading this tutorial". I like to draw a parallel to electrical currents here. As you probably know electric currents have two states: on and off; 1 and 0; true or false. This is a good example of if statements, but it is not solely representable of if statements. What if we want if statements to have more than two states, on or off? What if we want on, halfway on, or off? Well thats what I'll be teaching today.

Now you're probably thinking, "Enough formalities! Where is the witty guy who wrote the Input/Output lesson?". Well here I am! I was struck with a sudden gift of intellectual clarity and I felt I had to write that bit on the relation between if statements and electrical currents. So anyway, by now you are already an extremely adept programmer and you've probably written at least a few of your own programs. But wait! Let me guess: you're tired of making programs with no variation, that is, programs that just keep displaying text and getting input. How do we work with this input!?, you ask. Well I'll stop rambling and be concise: this is how you perform if statements:

Code:

if (condition is met)
{
 perform code;
}

Now when I said that's how you do if statements, I wan't completely honest. In a language like ruby, (http://www.ruby-lang.org/en/), you could copy and paste that into your program and it would work. But as a joke I once heard attests to: "In ruby, just take the comment lines out of your pseudo-code and your program will run. In c++, take the comments out, change the supporting syntax, then write another hundred lines of code to make it work." If you don't know what pseudo-code is, its plans for your program. Like this:

Tic Tac Toe
While Player doesn't want to stop
get input.....


So basically the joke says "C++ requires a lot of syntax to do simple things". But trust me this does pay off in more ways than just making your program look cool. Also, at the beginner level you won't notice this as much, but as you progress and your google search for programming help returns a link for a way to do what you need in ruby and the code is like "ChangeNumber();" whereas in c++ its something like creating a new function and using vectors and strings that takes up hundreds of lines of code, you'll know what I mean.

So anyway, I was rambling again. So here is the programming bit:

Code:

#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter the number 10" << endl;
cin >> x; // you remember cin right?
// heres our if statement:

if (x == 10)
{
  cout << "Good job!" << endl;
}
else
cout << "That was wrong!" << endl;

system("PAUSE");
return 0;
}

What that program did was tell you to enter the number ten and if you entered anything other than 10, it told you that your number was wrong. Instead of saying "If 10: do this" And "If not 10: do this". I said "If 10: do this. or else do this:". Basically I was lazy and instead of two if statements, i used one if statement and one else statement.

You'll notice I didn't put {} around my else statement. IF your code is only one line, no matter if you are using else or if, you dont have to put {}. But if your code is more than one line, you need to use {}. Now you're probably wondering: "What if we want to have more than one condition?". Good question. Observe:

Code:

#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter the number 10" << endl;
cin >> x; // you remember cin right?
// heres our if statement:

if (x == 10)
{
  cout << "Good job!" << endl;
}
 if (x == 5)
cout << "You entered 5, not 10!" << endl;

system("PAUSE");
return 0;
}

Another way of checking if they entered 10 is this way:

Code:

if (x!=10)

that says "If x doesn't = 10".

I'll show you how to use boolean operators now: just read the comments in the code.

Code:


#include <iostream>
using namespace std;
int main()
{
cout << "Enter 6 or 5" << endl;
int x;
cin >> x;
if (x == 6 || x == 5) // the || operator means "OR" so they can              //enter 5 or 6                               
{
cout << "Good job!" << endl;
}
system("PAUSE");
return 0;
}

Code:


#include <iostream>
using namespace std;
int main()
{
cout << "Enter 6" << endl;
int x;
cin >> x;
cout << "Now enter 5" << endl;
int y;
cin >> y;
if (x == 6 && y == 5) // the && operator means "AND" so they have to              //enter 5 and 6                               
{
cout << "Good job!" << endl;
}
system("PAUSE");
return 0;
}

Input with characters (chars) are slightly different:

Code:

char c;
cin >> c;
if (c == 'a')
{
  cout << "You entered a" << endl;
}

characters are surrounded by ''. not "", but ' '.

Thats it for this lesson.

You Try! geek
1. Create a text based RPG that lets you make decisions by typing letters and or numbers. This is ALOT of fun, if you do make one, please send it to me @ [You must be registered and logged in to see this link.] I'd love to see them.

Please watch the video tutorial for this lesson at: [You must be registered and logged in to see this link.]
Back to top Go down
View user profile http://1337codez.co.cc
Pixel
Donater/Mod/Updater
Donater/Mod/Updater
avatar

Points Points : 6508
Posts : 551
Reputation : 28
Join date : 2010-03-17
Location : PBP-Unpacker

PostSubject: Re: Lesson 5 - if Statements   Tue Jan 25, 2011 2:33 pm

Nice C++ lesson ^_^ we don't have alot of member with large coding knowledge Very Happy


★Why not with some /\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ush★What up ninnyninja!?


Last edited by Pixel on Tue Jan 25, 2011 2:36 pm; edited 1 time in total
Back to top Go down
View user profile http://cheatdevicee.tk/
DarK_DemoN
Member
avatar

Points Points : 6144
Posts : 381
Reputation : 25
Join date : 2011-01-25
Location : Hacking Kingdom

PostSubject: Re: Lesson 5 - if Statements   Tue Jan 25, 2011 2:35 pm

Me and my friend made these tuts together.

Why are you doubting my capabiliteis?
Back to top Go down
View user profile http://1337codez.co.cc
Pixel
Donater/Mod/Updater
Donater/Mod/Updater
avatar

Points Points : 6508
Posts : 551
Reputation : 28
Join date : 2010-03-17
Location : PBP-Unpacker

PostSubject: Re: Lesson 5 - if Statements   Tue Jan 25, 2011 2:37 pm

DarK_DemoN wrote:
Me and my friend made these tuts together.

Why are you doubting my capabiliteis?
I've edited my post Wink I though you were someone else Very Happy

Sorry :P


★Why not with some /\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ush★What up ninnyninja!?
Back to top Go down
View user profile http://cheatdevicee.tk/
DarK_DemoN
Member
avatar

Points Points : 6144
Posts : 381
Reputation : 25
Join date : 2011-01-25
Location : Hacking Kingdom

PostSubject: Re: Lesson 5 - if Statements   Tue Jan 25, 2011 2:41 pm

Ok, you might have thought i was someone called Wander (my friend i was talking about and the creator of the video tuts). If you know him ill pass on your thanks Smile

Pixel wrote:

Nice C++ lesson ^_^ we don't have alot of member with large coding knowledge
I can see that haha. If you would like ill post a simple calculator i made in C++ that has (cheasy) background music and some other features
Back to top Go down
View user profile http://1337codez.co.cc
Pixel
Donater/Mod/Updater
Donater/Mod/Updater
avatar

Points Points : 6508
Posts : 551
Reputation : 28
Join date : 2010-03-17
Location : PBP-Unpacker

PostSubject: Re: Lesson 5 - if Statements   Tue Jan 25, 2011 2:53 pm

DarK_DemoN wrote:
I can see that haha. If you would like ill post a simple calculator i made in C++ that has (cheasy) background music and some other features
You can post all your works :P But remember, it is always better to have your name somewhere in your codes. ^_^

You'll be sure that no one will steal your works Very Happy


★Why not with some /\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ush★What up ninnyninja!?
Back to top Go down
View user profile http://cheatdevicee.tk/
DarK_DemoN
Member
avatar

Points Points : 6144
Posts : 381
Reputation : 25
Join date : 2011-01-25
Location : Hacking Kingdom

PostSubject: Re: Lesson 5 - if Statements   Tue Jan 25, 2011 2:56 pm

Pixel wrote:

You can post all your works But remember, it is always better to have your name somewhere in your codes. ^_^

You'll be sure that no one will steal your works


YUP!! Just like all the GFX i make i ensure my name is there somewhere! Smile

And in the case of this calculator it says it right on the window and in the program itself!! Hahaha

I'll make a "C++ Project" threads and post it in there
Back to top Go down
View user profile http://1337codez.co.cc
Sponsored content




PostSubject: Re: Lesson 5 - if Statements   

Back to top Go down
 

Lesson 5 - if Statements

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
ChEaTdEViCe ::  :: Programming :: C++ Programinng Basics-
Free forum | © phpBB | Free forum support | Contact | Report an abuse | Forumotion.com