The for statement in Java:
Another important statement to implement repetitive processing in Java is the for statement. The general form of this statement is:
for (control_initializer; logical_expression;
control_adjustment)
{
statements;
}
The statements are executed repeatedly under the control of a variable, referred to as the control variable. The semantics of the for statement are as follows.
The control variable is created and given its initial value in control_initializer. Next, the logical_expression determines if the loop body is executed. After each time the statements are executed, control_adjustment defines the new value of the control variable, and the logical_expression is checked again. The loop terminates when the logical_expression becomes false.
The control_initializer is only executed once and is the first statement to be executed by the for loop.
Comparing the general form of the for statement with that of the while statement, it is clear that they contain the same elements but that these are arranged differently.
An example of the use of the for statement, which could be used to initialize or change elements of an array, is shown below:
int []a = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < a.length; i++)
{
a [i]= 10;
}
Here the statement a [i]= 10; is repeatedly executed. To begin with, the value of i is zero (int i=0 corresponds to control_initializer). Then the test i< a.length is performed. As this is true, the loop body a [i] = 10 is executed, in this case resulting in a [0] being assigned the value 10. Next, i is incremented by one (i++ corresponds to control_adjustment).
Then logical_expression is evaluated again. Because it is true, we repeat the execution of a [i] = 10, in this case resulting in a [1] being assigned the value 10, and increment i again. This process is repeated until i equals the length of the array, when the condition 10 < a.length is false, meaning that the statement a [i]= 10 is not executed, and the loop ends. (Remember that the last element of this array will be a [9], so each array location has been initialized to 10 after this loop.)
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The for statement in Java homework and assignments? Live The for statement in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The for statement in Java homework help, java assignment help and The for statement in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours