https://vimeo.com/307050298 Imagine you have a block of code you need to repeat multiple times. You can wrap it in a function and call that function as many times as you need to. That could do the trick, however, most of the time you don't even know in advance how many times you need to call it. Show
Loops solve this issue! A loop, in programming, is a technique that allows you to repeat one or more instructions without having to retype the same set of instructions multiple times. Use enumerated loops for known number of iterationsEnumerated loops are loops that are used when you know in advance how many times you want to loop. In Java, these are called for loops. With these, you can provide the number of iterations to be performed:
For loops with an integer valueHere is an example of a for loop that repeats a statement five times:
In this set of instructions, we have an enumerating
variable
Can you imagine if we wanted to clap not five, but 42, or 1001 times? The general syntax for the for statement is as follows:
For loops with collectionsUse enumerated loops when you need to iterate over an array or a collection. Here is an example for arrays:
As we saw in part 1, an array has a
With the enhanced for, you simply need to define a variable of the type of the array or collection you are looping over. This variable will be assigned the value of each element of the array or collection until you have reached the end. The for loop is great when you want to loop a predefined number of times or over all the elements of an array. When it is not the case, Java provides a more general way of looping with conditional loops. Loop until you reach a condition with while loopsConditional loops are also called while loops. They come in two types:
You can guess by the name, the loop needs to keep going while … something… or "as long as." It is a little like a combination of a for loop and an if statement. The number of repetitions is not defined by the lower and upper limits of an enumerator, but by a condition like that of an if statement. While loopHere's what the syntax looks like:
It can be interpreted as "as long as the logical expression is true, repeat the instructions." This is how it works in detail:
Let's look at an example to try:
This will produce the following result:
At each loop turn, ❗️The following is important to keep in mind: using while loops can cause a program to crash. ☠️ When the expression remains
It's a common and easy mistake to make. So, pay attention! As you can see, the while condition is checked BEFORE the corresponding block of code can be executed even one time! Do-while loopDo-while loop is very similar to the first one, however, the condition is placed at the end of the corresponding block of code. This way, the block of code will always be executed at least once. Here's how the syntax looks:
Let's look at an example:
This way, at least one push-up is performed before the condition is even checked. Let's tweak it a bit to demonstrate the difference from the original while loop: Let's look at an example:
You can see that using the original while loop will perform no push-ups. The the do/while loop will cause a push-up to be performed once. Skipping some instructions inside a loopWithin each type of loop, there might be situations when you want to skip some iterations or interrupt the whole loop prematurely upon a certain condition. For example, you may want to repeat something 10 times, but skip (or partially skip) when the value equals
You may also interrupt the sequence completely, for example, if you want to find an element in an array and stop looking once found: In Java, to interrupt an execution sequence, use a
Once you find an item you are looking for, stop browsing the rest of an array. Try it out for yourself!https://api.codevolve.com/api/v1/publishable_key/2A9CAA3419124E3E8C3F5AFCE5306292?content_id=db6d1696-27b8-4a02-a020-e8d544f2e7a8 SummaryIn this chapter, you've learned two types of loops:
What is the process of repeating a set of operations until a specific result is achieved?Iteration is the process of repeating a set of operations until a specific result is achieved. In terms of design, this principle allows complex solutions to be created by continually exploring, testing, and redesigning.
What type of loop uses a Boolean expression to control the number of times that it repeats a statement or set of statements?A condition-controlled or sentinel loop uses a true/false condition to control the number of times that it repeats.
What value can be used to terminate the repetition?c) Repeating a set of instructions a specific number of times is called repetition. ANS: counter-controlled (or definite). d) When it is not known in advance how many times a set of statements will be repeated, a(n) value can be used to terminate the repetition.
Which C++ structureLoops. A Loop is a control structure that causes a statement or group of statements to be executed repeatedly. Frequently in a program you will want to repeat an action several times or until some condition is met.
|