Results 1 to 7 of 7
  1. #1
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default [SOLVED] Understanding if else statements

    I am having a hard time understanding how to evaluate if else statements. I am a total newbie to Java but I am really trying hard to learn. I am trying to figure out how the following works:
    Based on the code below, what is the value of y if x = 5?
    int x, y;
    if(x > 5)
    y= 1;
    else if (x < 5){
    if(x < 3)
    y = 2;
    else
    y= 3;
    }
    else
    y = 4;
    I am guessing that since x is = to 5 then y is = to 3 because of the closing brace but I am not really sure how this works. Can anyone walk me through how the logic flows in this code? I would really appreciate it. Also what if the value of x started out to be a different value like 6 or 3 and what would x be if y was 2?
    I am guessing that if x was 6 y is 1 and if x is 3 y would be 4. Finally, if y were found to be 2 x could be 2 or 1. I am I way off base here or what? I appreciate any help you all can give me to understand this stuff. :)

  2. #2
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    Java Code:
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    
    		int x = 5;
    		int y;
    		// if x is strictly larger than 5 this statement is true, if x equals 5
    		// or is less than 5 this is false
    		if (x > 5) {
    			y = 1;
    		}
    		// if the "if" statement evaluated to false, continue.
    		// if x is strictly less than 5 the following statement is true, if x equals 5 or
    		// is greater than 5 this is false
    		else if (x < 5) {
    			// if x is strictly less than 3 the following statement is true, if x equals
    			// 3 or is greater than 3 this is false
    			if (x < 3) {
    				y = 2;
    			}
    			// if the previous "if" statement evaluated to false execute the the
    			// following "else" statement
    			else {
    				y = 3;
    			}
    		} 
    		// if the "else if" statement evaluated to false, execute the following "else" statement
    		else {
    			y = 4;
    		}
    	}
    }
    put System.out.println() statements throughout the code to see what happens to y

  3. #3
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    I am not sure exactly what to write for out put statements. Would I just put System.out.println() after each if, else if, and else statement? Like this?
    public class Test {

    /**
    * @param args
    */
    public static void main(String[] args) {

    int x = 5;
    int y;
    // if x is strictly larger than 5 this statement is true, if x equals 5
    // or is less than 5 this is false
    if (x > 5) {
    y = 1;
    System.out.println("y");
    }
    // if the "if" statement evaluated to false, continue.
    // if x is strictly less than 5 the following statement is true, if x equals 5 or
    // is greater than 5 this is false
    else if (x < 5) {
    // if x is strictly less than 3 the following statement is true, if x equals
    // 3 or is greater than 3 this is false
    if (x < 3) {
    y = 2;
    System.out.println("y");
    }
    // if the previous "if" statement evaluated to false execute the the
    // following "else" statement
    else {
    y = 3;
    System.out.println("y");
    }
    }
    // if the "else if" statement evaluated to false, execute the following "else" statement
    else {
    y = 4;
    System.out.println("y");
    }
    }

  4. #4
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    I think I just may have answered my own question about what to write for output statements. Please bear with me, I am very new but I will learn Java no matter what it takes!
    This is what I should use for what you suggested I do right?:
    public class Test {

    /**
    * @param args
    */
    public static void main(String[] args) {

    int x = 5;
    int y;
    // if x is strictly larger than 5 this statement is true, if x equals 5
    // or is less than 5 this is false
    if (x > 5) {
    y = 1;
    System.out.println("y is 1");

    }
    // if the "if" statement evaluated to false, continue.
    // if x is strictly less than 5 the following statement is true, if x equals 5 or
    // is greater than 5 this is false
    else if (x < 5) {
    // if x is strictly less than 3 the following statement is true, if x equals
    // 3 or is greater than 3 this is false
    if (x < 3) {
    y = 2;
    System.out.println("y is 2");
    }
    // if the previous "if" statement evaluated to false execute the the
    // following "else" statement
    else {
    y = 3;
    System.out.println("y is 3");
    }
    }
    // if the "else if" statement evaluated to false, execute the following "else" statement
    else {
    y = 4;
    System.out.println("y is 4");
    }
    }
    }

  5. #5
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    whatever helps you identify where the programs is executing logic is fine. you can have the statements say "entered if(x<5) statement" or something like that, and yes you can print the value of the variable "y" out where ever you would like to know what it is

  6. #6
    hungdukie is offline Member
    Join Date
    Nov 2008
    Posts
    32
    Rep Power
    0

    Default

    That is great. Thank you so much for your help! I am new here so how do I mark the thread as solved?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    There is a menu on top of the first post in this thread. Click on Thread Tools. You can see an option to mark the thread solved.

    And also please use code tags next time when you posting codes. Unformated codes are very difficult to read.


Similar Threads

  1. [SOLVED] Help with understanding nullpointexcepion
    By soxfan714 in forum New To Java
    Replies: 4
    Last Post: 11-11-2008, 05:51 AM
  2. Replies: 3
    Last Post: 10-07-2008, 12:03 AM
  3. if else statements and using images
    By Joshr in forum New To Java
    Replies: 8
    Last Post: 10-05-2008, 07:51 PM
  4. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 07:56 PM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:56 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
  •