Forums Register Login

System.out.print(s + (b ? "T" : "F"));

+Pie Number of slices to send: Send
I understand the flow of the following code but do not understand how the syntax System.out.print(s + (b ? "T" : "F")); resolves to a proper conditional expression especially since the (b ? "T" : "F") part of the expression is never assigned to a variable but appended to one - s.


package testPackage;


public class EBH204 {
static boolean m1(String s, boolean b) {

System.out.println(" ");
System.out.println("just entered m1");
System.out.println("b before conditional operation " + b);
System.out.println("s before conditional operation " + s);
System.out.println(" ");

System.out.print(s + (b ? "T" : "F"));
System.out.println(" ");

System.out.println("b after conditional operation " + b);
System.out.println("s after conditional operation = " + s);

return b;

}

public static void main(String[] args) {
System.out.println("just entered main");
m1("A", m1("B",false) || m1 ("C", true) || m1("D",false));
//System.out.println(" A and B " + (m1("A", m1("B",false))));
System.out.println(" A and B and C " + (m1("A", m1("B",false) || m1 ("C", true))));
System.out.println("just executed m .... in main");
}
}

Output:

just entered main

just entered m1
b before conditional operation false
s before conditional operation B

BF
b after conditional operation false
s after conditional operation = B

just entered m1
b before conditional operation true
s before conditional operation C

CT
b after conditional operation true
s after conditional operation = C

just entered m1
b before conditional operation true
s before conditional operation A

AT
b after conditional operation true
s after conditional operation = A

just entered m1
b before conditional operation false
s before conditional operation B

BF
b after conditional operation false
s after conditional operation = B

just entered m1
b before conditional operation true
s before conditional operation C

CT
b after conditional operation true
s after conditional operation = C

just entered m1
b before conditional operation true
s before conditional operation A

AT
b after conditional operation true
s after conditional operation = A
A and B and C true
just executed m .... in main

Can someone advise me?

Thanks,

JerryB
+Pie Number of slices to send: Send
The tertiary operator ?: returns a value, just like a function returns a value. Just as you can do this:



or this:



you can do this:



or this



I hope that helps,
Corey
+Pie Number of slices to send: Send
Thanks Corey.

What threw me is that on page 176 at the top of K&B the code structure reads:

someVariable = (boolean expression) ? value to assign if true: value to assign if false

From reading that I believed that the results of the ?: would always need to be assigned to a variable ( because of the assignment = ) whereas it can be used to compute values, assign to a variable, or control the flow of execution.

Thanks alot.

JerryB
+Pie Number of slices to send: Send
As an add-on to Corey's answer, you can consider the expression b?"A":"B" as an actual method:
+Pie Number of slices to send: Send
Thanks alot Barry. That also helps.

:-)

JerryB
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 953 times.
Similar Threads
Operators - Dan Chisholm question
Conditional Operator
doubts in 4 questions
Conditional And-Or
Operators
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:48:34.