I am not getting this example can anyone explain it in a easy way👇👇👇👇🙏 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I am not getting this example can anyone explain it in a easy way👇👇👇👇🙏

#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; }

18th May 2019, 12:16 PM
Ravi Henry
Ravi Henry - avatar
1 Answer
+ 12
Ravi Henry in your sample code you have used nested if else in which for age = 18 you have define condition on age with if statement and in the body part of if statement you have used another if else with another condition in if statement. Then in else part you have done the same and used if else statements in else part with condition. So first age=18 will goes in primary if condition which is if(age>14) -> this becomes true so goes inside the primary if and match the condition of primary 1 if which is if(age>=18) -> this is true as age is 18 So goes inside the body and print "Adult" if the condition is false then it will print "Teenager" And if the age is 14 and primary if condition is false then goes in else part and evaluate if and else condition present in else statement and execute it
18th May 2019, 12:30 PM
GAWEN STEASY
GAWEN STEASY - avatar