If, then, else, elseif...?

Started by
15 comments, last by Oluseyi 19 years, 6 months ago
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 :).
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...
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
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!
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. >
- stormrunner
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
Thank you very much everyone that really helped! In the meantime I'm gonna go C++ book hunting :).
Quote:Original post by xDMG89x
Thank you very much everyone that really helped! In the meantime I'm gonna go C++ book hunting :).


Go and download "Thinking in C++, Volume One" :)

Cheers,
Drag0n
-----------------------------"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning..." -- Rich Cook"...nobody ever accused English pronounciation and spelling of being logical." -- Bjarne Stroustrup"...the war on terror is going badly because, if you where to compare it to WWII, it's like America being attacked by Japan, and responding by invading Brazil." -- Michalson
"else if" in c/c++ is really just an else statement followed by an if statement, right?
Quote:Original post by Anonymous Poster
"else if" in c/c++ is really just an else statement followed by an if statement, right?
Yep.

This topic is closed to new replies.

Advertisement