AboutArtemus Harper Expertise I have a BS in computer science and am working towards a Masters degree.
Experience I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection, and Java java.nio.*
knowledge of Java bytecode and annotations.
Basics in c++ and c#
Expert: Artemus Harper Date: 7/1/2008 Subject: If Else statements and ; expected
Question I am using If Else statements in a form of repeating Else If statements.
If (calculated = 10); {
return 55;
}Else If (calculated = 9); {
Is in my code but on the first Else it asks for a semicolon. Why would this be?
Answer The compiler is very confused on what you are trying to do since this has many problems.
The problems are:
1. if and else are all lower case, it won't work if any of the letters are upper case
2. Use == in an if statement, not =. == is used for comparison, while = is used for assignment.
3. Do not put a ; after the if (...) statement (or any where a { will follow)
So your code should look like:
if (calculated == 10) {
return 55;
} else if (calculated == 9) {
...
Also note that if you are planning to do each number, a switch statement works better:
switch(calculated) {
case 10:
return 55;
case 9:
//do something
case 8:
//do something else
...
default:
//do something when none of the other values are met
}
note that with switch statements, if you do not use a return, use a break instead.