For Loop Java, A statement is a single command that is executed by the java interpreter. By default, the java interpreter runs one statement after the other. Java offers many statements including simple, compound, program flow control statements. Today we are going to talk about some of these statements in java and the various control structures of java.
Programs not only store data but also manipulate the data in terms of consolidation, rearranging, modifying data. To perform their manipulate miracles, programs need tools for performing repetitive actions and for making decisions. Java, of course, provides such tools by providing the statements to attain so. Such statements are called control statements that control the flow of aq control of a program. These also enhance the performance of the program and helps the user to use their programs easily.
Program control statements are of 3 types:
The selection statements allow choosing the set of instructions for execution depending upon an expression’s truth value. Java provides 2 types of selection statements and they were if statement and switch statement. In addition, in certain circumstances, the operator can be used as an alternative to if statement. The selection statements are also called conditional statements or decision statements.
THE IF STATEMENT OF JAVA:
An if statement tests a particular condition. If the condition evaluates to truth a course of action is followed. The statement or set of statements is executed. Else the course of action is ignored.
THE IF-ELSE STATEMENT:
We can have an if-else part in if statement. The else part gets executed if the condition or if statement evaluates to false.
A nested if is an if that has another if inside another if body or else’s body.
There is another operator known as the conditional operator which is used as a substitution to if statements sometimes. Now after knowing all the if statements that are basically a part of the selection statement let us take a sneak peek into the switch statement.
Java provides multiple branch statements which are another part of a selection statement called the switch statement. The selection statement successively tests the worth of an expression against a listing of the integers or character constants. once a match is found, the statements related to the constants are executed. The expression is evaluated and its values are matched against the values of the constants per the case statements. When a match is found, the statement sequence is executed associated with that case until there is an occurrence of the break statement that breaks through all the cases and then goes to the end of the switch statement.
The iteration statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also called the loops or the looping statements. Java provides 3 kinds of looping statements and they are for loop, while loop and do-while loop. Every loop has its elements that govern its execution. Generally, a for loop has 4 elements that have different purposes. The 4 parts are: initialization, test expression, update expression and the body of the loop. The initialization includes initializing a value to the loop statement followed by the test expression whose truth value decides whether the loop body shall be executed or not. Then comes the update expression which shows the updating that needs to be done with every time the loop is performed and finally the body of the loop enclosed in braces which shows us the statements to be executed using these.
The looping statements are divided into 2 types:
ENTRY CONTROLLED LOOP
EXIT CONTROLLED LOOP
In the entry-controlled loop, the condition of the performance of the loop is checked at the very beginning of the loop statement. For loop and while loop is examples of the entry-controlled loop. Here f the condition is false then the controller does not even enter the loop statement.
In the exit-controlled loop, the condition is checked at the end of the loop which means that even though the condition is false yet the statements shall be executed at least once. Do while loop is an example of the exit-controlled loop.
Java provides several variations that increase the flexibility and applicability of for loop. Some of them are:
MULTIPLE INITIALIZATION AND UPDATE EXPRESSION
In a while loop, a loop control variable should be initialized before the loop begins as an uninitialized variable can be used in an expression. The loop variable should be updated inside the body of the do-while.
Unlike the 2 other loops the do-while loop the test expression is executed before entering the loop body. The most common use of a do-while loop is in the menu selection routine.
Thus, this was all that we needed to know about the types of statement java uses and mostly the loop statements which helps us to increase the time and space complexity and helps us to perform our work faster.
(image source google search)