Skip to content

Latest commit

 

History

History
86 lines (73 loc) · 5.69 KB

07-For-Loops.md

File metadata and controls

86 lines (73 loc) · 5.69 KB

7. For Loops

1. Lesson Summary

In this lesson, we continue with the Java Basics Tutorial. That is the seventh part of this series, and if you want to see the complete list of lessons, you can do it here.

In programming, a loop is used to repeat a block of code until a specified condition is met. The For Loop is best when you want to do something for a fixed number of times.
How does For Loop work?

  1. Control falls into the For Loop. Initialization is done.
  2. The flow jumps to the Condition.
  3. The Condition is tested.
    1. If it is true, the flow goes into the body.
    2. If it is false, the flow goes outside the loop.
  4. The statements inside the body of the loop get executed.
  5. The update takes place, and the flow goes to Step 3 again.
  6. The For Loop has ended, and the flow has gone outside.
This process goes on until the test expression is false. When the test expression is false, the loop terminates.

Say we want to loop over a range of numbers and print out each one along the way. We can do this best with a For Loop. We will start from the first number, print it out, move to the next number to do the same thing, and continue until we’ve printed each number. Let’s print the numbers zero through nine:

Let’s look at the first line of the For Loop and the three parts necessary to make it work. First, we have an initialization expression, then termination expression, and increment expression in the end.

The initialization expression initializes the loop and is only executed once when the loop starts. That is where you must declare and initialize a variable, usually of type int, to hold an initial number that will be used to loop until it reaches a specific value. That can be thought of as the start of the range to iterate over. In our case, we started at 0.

The For Loop will continue looping until the termination expression evaluates to false. The termination expression is evaluated before each iteration of the loop, and it must return a boolean to decide whether or not to continue looping. If the boolean returned is equal to true, we will run the body of our For Loop again. In our case, the loop terminates after it prints 9.

The increment expression is executed after each iteration of the loop. To increment the variable, we initialize it by using the ++ operator. That allows the termination expression to know how many times the loop loops. The initialization variable count, in our example, starts at 0, and it’s printed out. Then the incrementer increments it, and the next iteration of the loop runs to print the new value and continues that way until it prints 9.

In conclusion, you should use a For Loop when you know how many times the loop should run. There are other more advanced iteration structures that we will look at in the next lesson.

2. Table of Contents

3. Lesson Video

YouTube Thumbnail

4. Lesson Topics

In this lesson we cover the following topics:

  • Increment adn Decrement
  • For Loops
  • Loops with a Step
  • Iterating over Characters
  • Infinite Loops
  • Coding Exercises

5. Resources

Remember that coding is a skill, which should be practiced. To learn to code, you should write code every day for a long time. Watching tutorials is not enough. You should code!

Resources Link
Lesson Video YouTube
Lesson Content SoftUni

6. Practical Exercises

You will get access to automated exercises which will sharpen your coding skills. Become a member of the SoftUni Global Community and communicate with other students and mentors and get help for FREE. Please watch the video and solve the exercise problems. Writing code is the only way to master the skill of coding. Submit your code at the SoftUni Judge.

Resources Link
Problem Descriptions Access the Learning Materials
Submit Solutions for Evaluation Access the Learning Materials

7. Navigation

« Previous Lesson   |   Home   |   Next Lesson »