



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: PRIN OF COMPUTER PROGRAMMING; Subject: COMPUTER SCIENCE; University: Georgia State University; Term: Unknown 1989;
Typology: Exams
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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:
Greater than.
= Greater then or equal to.
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; }
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):
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; }