for Structure
for structure generally used to perform repetition that its number is definitely or previously known. In this type, we have to define the initialization and the conditions for exit from loop. In addition, we also need to add iteration, the variable controller to make increment or decrement process. Here is a common form of writing the syntax for building for structure:
for (initialization; condition; iteration) {
//Statement that will be repeated
}
A loop process will continue as long the condition returns true. The loop process can only be terminated if the condition is false or has not met again. The complete work of loop structure is as the following: when the program entering loop structure, program will perform initialization process. In general, this is an expression that will set the initial value of the loop index, which acts as a counter of the number of repetition to be performed. It should be noted that in initialization process, this will only be done once. Furthermore, the program will check the condition defined. The condition must be an expression that produces a boolean value. If the condition is true, then the statement contained in the loop block will be executed. Conversely, if false, then the loop will be discontinued. Lastly, the program will run iteration part, which is usually increment or decrement process of loop index
Please look at the following simple program:
class DemoFor1 { public static void main(String[] args) { for (int i=0; i<10, i++) { System.out.println("Java"); } } }
When executed, the program will print text "Java" 10 times, from index-0 to 9, as follows:
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
In loop structure above, we use increment for the iteration process. However, we also can use decrement to produce the same output, as follows:
class DemoFor2 { public static void main(String[] args) { for (int i=10; i>0, i--) { System.out.println("Java"); } } }
This time, the program will print text "Java" 10 times, but the index starts from 10 to 1 (downhill).
while Structure
while structure is a kind of loop that defines the condition in the beginning of the block. This means that, if the conditions are not met (false) then loop process will never do.
Value initialization process on the while structure is written before we write while structure itself, the iteration will be written in the block. Here is the general form of while structure in Java.
initialization
while (condition) {
// Statements that will be repeated
...
iteration
}
For example, to display the text "Java" 10 times to the screen, we can write code as follows:
class DemoWhile { public static void main(String[] args) { int i=0; while (i<10) { System.out.println("Java"); i++; } } }
The above program will give the following result:
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
do-while Structure
do-while structure actually similar with while structure. The difference is only in the condition placement. In the while structure, the condition placed on the initial loop block, and in the do-while structure, the condition is at the end of the block. Consequently, for do-while structure, process repeatability will be performed at least once despite the fact the condition is not met (false). Here is a common form of writing do-while structure.
initialization
do {
// Statements that will be repeated
...
iteration
} while (condition);
Please look at the following program:
class DoWhileExample { public static void main(string[] args) { int i=6; do { System.out.println("I am learning Java”); i++; } while (i < 5); } }
This time, although the value of i is greater than 5, which means the condition is false, but the program will continue to execute the statement inside the loop block 1 times. How it works as follows: At first, the program will set the variable i with a value of 6. Next, the program will enter into the loop block and print text "I am learning Java" to the screen as much as 1 times as well as raise the value of i becomes 7. After that, the program will check the conditions defined. Because 7 is greater than 5, then the condition (i < 5) is false, and this will stop repetition process. Here is the result that will be given:
I am learning Java
Thank you so much for the valuable info
ReplyDeleteHere am sharing some information about Best Training Institutes java training in hyderabad |java course in hyderabad | java institutes in hyderabad | software training institutes in hyderabad |selenium training in hyderabad | software training institutes in hyderabad with placements |python training in hyderabad | python course in hyderabad | python institutes in hyderabad
Thank you for sharing valuable information with us.
ReplyDeleteJava web application is a type of free programming language used to develop applications and software that are becoming highly in demand on the internet. The software engineer can provide the web application or software in the form of windows, Linux, and MAC OS. There are various types of systems that can not work without java and it is getting necessary day by day even the world’s no.1 application depends on java. Java has become a most popular feature in android smartphone applications that's why Aptech learning provides java training in Dwarka, Janakpuri, and in Gurgaon also. Java training helps to get you complete knowledge about web frameworks, Java testing tools, and design patterns in java.
Let's get learn about the PHP training course
PHP is a type of programming language that is most commonly used in dynamic functions such as modifying, collecting, and deleting all types of databases It also helps to encrypt all types of data in the form of HTML and XML file. Aptech learning provides you with a complete PHP training course at a reasonable cost that will help you to grow more in the era of the software industry.
For more details please visit our website : Aptech Gurgaon