if, while... must return a boolean| if(test condition is true) { // do something } |
if(test condition is ture) { // do something } else { // do other thing } |
if(test condition 1 is ture) { // do something } else if(test conditon 2 is true) { // do some other thing } else { // do other things } |
int a = 0;
if(a = 0) {
// do something;
}
Example 2
int a;
if(a) {
// do something
}
In below are some correct examples
int a = 0;
if(a == 0) {
// do something
}
Example 2
int a;
if(a > 0) {
// do something
}
Example
public class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
|
while (test condition) {
|
if the test conditon is true, statements are excuted, |
do {
|
execute the statments |
while(true) {
// do something
}
for (initialization; termination; increment) {
statement
}
|
initialization is an expression that initializes the loop-it's executed once termination expression determines when to terminate the loop.false, the loop terminates.increment is an expression that gets invoked after each iteration through the loop.
for ( ; ; ) { // infinite loop
...
}
Another example
public class ForTest {
public static void main(String[] args) {
if(args.length == 0) {
System.out.println("Hello");
} else {
for(int i = 0; i < args.length; i++) {
System.out.println("Hi " + args[i]);
}
}
}
}
|
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
default: System.out.println("Hey, that's not a valid month!"); break;
}
}
}
|
public class SwitchDemo2 {
public static void main(String[] args) {
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if ( ((year % 4 == 0) && !(year % 100 == 0))
|| (year % 400 == 0) )
numDays = 29;
else
numDays = 28;
break;
}
System.out.println("Number of Days = " + numDays);
}
}
|
| Number of Days = 29 |
return StatementThe last of Java's branching statements is the return statement. You usereturnto exit from the current method. The flow of control returns to the statement that follows the original method call. Thereturnstatement has two forms: one that returns a value and one that doesn't. To return a value, simply put the value (or an expression that calculates the value) after thereturnkeyword:The data type of the value returned byreturn ++count;returnmust match the type of the method's declared return value. When a method is declaredvoid, use the form ofreturnthat doesn't return a value:return;