[Tut]If/Else Statements

By Skitles on Oct 05, 2008

Title^

I'm here to teach you about if/else statements. You'll need to at least know how to build a "Hello World" program by including iostream, how endl; works, and how cin works to do this.

include

using namespace std;

int main()
{
int iFirst;
int iSecond;
int iUseranswer;

cout << "Please put in number one: ";
cin >> iFirst;

cout << "Please put in number two: ";
cin >> iSecond;

cout << "What is " << iFirst << " plus " << iSecond << "?" << endl;
cin >> iUseranswer;

if (iUseranswer == iFirst + iSecond)
{
cout << "YAY!!!11!11 You got it, nyoro." << endl;
system("PAUSE");
return 0;
}else{
cout << "NO! WRONG! GAH!!!!11!!!11!12132" << endl;
system("PAUSE");
return 0;
}
}

Alright, that was a lot, and many of you are probably wondering a few things. I'll try to answer most of your questions below:

*** Why don't iFirst and iSecond in line 16 have quotations around them?***

iFirst and iSecond are what's called *variables.* When the user imputs a number at iFirst and iSecond, the number gets stored in memory. Cout can display these numbers in the window in the same way they display stuff like "What is " on the same line.

***Okay now, how do these "if" statements work?***

As you'll probably noticed, the if statement on line 19 had a bunch of stuff in brackets. if (iUseranswer == iFirst + iSecond) basically says "if the imputted answer is equal to the first number plus the second number, execute the following code". The following "else" statement means "Otherwise, execute *this* code. To explain this better, look at the following snippet:

if(This statement is true)
{
Then do this.
}
else
{
If it doesn't, then do this.
}

***Why is it "==" on line 19 and not "="?***

In C++ = means "assign to". Thus, if it was "if(iUseranswer = iFirst + iSecond)", it would've meant "if(iUseranswer is assigned to iFirst +iSecond)", thus not working.

As you can see, the if statement is one of the building blocks toward making programs in C++ that make decisions.

I hope I explained that clearly. If you have any questions, feel free to post

Comments

Sign in to comment.
Jonesy44   -  Oct 06, 2008

Okok, NOW i see what i've done! lol

I openned the wrong one i didnt scroll down the page far nuff haha

 Respond  
guest598594   -  Oct 06, 2008

Go back on chatscape

 Respond  
Jonesy44   -  Oct 06, 2008

Scrub that, that link u sent me to is the one i have installed lol

 Respond  
Jonesy44   -  Oct 06, 2008

Yeaah, sorry i was on a while back.

I googled Visual C++ and i read the Wiki page, which made Visual C++, the microsoft thing i downloaded look like the right one.. Im a bit confused man :s

lol

 Respond  
guest598594   -  Oct 06, 2008

lol, VB Express is for VB (visual basic...) and C++ Express is for C++.

Go here and go to Visual C++ 2008 and hit download.

Although I think it takes up quite a bit of memory & takes a while to install, so if you want something smaller there's Dev-C++.

I went on chatscape and I guess you already left :P

 Respond  
Jonesy44   -  Oct 06, 2008

md, getchu and you C++ knowledge! i've installed whatever i just downloaded, it linked me to something called VB express?? i dont know, anywayas, get yer ass on irc.chatscape.net? :P

 Respond  
guest598594   -  Oct 06, 2008
    int iFirst;
    int iSecond;
    int iUseranswer;

could be

    int iFirst,iSecond,iUseranswer;
 Respond  
Imrac   -  Oct 06, 2008

wrong section there bud. And this might be a better thing for the forums.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.