Flow control

From Wikibooks, open books for an open world
Jump to: navigation, search

Random numbers Java Programming
Flow control
Conditional statements
Navigate Language Fundamentals topic: v  d  e )


Control flow, common to all popular programming languages, is the method used to control the order in which code executes. Because solutions are rarely simple enough to just start at the top and finish at the bottom, there are always going to be times when you either want something to be done which relies on something else (conditional) or to repeat certain tasks (loops).

[edit] Boolean logic

Before you can understand how conditional control works, you must understand how boolean logic works. Boolean values are values that evaluate to either true or false, and are represented by the boolean data type. Boolean expressions are very similar to mathematical expressions, but instead of using mathematical operators such as "+" or "-", you use comparative or boolean operators such as "==" or "!".

[edit] Comparative operators

Java has several operators that can be used to compare variables. For example, how would you tell if one variable has a greater value than another? The answer: use the "greater-than" operator.

Here is a list of the comparative operators in Java:

  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to
  • == : Equal to
  • != : Not equal to

To see how these operators are used, look at this example:

Computer code
int a = 5, b = 3;
boolean value;
value = a > b; // Value is true because a is greater than b
value = a == b; // Value is false because a does not equal b
value = b <= a; // Value is true because b is less than a
System.out.println(value); // Prints out "true" because the last statement evaluated to true

Comparative operators can be used on any primitive types (except boolean), but only the "equals" and "does not equal" operators work on objects. This is because the less-than/greater-than operators cannot be applied to objects, but the equivalency operators can.

Note Specifically, the == and != operators test whether both variables point to the same object. Objects will be covered later in the tutorial, in the "Classes, Objects, and Types" module.

[edit] Boolean operators

Boolean operators operate directly on boolean values.

Here is a list of three common boolean operators in Java:

  • ! : Boolean NOT
  • && : Boolean AND
  • || : Boolean inclusive OR

The boolean NOT operator ("!") inverts the value of a boolean expression. The boolean AND operator ("&&") will result in true if and only if the values on both sides of the operator are true. The boolean inclusive OR operator ("||") will result in true if either or both of the values on the sides of the operator is true.

To show how these operators are used, here is an example:

Computer code
boolean a = true, b = false, value;
value = !a; // Value is false because a is true
value = a || b; // Value is true because a is true, even though b is false
value = a && b; // Value is false because b is false, even though a is true
value = a && 5 > 3; // Value is true because a is true and "5 > 3" evaluates to true
System.out.println(value); // Prints out "true" because the last statement evaluated to true
Note The && and || operators should not be confused with & and |. & and | are both bitwise operators, which are out of the scope of this module.

Here are the truth tables for the boolean operators:

  • !a
a Result
true false
false true
  • a || b
a b result
true true true
true false true
false true true
false false false
  • a && b
a b result
true true true
true false false
false true false
false false false
Note In Java, boolean logic has a useful property called "short circuiting". This means that expressions will only be evaluated as far as necessary. In the expression (a && b), if a is false, then b will not be evaluated because the expression will be false no matter what. For help on simplifying complex logic, see De Morgan's laws.


Random numbers Java Programming
Flow control
Conditional statements
Personal tools
Namespaces

Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export