1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 3. Operators and Iteration


Lesson Objective

Understand Iteration and how to use the following types of loops in Java programming.

  • While Loops
  • Do While Loops
  • For Loops

Lesson Notes

Arithmetic Operators

These are used to perform common mathematical operations.

Operator Description Example
+ Addition Adds together two values
- Subtraction Subtracts one value from another
* Multiplication Multiplies two values
/ Division Divides one value by another
% Modulus Returns the remainder of a division
++ Increment Increases the value of a variable by 1
-- Decrement Decreases the value of a variable by 1

Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
= Assigns a value to a variable n = 5
+= Add to a variable n += 6
-= Take away from a variable n -= 2
*= Multiply a variable n *= 2
/= Division n /= 4
%= MOD n %= 3
&= Bitwise AND Operation n &= 3
|= Bitwise OR Operation n |= 3
^= Bitwise XOR Operation n ^= 3
>>= Right shift AND n >>= 3
<<= Left shift AND n <<= 3

Iteration

Iteration is the process of repeating code until a condition is met.

Definite Iteration: When code it repeated for a set number of times.

Indefinite Iteration: When code is repeated for an unknown number of times.

While Loops

While loops are examples of indefinite iteration. They repeat lines on code for an unknown number of times (until a condition is met).

The while loop loops through a block of code as long as a specified condition is true.

Code:

public class Hello_World_1 {
   public static void main(String args[]){
      int num = 0;
      System.out.println("Some numbers will appear.");
      while (num <= 10){
         System.out.println(num);
         num++;
      }
      System.out.println("Some numbers HAVE appeared.");
    }
}
//++ can be used to increment numbers quickly.

Cmd Output:

Some numbers will display now.
0
1
2
3
4
5
6
7
8
9
10
Some numbers HAVE appeared.

The while loop has been adapted to show an example of a while loop iterating over a user input.

Code:

import java.util.Scanner;
public class Hello_World_1 {
   public static void main(String args[]){
      Scanner scan_ob = new Scanner(System.in);     
      String name;
      System.out.println("Enter your first name:");
      name = scan_ob.nextLine();
      while (!name.equals("Catman")){
         System.out.println(name);
         System.out.println("You are not allowed in!");
         System.out.println("Enter your first name:");
         name = scan_ob.nextLine();
      }
      System.out.println("Welcome Catman...");
   }
}
// !.equals() is used to identify if a String is NOT EQUAL to another string.

Cmd Output:

Enter your first name:
Batman
You are not allowed in!
Enter your first name:
ManBat
You are not allowed in!
Enter your first name:
Mr Reynolds
You are not allowed in!
Enter your first name:
Catman
Welcome Catman...

Do While Loops

The do while loop is a modified version of the while loop. It executes the code block once before evaluating the condition.

Code:

public class Hello_World_1 {
   public static void main(String args[]){
      int num = 0;
      System.out.println("Some numbers will appear.");
      do {
         System.out.println(num);
         num++;
      }
      while (num <= 10);
      System.out.println("Some numbers HAVE appeared.");
   }
}
// The condition is checked at the end of the loop, after the code has run once.

Cmd Output:

Some numbers will display now.
0
1
2
3
4
5
6
7
8
9
10
Some numbers HAVE appeared.

FOR Loops

FOR loops demonstrate definite iteration. They repeat a fixed number of times and can be executed in various ways.

These loops are suitable when you have precise knowledge of how many iterations are required for a given code block.

Skeleton Code:

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block. If the condition is true, the loop will start over again, if it is false, the loop will end.

Statement 3 is executed (every time) after the code block has been executed.

For loops are used when you know exactly how many times you want to loop through a block of code.

Code:

public class Hello_World_1 {
   public static void main(String args[]){
      System.out.println("Some numbers will appear.");
      for (int i = 0; i <= 10; i++) {
         System.out.println(i);
      }
      System.out.println("Some numbers HAVE appeared.");
   }
}
// Statement 1: int i = 0 (sets variable before start)
// Statement 2: i <= 10 (defines the condition for the loop to run (i must be <= 10). 
// Statement 3: i++ (increases the value at the end of each loop)

Cmd Output:

Some numbers will display now.
0
1
2
3
4
5
6
7
8
9
10
Some numbers HAVE appeared.

Nested Loop

A nested loop is a loop inside a loop. The “inner loop” executes once for each iteration of the “outer loop.”


3