Chapter 4: Basic Control Structures.

The basic control structures enable us to accomplish much more than the sequential statements we have used so far. It lets us test conditions and depending upon the result, do one thing or the other (if we use the if control structure). When we use the looping structures, we are able to do one thing many times.

The IF statement: To use the if statement to carry out tests, we need to use the relational operators: 1. <  Less than. 2. >  Greater than. 3. <=  Less than or equal to. 4. >=  Greater then or equal to. 5. ==  Equal to. 6. !=  Not Equal to.

These operators apply to numbers (both integer and double). If two numbers are tested, one of which is an integer and the other is double, the integer will be converted to double and then tested. Note: We cannot use the relational operators (<, <=, > & >=) to test objects. We could however, use the equality operators(== & !=) to compare objects. But this may not give us the results that we may be seeking because these operators only compare the value stored in the objects (that is, the address in them) and not the actual contents of the objects. Thus we would not use these to test strings. We use instead the method equals() used in the following manner. object1.equals(object2) In the expression above, we are testing to see if object1 is equal to object2. We can also use the equalsIgnoreCase() to compare two strings. This will compare the two strings ignoring the case of the characters, example: object1.equalsIgnoreCase(object2) To test if one string is less than, equal to or greater than another, we will use the compareTo() method that is available in the library. This method returns integer values – for example: 1. if (string1 < string2)  return value  < 0 (maybe –1) 2. if (string1 == string2)  return value  0 3. if (string1 > string2)  return value  > 0 (maybe 1)

How to use this method: string1.compareTo(string2);

The compareTo() method will go through each character of both strings and compare them. It will then return a value.The Unicode values are used to determine if one character is less than another. Note: Upper case A is smaller than Lower case a, thus if

1 these were tested (if string1.compareTo(string2): with string1 consisting the value A and string2, the value a) then a value less zero would have been returned. If statements:

The if statement allows us to test a condition and decide upon an action depending on the outcome of the test. The structure of the if statement is:

if (expression) statement

The expression must produce a boolean type because an if statement is geared to always return a true of false answer. The brackets around the expression are mandatory. The evaluation of the if statement is as follows: The expression (in the form of a condition) is evaluated and if it is true, then whatever statements follow the if would be executed. If it is false, nothing would happen (in the above example). Indentation makes if statement more clear and easier to understand, especially when numerous nested if’s are involved. Note: if we write  if (x > y); a = 25; this is incorrect. There should not be a semicolon after the second brace. The compiler will compile this correctly, but depending upon the situation, it could produce error in output. Fortunately, in this case we will get the result we are looking for providing that x is greater than y. This if statement is considered an empty statement and practically does nothing. Blocks: After the test condition, you may want to have more than one statement in the statement area. To do so, they must be placed inside a block. Thus open with the curly brace and close with a curly brace. In side you place any amount of statements with each ending with a semicolon. After the last curly brace, there must be no semicolon.

The else statement:

When we test with the if statement, we had only one area for action – and that was if the condition tested was true. If the condition tested is false, we may want to do something else thus we use the else statement. The structure is as follows:

If (expression) { statement; statement; } else { statement; statement; }

2 Performing the And operation:

The and operator is the && symbol. The and operation is used to test compound expression, example if ( i < 1) && (i > 10) Do something; In the above test, if i is any value less than 1 and i is greater than 10, the statements that follow inside of the if statement would be executed. Other wise, those statements would be skipped. Note that if the first condition return false, the other condition would not be tested (short circuit). Both conditions must be true for the statements to be executed.

Performing the Or operation:

The or operator is the || symbol. Also used to test compound expression, example

if ( i < 1) || (i > 10) Do something;

In the above test, if i is any value less than 1 or greater i is greater than 10, the statements that follow inside of the if statement would be executed. Other wise, those statements would be skipped. Note: in this case only one condition has to be true. When the first condition is tested and it evaluates to true, the others will not be tested.

Performing the Not operation:

The not operator is the ! symbol. In this case a condition or more than one conditions are tested. The return value after the test is “notted” i.e. the opposite is returned as the final result. For example, if we were testing two strings for inequality, we could write it in the following manner: !str1.equals(str2) or if (!(9 > 11)) evaluates to !(false)  thus true.

In terms of precedence, the && operator evaluates first followed by the || and then the !. Example: !((a < b) || ((c

3 The Dangling else: If we have the following code which should mean that the else should be attached to the first if statement: if (a < b) if (a > c) total = total + a; else total = total + b; The else statement could be misinterpreted to belong to the second if when it was intended for th first if. In fact the compiler will associate it with the second if (the association is made with the first if up the ladder). For this to be corrected, we need to put the second if in a block, example:

if (a < b) { if (a > c) total = total + a; } else total = total + b;

Loops:

Most often than not, we need our algorithm to do something over and over until a condition is met. To do so we use loops. There are two main types of loop available – the while loop and the for loop. For either loop, we must always make sure that the loop will terminate at some point in time, else it would be executing forever.

While loop:

The while loop is split into two parts (or two format): 1. When the while statement is placed at the beginning of the loop. 2. When the do statement is placed at the beginning of the loop.

In the first case, because the condition is tested before entering the loop, the loop may not be executed at all. Example, if we are reading data from a file using a while loop, we could have the following syntax:

while(x > y) //test to see if x is greater than y. { //if it is, then go into the loop. a = b * c; ……… }

So a while loop would test the condition – if it is true, then execute the loop.

4 In the use of the do, we have:

do { a = b * c; ……… } } while(x > y);

Using the while loop for counting:

We do not want an infinite loop, as the following:

while (true) { int i = 0, j = 0; System.out.println(i++); System.out.println(++j); }

Generally, when ++ and -- operators are used in expressions, they cause the value of the attached variable to either increase by 1 or decrease by 1. These could be placed either before the variable or after the variable as in the above example. In the above case, when placed after the variable, the value for the variable would be printed then increased. When placed before the value of the variable would be increased, then printed. So in the first case, the output would start from 0, whereas in the later case, it would start from 1.

However, this happens only as in this case, when they are put inside a print statement. If they are used inside a program other wise, it does not matter where they are place. Same is true for the – operator.

Also note that the above code would produce an infinite loop. We could add a break statement to get out of the loop – as follows.

while (true) { int i = 0, j = 0; System.out.println(i++); System.out.println(++j); if(I == 50) || (j == 50) break; }

5