Understanding For Loops in Java Programming

java unit 7 loops n.w
1 / 15
Embed
Share

Learn about for loops in Java programming, a powerful tool for repeating tasks based on an incrementing or decrementing control variable called a loop index. Discover how to initialize, set loop conditions, and increment variables within for loops and see examples of their implementation.

  • Java Programming
  • For Loops
  • Loop Index
  • Control Variable
  • Iteration

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Java Unit 7: Loops FOR LOOPS

  2. A for loop is a kind of loop that repeats itself based on an incrementing or decrementing control variable called a loop index . It s the best kind of loop to use when you know in advance how many times the loop should run.

  3. Incrementing Index You ve already seen loops which use a variable in its loop condition to control when it should stop. It increments or decrements every iteration, so it ll eventually reach its stopping point. When used this way, this kind of variable is referred to as a loop index . int i = 0; while(i < x) { i++; }

  4. For loops for (int i = 0; i < limit; i++) { body code } A for loop condition consists of three elements, in one compact line: The initialization (int i = 0;) The looping condition (i < endValue;) The incrementation (i++) Notice that this element doesn t contain a semicolon, while the others do.

  5. For Loop: Initialization for (int i = 0; i < limit; i++) int i = 0; The initialization segment consists of an assignment and (potentially) a declaration of the loop index. It also has a semicolon at the end. It is also possible to use another variable declared before the for-loop. However, you still need to assign a value to the variable in the for-loop. int i; for (i = 0; i < limit; i++) { } Other types such as double may be used too.

  6. For Loop: Loop Condition for (int i = 0; i < limit; i++) i < limit; The looping condition works just like it does in a while loop. As soon as the condition becomes false, the loop will exit. However, this should always be an inequality, where the loop index is either greater than or less than the loop limit. Just like the initialization segment, this one contains a semicolon at the end.

  7. For Loop: Incrementation for (int i = 0; i < limit; i++) i++ This defines how the index is incremented at the end of each iteration of the body code. It could be written in other ways: i = i + 1 i += 2 i -= 1 Count up and down whichever way you want. Notice this one does not have a semicolon

  8. For loop pseudocode 1. Initialize loop index. 2. If looping condition is true, go to line 3. Else, go to line 6. 3. Execute body code. 4. Increment the index (as defined in the for loop) 5. Go to line 2. 6. Exit the for loop.

  9. For Loop writing and uses Quite often in programming it is more useful to start the loop index at 0 and have the stopping condition be a strict inequality. for (int i = 0; i < x; i++) { } This is especially useful in situations such as going through a string of characters. If we begin the loop index at 0, and write a looping condition where the index has to be less than the length of the string, we can iterate through all the characters in the string without going over.

  10. For Loop writing and uses int i = 0; String myString = Something ; for(i = 0; i < myString.length(); i++) { System.out.println(myString.substring(i, i + 1)); } Note the loop condition doesn t use less than or equal . If i did equal myString.length() then, the body code would refer to a character index which doesn t exist. S 0 O 1 M 2 E 3 T 4 H 5 I 6 N 7 G 8

  11. For Loops and Scope Recall that we have the option of declaring a new variable in our for-loops. for(int i = 0; int i = 0; for(i = 0; When a for loop declares a new variable for its loop index, that variable applies only to that loop. We would say that the loop index is within the scope of the for loop. This could lead us to a few errors if we don t understand the scope of variables.

  12. Error Case: Redeclaration int i = 0; for (int i = 0; i < x; i++) { // ERROR! } The for-loop initializes a variable of its own. But before the loop, a variable of that same name has already been created. This is a redeclaration error: declaring the same variable twice.

  13. Error Case: Undeclared Variable for(int i = 0; i < x; i++) { System.out.println(i); } System.out.println(i); Even though i has already been declared, it was declared for the scope of the for loop. As soon as the for-loop ends, the variable fizzles out of existence and for the rest of the code it s like it was never there. //ERROR!

  14. Fixing Errors for(int i = 0; i < x; i++) { System.out.println(i); } int i = 0; System.out.println(i); This code will compile and work correctly. A redeclaration error would not occur because the system forgot about the i variable after the for loop ended.

  15. Fixing Errors int i = 0; for(i = 0; i < x; i++) { System.out.println(i); } System.out.println(i); This is also a good solution. But in this case, the variable i is declared outside the scope of the for loop, to it is available to everything inside and outside the loop.

Related


More Related Content