Control Flow

Statements in Kenya do not all have to be assignments, method calls and returns. The flow through a program can be manipulated using loops and conditionals.

For Loops

The for-loop construct is very powerful and flexible. Its syntax is:

	
	for( loop_start ; loop_condition ; loop_update ) {
		statements...
	}
	

What happens is that loop_start is executed just before any loops, loop_condition is tested each time round the loop, and if and only if that returns true are the statements executed. Then after the statements are executed, loop_update is executed, then the loop_condition is tested again, and thus the loop loops.

The most common use for a for loop is to do something to every element of an array; e.g:

	void main(String[] args){
		//prints out the provided arguments to the program.
		for( int i = 0 ; i < args.length ; i++ ){
			println( args[i] );
		}
	}

However there are many other things you can use them for aswell, as loop_start and loop_update can be (almost) any appropriate statement, loop_update is also optional (i.e. you don't have to put it there).

If you are in the middle of a for loop and wish to exit it prematurely, you can use the keyword break to immediatly leave the current loop.

Example 1.3. For Loop Example

	
	void main(){
		
		int[] x = { 1, 3, 2, 5, 4 };
		
		
		for( int i = minArray(x) ; i <= maxArray(x) ; i++ ){
			for( int j = 0 ; j < x.length ; j++ ){
				if( x[j] == i ) {
					println("The number " + i + " is in the array at index " + j );
					break;	// this breaks out of the first for loop.
				}
			}
		}
		
	}
		

	int minArray( int[] a ){
		int minVal = a[0];
		
		for( int i = 0 ; i < a.length ;i++){
			if( a[i] < minVal ){
				minVal = a[i];
			}
		}
		return minVal;
	}		
	
	int maxArray( int[] a ){
		int maxVal = a[0];
		
		for( int i = 0 ; i < a.length ;i++){
			if( a[i] > maxVal ){
				maxVal = a[i];
			}
		}
		return maxVal;
	}	
	

While Loops

The syntax of a while loop is below. Its general meaining is "while the condition is true, execute the loop".

	vhile ( condition ) {
		statements ...
	}

It tests a condition before each iteration of the loop, if it is true the loop is executed, if it false, we leave the loop.

As with for loops, the break keyword can be used to prematurely exit from the loop.

Example 1.4. While Loops Example

	void main(){
	
		println("Whatever you say, I can say too :)");
		
		while( !isEOF() ){
			String s = readString();
			
			if( s == "stop" ){
				break;
			}
			
			println( s );
		}
	
	}

If Statements

if statements can come in three very similar varieties, as shown below:

	if( boolean_condition ) {
		statements executed only if condition is true;
	}
	
	
	if( boolean_condition ) {
		statements executed only if condition is true;
	} else {
		statements executed only if condition is false;
	}
	
	
	if( condition1 ) {
		statements executed only if condition is true;
	} else if ( condition2 ) {
		statements executed only if condition1 is false and condition 2 is true.
	} //else//else if here as appropriate

With chained if-then-else if-then-else if statements, it is important to remember that the first true block is executed, and none of the rest.

Example 1.5. If Statement Example

	
	void main(){
	
		String s = "hi";
		
		int j = 3;
		
		if ( j == 3 ) {
			println("j == 3");
		} else {
			println("j != 3");
		}
		
		if( s == "hi" ){
			println(s);
		}
		
		
		if( j != 3 ){
			println("j not 3");
		}else if( s == "hi" ){
			println("s = hi");
			j = 4;
		}else if( j == 4 ){
			//even tho j gets set to 4 above,
			//because this is in an else, this
			// line is not executed.
			println("j == 4");
		}
		
		
		if( j == 4 ){
			println("but j == 4");
		}	
	
	}
	

Switch Statements

Switch statements allow you to select code to execute depending on the value of an int (or char).

The basic syntax of a switch is as follows:

	switch( int_expression ) {
		
		case int_constant_expression:{
			statements;
			break;		
		}
		case int_constant_expression:{
			statments;
		}
		default:{
			statements;
		}
	}		

The program jumps to the case that matches the value of the int, and executes the statements for that case, and all the cases directly below it in order.

To stop this behaviour (which is known as falling-through), a break statement can be used to leave the switch statement.

If none of the cases match, the default case is executed (if present), or (if there is no default case) the entire switch is skipped.

Example 1.6. Switch Example

	void main(){
		for( int i = 0 ; i < 6 ; i++ ){
			printSomething(i);
			println();
		}
	}
	
	
	void printSomething(int i){
		
		switch( i ) {
			case 0:{
				println("Case 0");			
			}
			case 2:{
				println("Case 2");
				break;
			}
			case 4:{
				println("Case 4");	
			}
			default:{
				println("Default");
				break;			
			}
			case 1:{
				println("Case One");
			}
			case 3:{
				println("Case Three");
			}
		}
	}