Jump to content
  • Advertisement
Sign in to follow this  
xDMG89x

If, then, else, elseif...?

This topic is 5476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone! I am quite new to C++... the only language that I have some knowledge of is VB. I was wondering, how do you make if then else elseif statements in C++? Quite a simple question I know but I'd really appreciate it if some could help :).

Share this post


Link to post
Share on other sites
Advertisement

if (someBoolCondition)
{
// code
}

if (condition)
{
// code
}
else
{
// other code
}

if (condition)
{
// code
}
else if (anotherCondition)
{
// more code
}
else
{
// even more code
}

Although methinks you'll be better off with a good C++ book, you'll have a hard time learning it properly from random code snippits on the web...

Share this post


Link to post
Share on other sites
An example:


int a = 5;
int b = 10;

if(a == 5)
cout << "A is 5";
else if (a == 10)
cout << "A is 10";
else
cout << "A is not 5 or 10";



You can use { and } to make it more clear:

eg,


int a = 5;
int b = 5;

if(a == 5)
{
cout << "A is 5";
}
else if (a == 10)
{
cout << "A is 10";
}
else
{
cout << "A is not 5 or 10";
}




If you have any questions I'd be happy to (try and) answer them.

- heap

Share this post


Link to post
Share on other sites
I agree with OrangyTang. Even though I learnt a descent amount of C++ from reading code, I still have no idea how to use std:: properly. And that's quite important.

So, before you make that leap to C++, get some help, whether it be a book, class, or mentor. It will help you a lot in the long run!

Share this post


Link to post
Share on other sites
there is no then in c++. instead, a then action (per se) is essentially a block of code encapsulated by { }, that is executed if the statement is true, like so :

if( x == 2)
{
/* then */
/* do something */
}
else if( x== 3)
{
/* then */
/* do something else */
}
else
{
/* do something else*/
}



pretty simple and straightforward.
<edit : my bad - my computer is slow. oh well. >

Share this post


Link to post
Share on other sites
Like this....

# include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 50)
cout<< "Greater than 50";
else if (num > 40)
cout << "Greater than 40";
else
cout << "Less than 40";
return 0;
}






If the instructions inside an "if" exceed one line you have to use brackets.

# include <iostream>
using namespace std;
int main()
{
int age;
cout << "Enter your age: ";
cin >> age;
if (age > 50)
{
cout<< "Greater than 50"<<endl;
cout<< "GOD do I feel old";
}
else if (age > 40)
cout << "Greater than 40";
else
cout << "Less than 40";
return 0;
}





if you want to do more than one comparison then it would look like this...

# include <iostream>
using namespace std;
int main()
{
int age;
int weight = 200;
cout << "Enter your age: ";
cin >> age;
if (age > 50 && weight <= 180)
{
cout<< "Greater than 50"<<endl;
cout<< "GOD do I feel old";
}
else if (age > 50 && weight > 180)
cout << "Old and fat! Damn you genetics!";
else
cout << "Young but may or may not be fat.";
return 0;
}






Keeping in mind that && means AND and || means OR

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"else if" in c/c++ is really just an else statement followed by an if statement, right?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
"else if" in c/c++ is really just an else statement followed by an if statement, right?
Yep.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!