Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How do you read this syntax?

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do you read this syntax?

    Hi everyone,

    I am preparing for a Java exam at the moment and I came across the following syntax and don't understand how to read it or what exactly it does:
    i = (j>1)?2:1;

    the whole code block where this is in is as follows:
    int i, j=1;
    	i = (j>1)?2:1;
    	switch(i){
    	case 0: System.out.println(0); break;
    	case 1: System.out.println(1);
    	case 2: System.out.println(2); break;
    	case 3: System.out.println(3); break;
    	}
    The print out from this is
    1
    2
    Last edited by helloworld922; August 8th, 2010 at 03:41 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How do you read this syntax?

    Please remember to include code tags.

    Code:
    int i, j=1;
    i = (j>1)?2:1;
    switch(i){
    case 0: System.out.println(0); break;
    case 1: System.out.println(1);
    case 2: System.out.println(2); break;
    case 3: System.out.println(3); break;
    }

    Is there supposed to be a break statement after case 1: System.out.println(1);?

    I'm not sure how to read i = (j>1)?2:1, so I'm curious what the answer is too.

    But, if I had to take a guess, I would say this:

    the statement (j>1) is a boolean, which would result in false since j==1.
    the ? character probably indicates a decision for the value of the int based on the preceding boolean.
    the statement 2:1 probably gives options based on the boolean result, where before the colon is the true value and after the colon is the false value.

    Which means that i = (j>1)?2:1 will make i==1, which would work with your switch statement. Then, since case 1: System.out.println(1); doesnt include a break statement, it goes to the next switch statement afterwards, which is case 2: System.out.println(2); break;, which would give the printout:
    1
    2
    If I'm right, then that is some damn good guessing. If I'm wrong, I think I did a good job justifying it.
    Last edited by aussiemcgr; August 8th, 2010 at 08:27 AM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do you read this syntax?

    It seem silly to argue about what some lines of code do when you can compile and execute them to see.
    If more info is needed, add println()s to show the values of variables as they change and to show execution flow.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do you read this syntax?

    i = (j>1)?2:1;

    ...is a shorthand way of writing if/else (aka ternary operator). If the condition on the left evaluates to true, it returns the value after the question mark, if false it returns the value after the colon.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do you read this syntax?

    Hi,

    thanks to everyone for your very helpful comments.
    I hadn't seen this kind of notation before but it appeared in a practice test.

    Many thanks!!

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How do you read this syntax?

    Quote Originally Posted by copeg View Post
    i = (j>1)?2:1;

    ...is a shorthand way of writing if/else (aka ternary operator). If the condition on the left evaluates to true, it returns the value after the question mark, if false it returns the value after the colon.
    I'm a little proud of myself now.

Similar Threads

  1. New Syntax Highlighting Feature!
    By JavaPF in forum Forum Updates & Feedback
    Replies: 15
    Last Post: July 14th, 2010, 09:20 AM
  2. I need ideas on how to read this
    By thursgun in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: April 21st, 2010, 09:31 PM
  3. CPU throttling at 100% during I/O read
    By v2102ap in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 21st, 2010, 05:30 AM

Tags for this Thread