PIC20A Lecture 4 : Flow Control

In this section, you will learn

if

You must read the textbook p.102-104 or The if/else Statements
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
}

Warning : The test statement must return boolean .
The following codes are correct in C++ but have compiling errors in Java
Example 1
int a = 0;
if(a = 0) {
   // do something;
}
Example 2
int a;
if(a) {
   // do something
}
In below are some correct examples
Example 1
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

You must read the textbook p100-101 or The while and do-while Statements
while (test condition) {
statement(s) }
if the test conditon is true, statements are excuted, 
until it becomes false.

do {
statement(s) } while (test condition);
execute the statments 
test the condition,
if the condition is false, stop.
if not, continue, till the condition becomes false.

Warning : The test statement must return boolean .
Infinite loop
You can use while to write an infinite loop.
while(true) {
// do something
}

for

You must read the textbook p101-102 or The for Statement
for (initialization; termination; increment) {
    statement

}
The initialization is an expression that initializes the loop-it's executed once
at the beginning of the loop.

The termination expression determines when to terminate the loop.
This expression is evaluated at the top of each iteration of the loop.
When the expression evaluates to false, the loop terminates.

Finally, increment is an expression that gets invoked after each iteration through the loop.


All these components are optional. In fact, to write an infinite loop, you omit all three expressions:
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]);
	    }
	}
    }
}

Switch

You must read the textbook p.105-107 or The switch Statement
Example 1
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;
       }
    }
}


Example 2
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);
    }
}
The output from this program is:
Number of Days = 29

The return Statement

The last of Java's branching statements is the return statement. You use return to exit from the current method. The flow of control returns to the statement that follows the original method call. The return statement 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 the return keyword:
return ++count;
The data type of the value returned by return must match the type of the method's declared return value. When a method is declared void, use the form of return that doesn't return a value:
return;