CS170 Lab: Control of Flow

Highlights of this lab:

Program flow is the order in which a program executes its statements. Most program flow is sequential, meaning that the statements are executed one by one in the order in which they appear in the program or method. However, there are Java commands that make your program jump forward or backward, skipping over program code not currently required. These commands are said to control the program flow, and are discussed in this section.  Highlights of this lab include:

Lab assignment:

For this assignment you will be asked to write a program which reads input from the keyboard and executes some statements using the control of flow statements that you will learn today. For details, please click:

1. Boolean Values

Boolean values should really be discussed as a data type in Java; it is discussed here, because it is often used with the if statement which we will deal with later on in this section.

Many times in a program, you need a way to determine if a specific condition has been met. For example, you might need to know whether a part of your program executed properly. In such cases, you can use Boolean values, which are represented in Java by the boolean data type. Boolean values are unique in that they can be only one of two possible values: true or false.

You declare a boolean value like this:

boolean identifier;
boolean identifier = value; 
In the second example, value must be true or false. In an actual program, you might write something like this:
boolean file_okay = true;

Following are some statements which give you some ideas of how else the boolean value can be used in your Java programs.  They demonstrate that a boolean value does not have to be assigned true or false explicitly, as these two values can be obtained from the execution of other statements.

int total = 15;
boolean answer3 = (total == 15); // answer3 now contains the value 'true'
boolean answer4 = (total != 15); // answer4 now contains the value 'false'
boolean answer5 = (total < 15);  // answer5 now contains the value 'false'
boolean answer6 = (total <= 15); // answer6 now contains the value 'true' 
boolean answer7 = (total > 15);  // answer7 now contains the value 'false' 
boolean answer8 = (total >= 15); // answer8 now contains the value 'true'
boolean answer9 = (total == 15 / 3 * 2); // figure this out yourself. 
boolean answer9 = (total != 10 - 12 % 7 * 2 / 5 + 3)// figure this out yourself.

2.The if and if-else Statement

Most conditional branching occurs when the program executes an if statement, which compares data and decides what to do next based on the result of the comparison. For example, you've probably seen programs that print menus on-screen. To select a menu item, you often type the item's selection number. When the program receives your input, it checks the number you entered and decides what to do. You'd probably use an if statement in this type of program.

A simple if statement includes the keyword if followed by a logical expression, which, as you learned above, is an expression that evaluates to either true or false. In the following example, statements gets executed if the value of boolean_expressionis true.

if (boolean_expression)
If there are more than one statement to be executed, these expressions are surrounded by parentheses (called "block"). You follow the parentheses with the statement that you want executed if the logical expression is true. For example, look at this if statement:
if (age1 == 10)
	System.out.println ("The age1 is 10");
	System.out.println ("The person is a kid");

A statement must end with a semicolon, ";". However, do not place a semicolon after the block terminator character "}".

Having learned how to use an ' if' statement, it is not difficult to understand the ' if-else' statement.

The syntax of The if-else Statement is as follows:

if (boolean_expression)
if (age == 10)
	System.out.println ("The age is 10");
	System.out.println ("The age is not 10");

if (age1 == 10) {
        System.out.println ("The age1 is 10");
        System.out.println ("The person is a kid");
else {
	System.out.println ("The age1 is not 10");
	System.out.println ("The person may not be a kid");
Note that if's, and if-else 's can be nested and the rules for blocks mentioned for 'if' statement also apply for ' if-else' statements. Be sure that your braces match up.

3. Assignment


  1. Instruction 1
  2. Instruction 2
  3. Instruction 3
  4. Instruction 4

Due date:


Your source codes and script file that records the running of your program.

This page was modified by Qiang Hu: Thursday, 11-Nov-2004 11:15:58 CST.
Copyright: Department of Computer Science, University of Regina.

[Guide to Online Writing] [CS Dept Home Page]