Trouble understanding this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Trouble understanding this

I get the if/else command for the teenager where is the else command for the adult? is it the else { that points to child and somethings wrong? #include <iostream> using namespace std; int main() { int age = 18; if (age > 14) { if(age >= 18) { cout << "Adult"; } else { cout << "Teenager"; } } else { if (age > 0) { cout << "Child"; } else { cout << "Something's wrong"; } } return 0; }

21st Nov 2017, 8:08 PM
Terry Smart
4 Answers
0
can you explain which if's goto which else's I guess is what I'm trying to ask.
21st Nov 2017, 8:09 PM
Terry Smart
0
looks like if the person is at least 14, it checks to see if they're 18. if not, then teenager gets printed. if they're not at least 14, it goes all the way down to child.
21st Nov 2017, 8:16 PM
Gilbert Keys
Gilbert Keys - avatar
0
Ok that helps out. the last else statement is really unnecessary and could be left out. thanks.
21st Nov 2017, 8:24 PM
Terry Smart
0
inside main you have 3 if statement 2 with else statement one wihtout. 2 if statements are inside if statement inside the another if statement( one in if part, second in else part. first check if age is above 14. if yes then we are going inside this if statement and we check if age is equal or more then 18. if yes then its adult. if not then every other case in this statement (that mean 14, 15, 16 and 17) will go in else part and then its teenager. if the first statement ( the one which was checking if higher then 14) was false then we go ro else statement. inside we have another if statement which checks if age is more then one. if yes then its a child. if not there is no return because age cant be 0
21st Nov 2017, 8:30 PM
Bartek Nowacki
Bartek Nowacki - avatar