Single Selection IF Statement

• 02-06-2013, 06:37 AM
SnakeDoc
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:

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

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

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!
• 02-06-2013, 07:54 AM
Estrella
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. :)
• 02-06-2013, 08:33 AM
SnakeDoc
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!
• 02-06-2013, 10:51 AM
Tolls
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.
• 02-06-2013, 05:47 PM
SnakeDoc
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...
• 02-06-2013, 06:05 PM
Tolls
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.
• 02-06-2013, 06:30 PM
SnakeDoc
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?
• 02-06-2013, 06:40 PM
Tolls
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.
• 02-06-2013, 06:54 PM
StormyWaters
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.
• 02-06-2013, 07:15 PM
Tolls
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.
• 02-06-2013, 09:48 PM
SnakeDoc
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...
• 02-06-2013, 10:02 PM
StormyWaters
Re: Single Selection IF Statement
Quote:

Originally Posted by SnakeDoc
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....

Code:

```      if (a < b)           a ++;           b ++;```
is the same as

Code:

```    if (a < b) {           a ++     }     b++```
The a ++ will only occur if the if statement is true, while b ++ will always occur.
• 02-06-2013, 10:30 PM
SnakeDoc
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...
• 02-06-2013, 10:50 PM
StormyWaters
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.
• 02-06-2013, 11:10 PM
SnakeDoc
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!