Section 6.8. Concepts Summary


[Page 205 (continued)]

6.8. Concepts Summary

We have covered boolean expressions, conditionally executing code using if and else, combining boolean expressions using "and" (&&) and "or" (||) and method overloading.


[Page 206]

6.8.1. Boolean Expressions

A boolean expression is one that results in true or false. The values TRue and false are reserved words in Java. Here are some example boolean expressions.

> int x = 20; > System.out.println(x <= 30); true > System.out.println(x > 30); false > System.out.println(x == 20); true > System.out.println(x != 20); false


Notice that to check for a variable having a value we use == not =. The = is used to assign a value to a variable, not check for equality. To check for inequality use !=.

6.8.2. Combining Boolean Expressions

You can combine boolean expressions with the Java operators && to represent "and" and || to represent "or". When you use && both boolean expressions must be true in order for it to return true. If the first boolean expression isn't true the second won't even be tested. This is called short-circuit evaluation. When you use ||, only one boolean expression must be true in order for it to return true. With ||, if the first boolean expression is true, the second won't be evaluated. If the first boolean expression is false, the second one will still be evaluated.

> int x = 3; > int y = 5; > System.out.println(x < 5 && y < 6); true > System.out.println(x > 5 && y < 6); false > System.out.println(x < 5 && y > 6); false > System.out.println(x > 5 && y > 6); false > System.out.println(x > 5 || y < 6); true


6.8.3. Conditional Execution

To conditionally execute one statement, use the if keyword followed by a boolean expression inside an open and close parenthesis. Put the statement that you only want executed if the boolean expression is true on a new line and indent it. If the boolean expression is false, then execution will continue with the next statement.

if (boolean expression)    // statement to execute if the boolean expression is true    statement // next statement statement



[Page 207]

To conditionally execute a block of statements, use the if keyword followed by a boolean expression inside of an open and close parenthesis. Put the statements to be executed when the boolean expression is true inside of an open and close curly brace. Indent the statements to make it easier to visually see that these statements will only be executed if the boolean expression is true. If the boolean expression is false, execution will continue with the statement following the close curly brace.

if (boolean expression) {    statements }


If you want to execute one block of statements if the boolean expression is true and another if it is false, use the else keyword as well. Put the statements that you want to execute when the boolean expression is true inside of an open and close curly brace after the if (boolean Expression). Next, add the keyword else and put the statements that you want executed when the boolean expression is false inside of an open and close curly brace.

if (boolean expression) {  statements } else {  statements }


If you have 3 or more options use nested ifs and elses.

if (boolean expression) {    statements } else if (boolean expression) {    statements } else {    statements }


If you have four options, start with an if (boolean expression) and have two else if (boolean expression) and a final else. The last else is optional.



Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net