<< Chapter < Page Chapter >> Page >

The program shown in Listing 3 also won't compile, but for a different reason.

Listing 3 . Another program that won't compile.
/*File for2.java Copyright 1997, R.G.Baldwin This program will not compile because the variabledeclared inside the for loop is not accessible outside the loop.********************************************************/ class for2 { //define the controlling classpublic static void main(String[] args){ //main methodfor(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

The declaration of the variable named cnt , outside the for loop, was removed from Listing 3 and the declaration inside the loop was allowed to remain. This eliminated the problem of attempting to declarethe variable twice.

However, this program refused to compile because an attempt was made to access the variable named cnt outside the for loop. This was not allowed because the variable was declared inside the for loop and the scope of the variable was limited to the loop.

This program will compile

The Java program shown in Listing 4 will compile and run because the variable named cnt that is declared inside the for loop is accessed only inside the for loop. No reference to a variable with the same name appears outside the loop.

Listing 4 . A program that will compile.
/*File for3.java Copyright 1997, R.G.Baldwin This program will compile because the variable declaredinside the for loop is accessed only inside the loop. **********************************************************/class for3 { //define the controlling class public static void main(String[]args){ //main methodfor(int cnt = 0; cnt<2; cnt++) System.out.println("Value of loop var named cnt is " + cnt); }//end main}//End controlling class.

This program will also compile

Similarly, the program shown in Listing 5 will compile and run because the variable named cnt was declared outside the for loop and was not declared inside the for loop. This made it possible to access that variable both inside and outside the loop.

Listing 5 . Another program that will compile.
/*File for4.java Copyright 1997, R.G.Baldwin This program will compile and run because the variablenamed cnt is declared outside the for loop and is not declared inside the for loop.**********************************************************/ class for4 { //define the controlling classpublic static void main(String[] args){ //main methodint cnt = 5; //declare local method variable System.out.println("Value of method var named cnt is " + cnt); for(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

Empty clauses in a for loop

The first and third clauses in a for loop can be left empty but the semicolons must be there as placeholders.

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