• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification from Khalid Mughal's mock exam

 
Jaikumar Nair
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is one of the questions from the mock test of Khalid Mughal :
Which statements concerning the switch construct are true?
(a) all switch stmts must have a default label
(b) there must be exactly one label for each code segment in a switch statement
(c) the keyword continue can never occur within the body of a switch statement
(d) no case label may follow a default label within a single switch stmt
(e) a character literal can be used as a value for a case label
According to the mock exam "e" is the only correct answer.
But I am quite sure that "c" is also the correct answer. Could someone comment on this.
Regards,
JAI
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jaikumar,
Check out the following code.

Notice the continue statement inside the switch statement?
Try to compile and run it ...
P.S. The continue keyword can be used within loops only!
Regards,
Manfred.
 
Nandini Gangopadhyay
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too think c is a correct answer, if we give a continue, where will it take us, there is no new iteration taking place here. Could someone else clarify too?!!
Nandini
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Per the JLS 14.15 The continue Statement

A continue statement may occur only in a while, do, or for statement; statements of these three kinds are called iteration statements. Control passes to the loop-continuation point of an iteration statement.
 
Jaikumar Nair
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manfred,
I am aware of that and the example that you provided will run.
The option "c" states "the keyword continue can never occur within the body of a switch statement". Is it not a bit ambiguous? Lets take the example you have given. In the code, though "continue" is within the "for...loop", it is appearing within the body of a switch statement. In that respect, option "c" should be true. Do correct me if I am wrong
JAI
 
vkswamy venkatachalam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, jayakumar
Answer c may be wrong.The compiler fails to compile only when
there is 'continue' key word only inside a case.Try the following code.
public class Test1 {
public static void main(String args[]) {
int i = 5;
loop:for(int j=0;j<10;j++){
switch( i ){
case 4:
break;
case 5:
//inserted for knowing what is going inside.
//if U uncomment, donot forget to add { }
//{System.out.println("Inside case 5 : for loop i : "+i);
continue loop;
//if continue without loop and for loop the compiler rejects
default:
System.out.println("Hello World!");
}
System.out.println("end of for loop i : "+i);
}
}
}
the variant of continue works here.so the otion 'c' may be wrong.
Cindy Glass, correct me if I am wrong.
Regards,
vkswami
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic