If, then, else, elseif...?
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 :).
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:
You can use { and } to make it more clear:
eg,
If you have any questions I'd be happy to (try and) answer them.
- heap
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!
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 :
pretty simple and straightforward.
<edit : my bad - my computer is slow. oh well. >
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. >
Like this....
If the instructions inside an "if" exceed one line you have to use brackets.
if you want to do more than one comparison then it would look like this...
Keeping in mind that && means AND and || means OR
# 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
Quote:Original post by Anonymous PosterYep.
"else if" in c/c++ is really just an else statement followed by an if statement, right?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement