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: Questions about if statements and if-else statements

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Questions about if statements and if-else statements

    I have an exam on monday on java programming and i'm still a bit confused about a certain question involving nested if statements, the question is:

    What is the output of the following code:

    int x= 9;
    int y = 8;
    int z = 7;

    if ( x> 9 )
    if ( y > 8)
    System.out.println(" x > 9 and z>=7");
    else if ( z>= 7 )
    System.out.println(" x <=9 and z>= 7" );
    else
    System.out.println(" x <=9 and z , 7 ");






    the answer turned out to be" none of the above ", but I'm confused since when you have a nested if statements like this, why wouldn't it output "x<= 9 and z>= 7" since the condition for z is true?



    I get that the condition for 9 being greater than x isn't true, so it moves onto the next if statement, and y is not greater than 8, so wouldn't it jump to the ' else if statement ' and output that? Or would it skip everything past "y>8" since that condition isn't true? Just a bit confused.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,309
    My Mood
    Mellow
    Thanks
    252
    Thanked 780 Times in 765 Posts

    Default Re: Questions about if statements and if-else statements

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.
    I get that the condition for 9 being greater than x isn't true, so it moves onto the next if statement
    Not exactly. In this case, the next if statement is only executed if the first if statement is true. Formatting (and posting) the code 'correctly' may help you see:
    int x= 9;
    int y = 8;
    int z = 7;
     
    if ( x > 9 )
    {
    	if ( y > 8)
    	{
    		System.out.println(" x > 9 and z>=7");
    	}
    	else if ( z >= 7 )
    	{
    		System.out.println(" x <=9 and z>= 7" );
    	}
    	else
    	{
    		System.out.println(" x <=9 and z , 7 ");
    	}
    }
    Now examine the code and determine what happens if the first if statement is false.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Questions about if statements and if-else statements

    The way my professor presented the problem in the practice exam, is without the brackets. Because they weren't shown, are they implied to be there? Would it still produce the same output without the brackets? This is the syntax that she used to format the problem:


    int x= 9;
    int y = 8;
    int z = 7;


    ( the periods represent spacing since for some reason it's not showing what i'm typing spaced)



    if ( x> 9 )
    ....if ( y > 8)
    ..System.out.println(" x > 9 and z>=7");
    else if ( z>= 7 )
    ..System.out.println(" x <=9 and z>= 7" );
    else
    ..System.out.println(" x <=9 and z , 7 ");



    Would the exclusion of brackets change the output, or would it still be the same?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,309
    My Mood
    Mellow
    Thanks
    252
    Thanked 780 Times in 765 Posts

    Default Re: Questions about if statements and if-else statements

    The reason the problem was presented without the brackets was to test your understanding and suck you into making the mistake you made. The results are exactly the same. You can answer your questions by coding a quick example of each and running it.
    the periods represent spacing since for some reason it's not showing what i'm typing spaced
    That's why we ask you to use code tags to display code. The formatting is preserved between code tags.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Questions about if statements and if-else statements

    So using something else as a similar example:

     
    if (score >= 90) grade = 'A';
    if (score >= 80) grade = ' B ';
    if (score >= 70 ) grade = 'C';
    if (score >= 60 ) grade = 'D';
    else grade = 'F';


    in this case, if i were to input a value for example, because conditions 'A' through 'D' are true, then the code " would only work correctly if the grade <70", since D or F would be the output? just making sure I have the logic correct, I tend to overthink way too much

    like If I were to put in 92, conditions A-D are true, so D is printed

    if I were to put in 73, conditions c-d are true, d is printed

    56 is put in, F is only printed.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,309
    My Mood
    Mellow
    Thanks
    252
    Thanked 780 Times in 765 Posts

    Default Re: Questions about if statements and if-else statements

    Chained if statements in which only one of the statements can be correct should include 'else'. What you've written is very similar to a switch statement with no breaks and a default result.

    Over thinking can be reinforced by and tempered with more coding. You should be turning this thinking into code and wringing out the possible results by playing with that instead of theorizing what might happen and maybe being wrong.

Similar Threads

  1. help with if statements
    By tonynsx in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 30th, 2013, 05:28 PM
  2. Help my if statements!
    By l2code in forum Loops & Control Statements
    Replies: 1
    Last Post: March 22nd, 2013, 02:47 PM
  3. [SOLVED] If and else if statements???
    By skeptile2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 5th, 2013, 01:44 AM
  4. Stuck on a few Statements/Questions on timing of Main Game Loop.
    By StevenW in forum Java Theory & Questions
    Replies: 3
    Last Post: July 19th, 2011, 10:20 AM
  5. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 09:42 PM