Java Conditionals
|
Warning: These notes have been collected from several different sources - each uses different terminology... they may even be contradictory... Conditions... if, while, loops, for, switch, case, continue... A single statement after a condition does not require surrounding curly braces {}. If two statements are made and the curly braces are forgotten, only the first statement will execute for the condition and the next will execute reguardless of the condition. For standardization and simplicity I will always use curly braces, even if only one statement is made. Curly braces should align with/under the condition, the statements within are then indented.
| ||||
|
A boolean expression; is something that evaluates to true or false. The boolean expression is always surrounded by parentheses. A condition is a boolean expression. The condition is always surrounded by parentheses. A condition can be very complicated, with relational operators and logical operators. A statement is an expression followed by a semicolon, basically to "do something..." However it can be (and usually is) a block statement containing several statements. The statement(s) is sometimes called the loop body. An expression is a combination of variables, literals and operators. (In Java method calls are also expressions.) Expressions can be grouped in parentheses, so you don't have to worry about operator precedence. (In Java method calls are also expressions.)
| ||||
|
Three parts of a loop must be coordinated in order for the loop to work properly: 1. The initial values must be set up correctly. for example: int count = 1; 2. The condition in the while loop must be correct. for example: while ( count <= 3 ) 3. The change in variable(s) must be set up correctly. for example: count = count + 1 or count++ In a counting loop the variable count is initialized, tested, and changed. "For" loops do this, see below...
| ||||
|
Use a switch statement when you are testing the same expression for several possible values. Each possible value is placed in a case label. A switch statement compares any integer (byte,char,short,int or long) expression against a set of specific values (not a string!)
| ||||
| ||||
| ||||
|
A counting loop uses a loop control variable to count upwards or downwards (usually by an integer increment.) Counting loops are by far the most common type of loop. A counting loop increments a counter (loop control variable) after each input line is read. To control a counting loop, a special value may be assigned (a "sentinel") that is used to determine whether to execute or exit a loop. In a sentinel controlled loop the user may enter "some predetermined value" to tell the program that the iterations (loops) are complete. The special sentinel value must not be a value that could ever occur in the data. A sentinel controlled loop reads in lines until reaching a line that contains a special value (the "sentinel"). Another type of loop is the result-controlled loop also known as free loop or general loop. It keeps looping until the computation has reached a particular goal. It is like the instruction in a cookie recipe that says "keep stirring until the ingredients are thoroughly blended." You know when to quit only when the desired result has been achieved. A result-controlled loop reads in lines until a desired result has been achieved. |
See Java Links for more info, Object & Classes , Data Types , Operators , Conditionals , Definitions , Keywords , File Structure , Code Examples , Class Skeleton , Java Notes




