Using Loop constructs with embedded switch statements

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

Moderator: admin

Post Reply
cvsignin
Posts: 4
Joined: Fri Jun 24, 2016 10:19 am
Contact:

Using Loop constructs with embedded switch statements

Post by cvsignin »

Hi Admin,

One more small query.I executed the below:

Code: Select all

LOOP: for (i = 0; i <= 5; i++) {
			switch (i = 6) {
			case '0':
				System.out.println("A");
			case 1:
				System.out.println("B");
				break LOOP;
			case 2:
				System.out.println("C");
				break;
			case 3:
				System.out.println("D");
				break;
			case 4:
				System.out.println("E");
			case 'E':
				System.out.println("F");
			}
		}
I was wondering what would be the value of i being passed.The question might seem bad coding but i was curious to find value of i which instead of being passed as i=6 was passed as 0 for iteration!
If the variables in switch statements are compile time constants then i would assume that i=6 is also being assigned at compile time as it is assigned to a fixed value. Please help.
Last edited by admin on Mon Jul 25, 2016 8:26 pm, edited 2 times in total.
Reason: Please put code within [code] [/code] so that it is easier to read

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

Re: Using Loop constructs with embedded switch statements

Post by admin »

Try adding a System.out.println(i); to see what value is being passed.

No, this is not a compile time constant. See this for a good understanding about what makes a compile time constant: http://www.coderanch.com/t/454384/java/ ... e-constant

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

Re: Using Loop constructs with embedded switch statements

Post by admin »

admin wrote:Try adding a System.out.println(i); to see what value is being passed.
No, this is not a compile time constant. See this for a good understanding about what makes a compile time constant: http://www.coderanch.com/t/454384/java/ ... e-constant
If the variables in switch statements are compile time constants then i would assume that i=6 is also being assigned at compile time as it is assigned to a fixed value. Please help.
You seemed to be confused between variables are constants. i is a variable. Its value can change. Just because you assign 6 to i doesn't mean that is i is a constant. You can assign something else to i later.
If i were a constant, you would not have been able to assign anything else to i later.

I will suggest you to read a good book to get your basic concepts correct before attempting mock exams.

cvsignin
Posts: 4
Joined: Fri Jun 24, 2016 10:19 am
Contact:

Re: Using Loop constructs with embedded switch statements

Post by cvsignin »

Thanks Paul for your reply. For now being i find JLS not a good understanding read. I will try find some other source.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests