+ 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";

3rd Feb 2018, 7:59 PM
Tamaryn Thomson
Tamaryn Thomson - avatar
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
3rd Feb 2018, 8:28 PM
Saminjay Goel
Saminjay Goel - avatar
+ 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.
3rd Feb 2018, 8:15 PM
John Wells
John Wells - avatar
+ 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"; }
3rd Feb 2018, 8:12 PM
Saminjay Goel
Saminjay Goel - avatar
+ 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.
3rd Feb 2018, 8:36 PM
Saminjay Goel
Saminjay Goel - avatar
+ 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 {}.
3rd Feb 2018, 8:48 PM
Saminjay Goel
Saminjay Goel - avatar
+ 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
7th Feb 2018, 8:07 PM
Eric Zatarack
Eric Zatarack - avatar
+ 5
i need another example
4th Feb 2018, 1:39 AM
PREMMARAN G
PREMMARAN G - avatar
+ 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.
8th Feb 2018, 11:29 PM
Eric Zatarack
Eric Zatarack - avatar
+ 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
3rd Feb 2018, 8:06 PM
Saminjay Goel
Saminjay Goel - avatar
+ 3
if you need to do other tasks on first condition or for leave confusion, you must use nested else go with single
3rd Feb 2018, 8:19 PM
KrOW
KrOW - avatar
+ 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
3rd Feb 2018, 8:25 PM
Tamaryn Thomson
Tamaryn Thomson - avatar
+ 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
3rd Feb 2018, 8:32 PM
KrOW
KrOW - avatar
+ 3
what we are learned??? dont forget braces or you can obtain unexpected results 👍👍👍😁😁😁
3rd Feb 2018, 8:51 PM
KrOW
KrOW - avatar
+ 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.
3rd Feb 2018, 9:03 PM
Saminjay Goel
Saminjay Goel - avatar
+ 3
look at the code i provided in 3rd answer. In cases like that nested if are the only solution
3rd Feb 2018, 9:05 PM
Saminjay Goel
Saminjay Goel - avatar
+ 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?
3rd Feb 2018, 8:07 PM
Tamaryn Thomson
Tamaryn Thomson - avatar
+ 2
but you inserted braces in logic manner (wanted way) but if you dont insert braces, you obtain different results from which want
3rd Feb 2018, 9:01 PM
KrOW
KrOW - avatar
+ 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"; }
3rd Feb 2018, 9:19 PM
KrOW
KrOW - avatar
+ 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.
9th Feb 2018, 3:25 AM
Ch Pe
Ch Pe - avatar