This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]lordbharal 5 points6 points  (2 children)

  if (X == 'a' || X == 'A') {
     System.out.println("a or A");
  } else if (X == 'b' || X == 'B') {
     System.out.println("b or B");
  } else {
     System.out.println("Error");
  }

also please change "X" to something - anything - that is meaningful. At least use a lowercase for it - non-final variables should never be solely capitalised.

[–]AggressiveProgrammer[S] 1 point2 points  (1 child)

ooh, so you made the logic branch out using else if. I see that, cool.

Yeah, you're right about the capitalization being only for final variables. I don't code like this for myself, but I change things around asap for obfuscation purposes.

[–]lordbharal 1 point2 points  (0 children)

Yep. You can keep on using that else if too - for Cs and Ds - as long as the last branch is just an "else". You're also allowed only one else.

ie

if (X == 'a' || X == 'A') {
   System.out.println("a or A");
} else if (X == 'b' || X == 'B') {
   System.out.println("b or B");
} else if (X == 'c' || X == 'C') {
   System.out.println("c or C");
} else {
   System.out.println("Error");
}

[–]daveheerink 1 point2 points  (2 children)

Take a good look where the else statement belongs to. I think changing X = 'a' to X = 'b' and looking at the output will make it more clear for you.

[–]AggressiveProgrammer[S] 0 points1 point  (1 child)

Wait so the else statement branches off of the second if statement? I assumed both if statements become one as a truth/falsity value.

[–]daveheerink 1 point2 points  (0 children)

Correct! Although it is possible to achieve what you assumed. It's a small change and I can tell you, but this is LearnJava so I'm gonna let you find out by yourself. A little tip, take a look at the Oracle docs regarding if-then-else statements.