Centuries (For)

Hour glasses

The Roman occupation lasted until 410 CE. In that year the Romans moved their occupying armies back to the European mainland. The Britons had become Romanized and had little or no military to defend themselves from what was to come.

The for loop for(int i=1; i<5; i++) has three sections.

  1. initialization
  2. termination
  3. increment

The int i=1;is the common way to initialize the for loop. The i++is used to add 1 after the block of code is executed. Notice that the print lines above and below the for loop block (separated with curly braces) are executed outside the for code.

This code illustrates the use of a For Loop.

  1. import static java.lang.System.out;
  2. public class CenturiesFor {
  3. public static void main(String[] args) {
  4. out.println();
  5. out.println("The Romans ruled Britain in:");
  6. for(int i=1; i<5; i++) {
  7. out.println();
  8. out.println("Century " + i);
  9. }
  10. out.println();
  11. out.println("The Romans moved their armies out of Britain in 410 CE.");
  12. }
  13. }

Java code

Use Save As to save the code to your folder as: CenturiesFor.java. Then compile the code in the Command prompt window. Use javac CenturiesFor.java to create the .class file. Execute the program. Use java CenturiesFor to run the program.

Your output should look like this:

c:\javamike>java CenturiesFor

The Romans ruled Britain in:

Century 1

Century 2

Century 3

Century 4

The Romans moved their armies out of Britain in 410 CE.

 

< Romans (While) Anglo-Saxons (Class) >