Results 1 to 15 of 15
  1. #1
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Post Single Selection IF Statement

    Hello,

    I've been told to use only the Single Selection IF Statement Structure for an assignment... but can't figure out exactly what that means. I've googled and think I may have it, but if someone could verify that would be great thanks!

    So far i've done the following:

    Java Code:
    if (firstInt > secondInt && firstInt > thirdInt) {
          System.out.println(firstInt);
    }
    if (secondInt > firstInt && secondInt > thirdInt) {
       System.out.println(secondInt);
    }
    if (thirdInt > firstInt && thirdInt > secondInt) {
       System.out.println(thirdInt);
    }
    OR

    Java Code:
    if (firstInt > secondInt) {
          if (firstInt > thirdInt) {
          System.out.println(firstInt);
       }
    }
    if (secondInt > firstInt) {
       if (secondInt > thirdInt) {
       System.out.println(secondInt);
       }
    }
    if (thirdInt > firstInt) {
       if (thirdInt > secondInt) {
       System.out.println(thirdInt);
       }
    }
    OR

    Java Code:
    if (firstInt > secondInt)
       if (firstInt > thirdInt)
          System.out.println(firstInt);
    if (secondInt > firstInt)
       if (secondInt > thirdInt)
          System.out.println(secondInt);
    if (thirdInt > firstInt)
       if (thirdInt > secondInt)
          System.out.println(thirdInt);
    I think it's the last snippet... can you confirm? Thanks!
    Last edited by SnakeDoc; 02-06-2013 at 07:31 AM.

  2. #2
    Estrella is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    16
    Rep Power
    0

    Default Re: Single Selection IF Statement

    Can you also post what your program is supposed to do, exactly? The code is not always clear, and you risk getting the right answer for the wrong question. :)

  3. #3
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Single Selection IF Statement

    it's just a code snippet that takes three integer values and compares them to find which is largest, printing the largest integer to the console. I'm mostly focused on finding out the proper terminology definition... thanks!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: Single Selection IF Statement

    There's no logical difference between the second and third ones.
    Most coding standards suggest using the second one, though.

    As for whatever "Single Selection IF Statement Structure", ask whoever asked you to do this.
    Please do not ask for code as refusal often offends.

  5. #5
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Single Selection IF Statement

    Hello Tolls - thank you for your time!

    I am aware that all 3 are essentially the same... with the first snippet (using the LOGICAL AND operator) being my preferred way of accomplishing this task as it takes up the least amount of lines...

    The problem is that my professor won't tell us what a "Single Selection IF Statement Structure" is... he just refers us to our book which has two pages on the IF statement... however this "Single Selection" phrase isn't mentioned (i've read it over and over since I'm loosing points on this assignment)... I know how to write in Java, but am self taught and didn't learn all the proper terminology, and now am in a class to learn it more completely as well as finish my degree...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: Single Selection IF Statement

    I'd not heard the phrase before, but a quick google tells me is means an if with no else.
    An if/else is a double-selection statement, as it has 2 options.
    if/elsif/else is presumably called something as well.

    Here's a random link that describes this.
    Please do not ask for code as refusal often offends.

  7. #7
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Single Selection IF Statement

    hmm... that is the gist of what I had gathered from googling around, however the first code snippet is what I submitted originally, and was marked down for not using the proper "Single Selection IF Statement Structure"... so I corrected my code to use the second snippet, and same thing again. I didn't want to just resubmit with the third snippet until I was positive I had it right (at risk of looking foolish to my professor... lol). Perhaps my professor doesn't really know what he means? lol...

    from your link it appears the "Single Selection IF Statement" does not use the braces? In my opinion that is poor coding technique as it leaves open possibility for an oversight in future revisions of the code leading to unexpected results... the braces force the programmer to realize what code block will execute after a tested positive condition... Is it possible the "Single Selection IF Statement Structure" simply does not use the braces, meaning only a single line statement will be executed after testing the condition as true? such as my third snippet?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: Single Selection IF Statement

    Well, I have no idea then.
    It could be something else around your code, but who knows?

    The braces will have nothing to do with it as it is all about flow choices.
    An if with no elsif or else has a single-selection, braces or not.
    If your prof thinks the braces are something to do with it then they are, frankly, twits.
    Please do not ask for code as refusal often offends.

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default Re: Single Selection IF Statement

    I'll throw out a guess, the professor wants a single IF statement structure. That would be one if/else if/else, not a bunch of if statements. And I would recommend always using braces.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: Single Selection IF Statement

    But that goes against all the definitions of a single-selection I have found.
    if/else is defined as a double-selection.

    Still makes the prof a twit.
    Please do not ask for code as refusal often offends.

  11. #11
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Single Selection IF Statement

    My thought was the IF statement without the braces will only ever execute a single statement if the condition is evaluated to true... even if you have multiple statements listed... whereas an IF statement with braces can execute multiple statements (whatever is inside the block). I have no idea if this is correct though and I'm rather hesitant to resubmit to my professor to have him tell me it's wrong again (i already lost the points for it, just trying to correct it so I know what he wants/means).

    Tolls, I completely agree that this is kind of a crazy requirement, if I can write the code and it does what is required, then it should be ok. In the real world I would have written the section as my first snippet and moved on without thinking... its such a trivial code example lol...

  12. #12
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default Re: Single Selection IF Statement

    Quote Originally Posted by SnakeDoc View Post
    My thought was the IF statement without the braces will only ever execute a single statement if the condition is evaluated to true... even if you have multiple statements listed... whereas an IF statement with braces can execute multiple statements (whatever is inside the block). I have no idea if this is correct though and I'm rather hesitant to resubmit to my professor to have him tell me it's wrong again (i already lost the points for it, just trying to correct it so I know what he wants/means).
    Basically an if statement without the braces is the same as an if statement with the braces around just the next line of code... You can never have multiple statements for a if statement without braces....

    Java Code:
       
         if (a < b)
              a ++;
              b ++;
    is the same as

    Java Code:
         if (a < b) {
              a ++
         }
         b++
    The a ++ will only occur if the if statement is true, while b ++ will always occur.

  13. #13
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Single Selection IF Statement

    Thanks StormyWaters -- that was exactly what I was referring too... with the exception that the braces allow the programmer to stuff more than one statement to be executed if the condition evaluates to true... so my thoughts are without the braces is possibly the "Single Selection IF Statement Structure" in that it can only and will only ever execute a single statement if it is true (followed by the rest of the program), whereas an IF statement with the braces CAN execute multiple statements (meaning it's not a single statement, but multi-statement) --- possibly? Confusing... isn't it? I wish someone on here has actually seen this terminology used before... would be helpful before I resubmit my code again... lol...

  14. #14
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default Re: Single Selection IF Statement

    I dunno, even a if statement with braces can be setup so that it will execute only 1 statement, or even no statements...

    You probably won't find out until you ask your professor exactly what he is looking for. I am still leaning towards the single if/else if/else statement as you said he already marked you down for snippets 1 and 2, and 3 is essentially the exact same thing as 2, which is essentially the same thing as 1. The only thing that I can think of that a professor would be looking for is the if/else if/else structure being used, but I'm not the professor so I can't be sure.

  15. #15
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Single Selection IF Statement

    Ok... i'll probably just suck it up and ask him outright... hoping he doesn't think i'm not doing my bookwork or something (it's honestly not in the book! lol)... Thanks for everyone who helped me out with this question... if someone else in the future runs across this thread and has heard this terminology before -- please let me know! Thanks again guys!

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  2. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  3. Key selection for SSL/TLS
    By OrangeDog in forum Advanced Java
    Replies: 3
    Last Post: 04-27-2009, 10:38 PM
  4. Single and Multi Selection Lists
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:03 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •