Conditional Expression

Contact Us or call 1-877-932-8228
Conditional Expression

Conditional Expression

Java uses the same conditional expression as C and C++

condition ? expressionIfTrue : expressionIfFalse

This performs a conditional test in an expression, resulting in the first value if the condition is true,or the second if the condition is false

Note: due to operator precedence issues, it is often best to enclose the entire expression in parentheses


Code Sample:

public class Conditional {
  public static void main(String[] args) {
    int i = -6;
    String s;
    s = "i is " + ((i >= 0) ? "positive" : "negative");

Note that the parentheses around the test are not necessary by the operator precedence rules, but may help make the code clearer

The parentheses around the entire conditional expression are necessary; without them, precedence rules would concatenate the boolean result onto the initial string, and then the ? operator would be flagged as an error, since the value to its left would not be a boolean.

while and do . . . while Loops

Again, the loops in Java are pretty much the same as in C - with the exception that the conditional expressions must evaluate to only boolean values

while loops

while (condition) statement


while (condition){
	block of statements
  • While Loop Flowchart the loop continues as long as the expression evaluates to true
  • the condition is evaluated before the start of each pass
  • it is possible that the body of the loop never executes at all (if the condition is false the first time)

do ... while loops

do statemen; while (condition);


do { block of statements } while (condition;
  • Do ... While Loop Flowchart the condition is evaluated after the end of each pass
  • the body of the loop will always execute at least once, even if the condition is false the first time

for Loops

A for loop uses a counter to progress through a series of values

  • a value is initialized, tested each time through, and then modified (usually incremented) at the end of each pass, before it is tested again
  • the for loop does not do anything that cannot be done with a while loop, but it puts everything that controls the looping at the top of the block

For Loop Flowchart

for (initialize; condition; change)


for (initialize; condition<; change) {
	block of statements

for loops can use a variable declared out side of the control portion of the loop or in the control portion. The latter gives the variable block-level scope (existing only for the duration of the loop)

int j;
for (j = 0; j < 12; j++ )
	System.out.print("j = " + j);
for (int j = 0; j < 12; j++ )
	System.out.print("j = " + j);

A for loop may use multiple control variables by using the sequence operator, the comma ( , )

for (int j = 0, k = 12; j <= 12; j++, k-- )
	System.out.println(j + " " + k);

Note: if you use a block-scope variable (such as above) for the first counter, the additional ones will be block scope as well, and also will be the same type of data - i.e., the variable k above also exists only for the duration of the block. There is no way to declare two counters of different types at block-level scope.

Note that neither while nor do . . . while loops allow declaring a looping variable with this type of scope. The looping variable must be declared in advance.

ForEach Loops

Java 5 introduced a new type of loop, the for-each loop. When you have an array or collection class instance, you can loop through it using a simplified syntax

for (type variable : arrayOrCollection) {
	body of loop

The looping variable is not a counter - it will contain each element of the array or collection in turn (the actual value and not an index to it, so its type should be the same as the type of items in the array or collection). You can read the : character as if it is the word "from". We will cover this type of loop in more depth in the Arrays section.

For some reason, the looping variable must be declared within the parentheses controlling the loop - you cannot use a preexisting variable.

Since the looping variable is a local variable, it gets a copy of each value from the array or collection. Therefore you cannot use the ForEach loop to write values back to the array/collection - assigning a new value to the variable in the body of the loop is only overwriting the local copy.

Code Sample:

public class Loops1 {
  public static void main(String[] args) {
    int i = 1;
    System.out.println("While loop:");
    while (i <= 512) {
      System.out.println("i is " + i);
      i *= 2;
    System.out.println("i is now " + i);
    System.out.println("Do while loop:");
    do {
      i = i - 300;
      System.out.println("i is now " + i);
    while (i > 0);
    System.out.println("For loop:");
    for (i = 0; i < 12; i++) 
      System.out.print(" " + i);
    System.out.println("For loop that declares a counter:");
    for (int j = 0; j < 12; j++) 
      System.out.print(" " + j);
    System.out.println("ForEach loop:");
    String[] names = { "Jane", "John", "Bill" };
    for (String oneName : names)