+ 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.
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
+ 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
+ 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!
+ 3
Thanks for understanding bro, that's cool! : )
+ 2
plz rebuild the code again and send me plz i request
+ 2
@Lucky I'm sorry, is there a problem with the code?
+ 2
@Lucky, you're welcome mate, glad to help :)
+ 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
+ 2
okay 😢
+ 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
+ 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 ....
+ 1
thanks
+ 1
thanks bro
+ 1
donot worry cause im not a stranger