+ 14
Why nest if statements when you can use Logic?
So I've been looking at nested if statements in c++ and I'm wondering when I'd use nested statements instead of a single line with logic operations? For example: int x = 20; int y = 3; if (x > 15) if(y < 5) cout << "Yes"; else cout << "No"; Is The Same As: int x = 20; int y = 3; if (x > 15 && y < 5) cout << "Yes"; else cout << "No";
26 odpowiedzi
+ 20
try the code with x=20 and y=6.
it wont print anything in the 1st and print No in 2nd beacuse else checks for the parent if not the child one
+ 14
Actually, if x > 15 and y < 5 isn't the same, as "No" is printed when y >= 5 and x > 15 in the first version, but x <= 15 or y >= 5 in the second.
+ 7
if(a<c) { //parent if
cout<<"a is smaller than c";
if(a<b) //child if
cout<<"and also smaller than b";
else
cout<<"but not b";
}
+ 6
@KrOW no, if x<=15 No will be printed because that else is of the first if as without {} if will only consider one line (i.e. one ;) in its block. So the else will be outside of if.
+ 6
Ok, Thanks, I didn't knew that an if else statement is a block in itself and can work like that in functions without {}.
+ 6
check out my nestidIFs.cpp program, which compares nested IFs and a logic AND. I worked out a truth table to go with it...
https://code.sololearn.com/cvv90f8dOTwI/?ref=app
+ 5
i need another example
+ 5
if you pass the test of the outer "if", you'll go on to the inner "if", but if you don't pass that test, according to Tamaryn's code, nothing will happen because the output is only if you pass both nested IFs. If you don't pass the outer "if", you'll go into the "else", which outputs "No".
On the other hand, for the logic AND, if both (and only both) tests pass, you'll get "Yes", otherwise "No".
The {} braces must enclose everything in the if-block or the else-block. If there is any "if" code, say, outside the braces of the if-block and before the else-block, you'll get an error msg. One line doesn't need braces, but it's a good idea to use them in case you add more lines later -- just put them inside the braces.
+ 4
sometimes it can be useful but if one needs a direct answer like this code then using logic is better choice as it reduces the no. of lines. But if you need some func in a parent if and some func only if a statement is true then you definitely need nested if
+ 3
if you need to do other tasks on first condition or for leave confusion, you must use nested else go with single
+ 3
John, I'm not sure that's correct because the 2nd operation looks for both conditions to be true before printing yes. Which is exactly the same for the first piece of code. AND operator is only TRUE if both conditions are TRUE
+ 3
in the first piece of code nothing will printed if x<=15 because you print "No" only if x >15 and y<5... in this piece, else is referred to internal if... This is caused from missing curly braces and without they, only next first instruction "own" the "if" statement and same for "else" and other conditional/iterative constructs
+ 3
what we are learned??? dont forget braces or you can obtain unexpected results 👍👍👍😁😁😁
+ 3
you can insert braces in anyway you want but there will always be a difference in the outputs of your code one way or another.
+ 3
look at the code i provided in 3rd answer. In cases like that nested if are the only solution
+ 2
Surely this code will only run if all statments are true as intended. Can you explain more about what you mean with a parent if?
+ 2
but you inserted braces in logic manner (wanted way) but if you dont insert braces, you obtain different results from which want
+ 2
As i writed before, nested are really useful if you must do task also if other conditions (in the same "if" construct) dont verify:
if( age >0){
if(age<100){
cout <<"ok you age is ok";
}else{
cout<< "you are very old";
}
}else{
cout << "You dont be never born";
}
+ 2
One of my first java programs i nested if statements because i didn't know how to use else if. Sometimes nested or separate if statements work better than else if and else.