# Thread: [SOLVED] Understanding if else statements

1. Member
Join Date
Nov 2008
Posts
32
Rep Power
0

## [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. Member
Join Date
Jul 2008
Posts
68
Rep Power
0
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. Member
Join Date
Nov 2008
Posts
32
Rep Power
0
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. Member
Join Date
Nov 2008
Posts
32
Rep Power
0
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. Member
Join Date
Jul 2008
Posts
68
Rep Power
0
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. Member
Join Date
Nov 2008
Posts
32
Rep Power
0
That is great. Thank you so much for your help! I am new here so how do I mark the thread as solved?

7. 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.

#### Posting Permissions

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