Results 1 to 7 of 7
 11182008, 04:10 AM #1Member
 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. :)
 11182008, 04:51 AM #2Member
 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; } } }
 11182008, 05:18 AM #3Member
 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");
}
}
 11182008, 05:24 AM #4Member
 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");
}
}
}
 11182008, 06:02 AM #5Member
 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
 11182008, 07:16 AM #6Member
 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?
 11182008, 07:56 AM #7
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 26
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

[SOLVED] Help with understanding nullpointexcepion
By soxfan714 in forum New To JavaReplies: 4Last Post: 11112008, 04:51 AM 
Understand my logic errors and better understanding method and class creation
By freethinker89 in forum New To JavaReplies: 3Last Post: 10062008, 11:03 PM 
if else statements and using images
By Joshr in forum New To JavaReplies: 8Last Post: 10052008, 06:51 PM 
Understanding Vectors
By cbrown08 in forum New To JavaReplies: 7Last Post: 11052007, 06:56 PM 
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07242007, 07:56 PM
Bookmarks