Lesson 4: Conditionals and Loops


The term “flow of control” is used to refer to a sequence of statements that are executed in a program.

The small programs we have used have a simple flow of control: the statements are executed one after another.

As we get more complicated, statements may or may not be executed depending on certain conditions.

Conditionals

 

Or groups of statements are executed multiple times:

Loops

If Statements

Most computations required different actions for different inputs.

if ( boolean expression ) { statements }

 

 

The "if" statement

Typical Examples:

Absolute Value

if (x < 0) x = -x;

Income tax rate

if (income < 47500)
taxRate = 0.22;
else if (income < 114650)
taxRate = 0.25;
else if (income < 174700)
taxRate = 0.28;
else if (income < 311950)
taxRate = 0.33
else
taxRate = 0.35;

Error checking

if (d ==0)
System.put.println(“Error: division by 0”);
else
System.out.println(“Quotient = “ + n/d);

Error check for quadratic equation

double discriminant = b*b – 4.0*c ;
if (discriminant < 0.0)
{
System.out.println(“No real roots”);
} else {
double d = Math.sqrt(discriminant);
System.out.println((-b + d)/2.0);
System.out.println((-b – d)/2.0);
}

Loops

Loops allow us to perform a group of statements many times. This enables us to express lengthy or complicated solutions without writing lots of code.

  1. for Loops
  2. while Loops
  3. do…While Loops

 

For Loops

The for loop is a repetition control structure that allows us to execute a group of statements a specific number of times. Useful when we know how many times a task is to be repeated.

3 Characteristics of for loops:

  1. Initialize an index variable
  2. Test condition to exit the loop
  3. Modify the index variable

 for loop

While Loops

The while loop and the do-while loop are repetition control structures that allow us to execute a group of statements as long as a certain condition is met.

while loop

do while loop

 

Other Conditionals and Loop Constructs

Other Java constructs related to conditional and loops are used less frequently:

  • break - exits a loop without letting it run to completion
for (int i = 2; i < n/i; i++)
{
if (n % i == 0) break;
}
if (i > n/i) System.out.println(n + “ is a prime number.”);

 

  • continue - skip to the next iteration in a loop
for (int i = 0; i < 100; i++)
{
if (i%2 == 0) continue;
System.out.println(i + “ is an odd number.”);
}

 

  • switch statements - The Java switch statement provides a sequence or chain of if-else statements
switch (day)
{
case 0:
System.out.println(“Sunday”);
break;
case 1:
System.out.println(“Monday”);
break;
case 2:
System.out.println(“Tuesday”);
break;
case 3:
System.out.println(“Wednesday”);
break;
case 4:
System.out.println(“Thursday”);
break;
case 5:
System.out.println(“Friday”);
break;
case 6:
System.out.println(“Saturday”);
break;
}

 

  • Labeled break and continue - break and continue statements for jumping out of nested loops.
 test:
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 100; j++)
{
System.out.println(“i = “ + i);
System.out.println(“j = “ + j);
if (j == 50) break test;
}
}

test:
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 100; j++)
{
System.out.println(“i = “ + i);
System.out.println(“j = “ + j);
if (j == 50) continue test;
}
}

 

  • Conditional operator, ? - This is a ternary operator (three operands) that enable you to embed a condition within a single expression. The three operands are separated by ? and : symbols.

Ternary