It is one of the disadvantages of using for loop. An infinite for loop is one that runs without stopping. In example 3, we are going to check an infinite for loop. The simple loop format is also shown in the code below.Ĭode: // Java program to illustrate enhanced for loop In other words, it is also known as enhanced for loop. The sample code, as well as the output, is shown below. Syntax: for (T element:Collection obj/array) The syntax for printing the elements in the array is as follows. ("OUTPUT OF THE FIRST 10 NATURAL NUMBERS") Īfter the first example, we move on to the second example, where we introduce an array and print certain elements in the array. It runs from 1 to 10 generating all the natural numbers in between. There are three phases in the loop statement. The sample code is given below as well as the output. In the first example, we are going to generate the first 10 numbers in a Java program using for loop. Given below are the examples mentioned:: Example #1 Syntax: for (initialization condition testing condition The syntax of a for loop in a Java program can be easily executed using the following. Java is an entry-controlled loop as the condition is checked prior to the execution of the statement. Terminating the Loop – When the condition doesn’t satisfy in the testing condition phase, the loop closes and doesn’t work anymore.There can also be a decrement of 1 to the loop control variable if the condition of the program demands so. Incrementing/Decrementing Condition – In this phase, the loop control variable or the counter variable is incremented by 1 generally to move the code forward. Sometimes the print statement is also used within this phase. Statement Execution – In this phase, the print statement or the variable inside the for loop is executed making it easier to generate the output.Testing Condition – In the test condition, one of the counter variable variables is checked whether it is greater than or less than a certain quantity.Generally, the variables are initialized as zero or one. Initializing Condition – In the Initialization phase, we introduce the variables to be used in the Java program.Web development, programming languages, Software testing & others Start Your Free Software Development Course
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |