<< Chapter < Page Chapter >> Page >

The expressions in the first clause are executed only once, at the beginning of the loop. Any legal expression(s)may be contained in the first clause, but typically the first clause is used for initialization.

Declaring and initializing variables in the first clause

Variables can be declared and initialized in the first clause, and this has an interesting ramification regarding scope thatwill be discussed later.

Behavior of the second clause

The second clause consists of a single expression that must evaluate to a boolean type with a value of true or false. The expression in the second clause must eventually evaluate to false to cause the loop toterminate.

Typically relational expressions or relational and conditional expressions are used in the second clause.

When the test is performed

The value of the second clause is tested when the statement first begins execution, and at the beginning of each iteration thereafter.Therefore, just like the while loop, the for loop is an entry condition loop .

When the third clause is executed

Although the third clause appears physically at the top of the loop, it isn't executed until the statements in the body of the loop have completedexecution.

This is an important point since this clause is typically used to update the control variable, and perhaps other variables as well.

What the third clause can contain

Multiple expressions can appear in the third clause, separated by the comma operator. Again, those expressions will be executed fromleft to right. If variables are updated in the third clause and used in the body of the loop, it is important to understand that they do not get updated untilthe execution of the body is completed.

Declaring a variable in a for loop

As mentioned earlier, it is allowable to declare variables in the first clause of a for loop.

You can declare a variable with a given name outside (prior to) the for loop, or you can declare it inside the for loop, but not both.

If you declare it outside the for loop, you can access it either outside or inside the loop.

If you declare it inside the loop, you can access it only inside the loop. In other words, the scope of variables declared inside a for loop is limited to the loop.

This is illustrated in following sequence of four simple programs.

This program won't compile

The Java program shown in Listing 2 refuses to compile with a complaint that a variable named cnt has already been declared in the method when the attempt is made to declare it in the for loop.

Listing 2 . A program that won't compile.
/*File for1.java Copyright 1997, R.G.Baldwin This program will not compile because the variablenamed cnt is declared twice. ********************************************************/class for1 { //define the controlling class public static void main(String[]args){ //main method int cnt = 5; //declare local method variableSystem.out.println( "Value of method var named cnt is " + cnt);for(int cnt = 0; cnt<2; cnt++) System.out.println("Value of loop var named cnt is " + cnt); System.out.println("Value of method var named cnt is " + cnt); }//end main}//End controlling class. Note no semicolon required

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask