+ 1

The doubt is that in false statement the output is This is executed & in true statement also the output is the same... plz help

public class Myclass { public static void main(String[] args) { boolean b; b = false; System.out.println("b is " + b); b = true; System.out.println("b is " + b); if (b) System.out.println("This is executed "); b = false; if (b) System.out.println("This is not executed "); System.out.println("10 is " + (10 > 9) ); } } // it's output should be different why is it same in both cases.

29th Dec 2017, 4:23 PM
Lucky
Lucky - avatar
14 Answers
+ 2
@Lucky, we can do that, but having two if case with different condition would be redundant I guess, I personally would take the if...else way, but since you said you prefer using if case only, well, here you go mate: public class Myclass { public static void main(String[] args) { boolean b; b = false; System.out.println("b is " + b); b = true; System.out.println("b is " + b); if (b) System.out.println("This is executed when b is true"); b = false; if (!b) // <- Note the NOT operator (!) System.out.println("This is executed when b is false "); } } Hth, cmiiw
30th Dec 2017, 3:55 AM
Ipang
+ 3
public class Myclass { public static void main(String[] args) { boolean b; b = false; System.out.println("b is " + b); b = true; System.out.println("b is " + b); if (b) System.out.println("This is executed "); // The line following the if(b) will // only be executed if the b variable // value is true, since you assigned // b = false, it is not executed. b = false; if (b) System.out.println("This is not executed "); System.out.println("10 is " + (10 > 9) ); } } You can also use if...else like so, boolean b = false; if(b) System.out.println("It's true!"); else System.out.println("No! it's false"); Hth, cmiiw
29th Dec 2017, 5:41 PM
Ipang
+ 3
Bro, quickly now, remove that phone number, it's against privacy policy bro!. I don't make direct contact with anyone here, besides, I'm not the right person to follow when it comes to Java, there are many friends here who are much better in Java, I'm a noob bro. You are welcome to drop me a line in one of my code if you need help, I will help when and where I can bro : ) Cheers!
30th Dec 2017, 4:16 AM
Ipang
+ 3
Thanks for understanding bro, that's cool! : )
30th Dec 2017, 4:18 AM
Ipang
+ 2
plz rebuild the code again and send me plz i request
30th Dec 2017, 2:10 AM
Lucky
Lucky - avatar
+ 2
@Lucky I'm sorry, is there a problem with the code?
30th Dec 2017, 2:27 AM
Ipang
+ 2
@Lucky, you're welcome mate, glad to help :)
30th Dec 2017, 4:04 AM
Ipang
+ 2
actually im having lots of doubt so i need ones phone number so that i could talk and understand the answers properly would you prefer to give your number plz
30th Dec 2017, 4:05 AM
Lucky
Lucky - avatar
+ 2
okay 😢
30th Dec 2017, 4:17 AM
Lucky
Lucky - avatar
+ 1
Are you talking about System.out.println("b is" + b) and System.out.println("b is" + b)? they are same but you can declare 2 different bolleans. bollean a = true; bollean b = false; and it will be System.out.println("b is" + a) System.out.println("b is" + b) that is solution if you are talking about that. :p
29th Dec 2017, 4:40 PM
Sad
Sad - avatar
+ 1
you code is right i think but can you rebuild the code again and send me ... your if and else statement is true but i need the answer in if case only ....
30th Dec 2017, 3:37 AM
Lucky
Lucky - avatar
+ 1
thanks
30th Dec 2017, 4:01 AM
Lucky
Lucky - avatar
+ 1
thanks bro
30th Dec 2017, 4:04 AM
Lucky
Lucky - avatar
+ 1
donot worry cause im not a stranger
30th Dec 2017, 4:06 AM
Lucky
Lucky - avatar