# [SOLVED] Understanding if else statements

• 11-18-2008, 05:10 AM
hungdukie
[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. :)
• 11-18-2008, 05:51 AM
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
• 11-18-2008, 06:18 AM
hungdukie
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");
}
}
• 11-18-2008, 06:24 AM
hungdukie
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");
}
}
}
• 11-18-2008, 07:02 AM