The flow of the execution of the program is controlled by the control flow statement. Statements that determine which statement to execute and when are known as decision-making statements. Moving on with this article on Control Statements in Java An ‘if’ statement in java determines the sequence of execution between a set of two statements.Ĭontrol Statements can be divided into three categories, namely A control statement in java is a statement that determines whether the other statements will be executed or not. Following pointers will be covered in this article:Įvery programmer is familiar with the term statement, which can simply be defined as an instruction given to the computer to perform specific operations. Statements in Java is one of the fundamentals required for Java Programming. To create an infinite do while loop just pass the condition that always remains true. Like infinite while loop, we can create infinite do while loop as well. In this example, we are printing values from 1 to 10 by using the do while loop. The main difference between while and do-while loop is, in do while loop condition evaluates after executing the loop.įollowing is the syntax to declare do-while loop in Java. It means loop condition evaluates after executing of loop body. This loop executes at least once because the loop is executed before the condition is checked. In Java, the do-while loop is used to execute statements again and again. We can also create infinite loop by passing true literal in the loop.īe careful, when creating infinite loop because it can issue memory overflow problem. In first step, we set conditional variable then test the condition and if condition is true execute the loop body and increment the variable by 1.Ī while loop which conditional expression always returns true is called infinite while loop. In this example, we are using while loop to print 1 to 10 values. When the number of iteration is not fixed then while loop is used. It is used for iterating a part of the program several times. Like for loop, while loop is also used to execute code repeatedly. For-each loop terminates automatically when no element is left in the array object. In this example, we are traversing array elements using the for-each loop. In this loop, there is no need for increment or decrement operator.įollowing is the syntax to declare for-each loop in the Java. In Java, for each loop is used for traversing array or collection elements. Generally, nested loops are used to iterate tabular data. Sometimes based on the requirement, we have to create nested loop. Loop can be nested, loop created inside another loop is called nested loop. In this example, initial value of loop is set to 1 and incrementing it by 1 till the condition is true and executes 10 times. This flow diagram shows flow of the loop. It is used for set increment or decrement value for the loop. It is loop body and executed every time until the condition is false. It is optional and if we don’t specify, loop will be inifinite. The execution continues until the condition is false. It is used to test a condition each time while executing. It is optional, if we don’t want to set initial value. It is executed only once at the starting of loop. It is the initial part, where we set initial value for the loop. To create a for loop, we need to set the following parameters. For loop can be categories into two type.įollowing is the syntax for declaring for loop in Java.įor(initialization condition increment/decrement) When the number of execution is fixed then it is suggested to use for loop. The for loop is used for executing a part of the program repeatedly. We will explain each loop individually in details in the below. Java provides mainly three loop based on the loop structure. Loop is designed to execute its block till the specified condition is true. However, we can do the same with a single print statement by using loop. For example if we want to print table of a number then we need to write print statement 10 times. The most common use of loop is to perform repetitive tasks. Loop is designed to execute particular code block till the specified condition is true or all the elements of a collection(array, list etc) are completely traversed. Loop is an important concept of a programming that allows to iterate over the sequence of statements.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |