In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in java.
Java For Loop vs While Loop vs Do While Loop
The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
There are three types of for loops in java.
Java Simple For Loop
A simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value. It consists of four parts:
Syntax:
Flowchart:
For Each Loop Java Example
Example:
Output:
Java Nested For Loop
If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes.
Example:
Output:
Pyramid Example 1:
Output:
Pyramid Example 2:
Output:
Java for-each Loop
The for-each loop is used to traverse array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.
It works on elements basis not index. It returns element one by one in the defined variable.
Syntax:
Example:
Output:
Java Labeled For LoopFor Loop Statement Java
We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful if we have nested for loop so that we can break/continue specific for loop.
Usually, break and continue keywords breaks/continues the innermost for loop only.
Syntax:
Example:
Output:
If you use break bb;, it will break inner loop only which is the default behavior of any loop.
Output:
Java Infinitive For Loop
If you use two semicolons ;; in the for loop, it will be infinitive for loop.
Syntax:
Example:
Output:
Now, you need to press ctrl+c to exit from the program.
Next TopicJava While Loop
There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages −
Java programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Java supports the following control statements. Click the following links to check their detail.
Enhanced for loop in Java
As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements including arrays.
Syntax
Following is the syntax of enhanced for loop −
For Loop If Statement Examples JavaExampleLive Demo
This will produce the following result −
OutputWhat is Next?
In the following chapter, we will be learning about decision making statements in Java programming.
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.
A for loop is useful when you know how many times a task is to be repeated.
Syntax
The syntax of a for loop is −
Taste safe mud recipe. Fifteen tablespoons sounds like a lot (hey, actually it is a lot!) but I found that with less it just had the consistency of condensed milk which is not that fun to play with, you want to make slime not just play with your food.Add a tablespoon at a time and mix until the cornflour dissolves into the mixture. You want your slime to be warm enough that the cornflour can melt but not so hot that it starts to bubble or boil. I used an electric hand whisk to mix this so that it all mixes in properly, as your condensed milk turns into slime it becomes a lot harder to mix as well so this saves your arms getting worn out.Take your taste safe slime off the heat and add one to two teaspoons of food colouring.
Here is the flow of control in a for loop −
Flow DiagramExample
Following is an example code of the for loop in Java.
Live Demo
This will produce the following result −
- If you want to see more, SUBSCRIBE here - Follow me on Twitter - Play my FNAF fan game, FNIA here - Play my second FNAF fan game, FNIA 2, here -SMASH that LIKE BUTTON, it's hugely appreciated!About: So? Which soul are you? Today is the day I find out which Undertale Soul I am!Find out yourself here -If it doesn't work, try this one -Would you survive Yandere Simulator? Undertale what color is your soul.
OutputComments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |