enthuware.ocajp.i.v8.2.1327: local variables

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

enthuware.ocajp.i.v8.2.1327: local variables

Post by heleneshaikh »

Hi,

Local variables don't have a default value. If the enhanced for-loop is not executed because of a false condition, how can the values be printed on line 5?

Code: Select all

 public static void main(String args[]) {
        int i;
        int j;
        for (i = 0, j = 0; j < i; ++j, i++) {
            System.out.println(i + " " + j);
        }
        System.out.println(i + " " + j); //5. Not initialised if loop doesn't run
    }

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v8.2.1327: local variables

Post by admin »

It is true that the loop body doesn't execute but the initialization section of a for loop always executes. The condition expression also executes at least once. If it returns false, the increment section doesn't execute.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

Re: enthuware.ocajp.i.v8.2.1327: local variables

Post by heleneshaikh »

Thanks I really appreciate the quick reply. I understand it now.

Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests