Java

Topic: Flowcontrol

Explain Switch Case Statement?

The switch case statement, also called a case statement is a multi-way branch with several choices. A switch is easier to implement than a series of if/else statements. The switch statement begins with a keyword, followed by an expression that equates to a no long integral value. Following the controlling expression is a code block that contains zero or more labeled cases. Each label must equate to an integer constant and each must be unique. When the switch statement executes, it compares the value of the controlling expression to the values of each case label. The program will select the value of the case label that equals the value of the controlling expression and branch down that path to the end of the code block. If none of the case label values match, then none of the codes within the switch statement code block will be executed. Java includes a default label to use in cases where there are no matches. We can have a nested switch within a case block of an outer switch.

Browse random answers:

Find the output of this program
public void foo( boolean a, boolean b){
if( a ){  
System.out.println("A"); /* Line 5 */ 
}
else if(a && b) /* Line 7 */ 
{System.out.println( "A && B");}  
else /* Line 11 */ 
{  
if ( !b ){  
System.out.println( "notB") ; }  
else {System.out.println( "ELSE" ) ;}  
} 
}
Which two are acceptable types for x?
switch(x) {  
default:  
System.out.println("Hello"); } 

 
byte 
long 
char 
float 
Short 
Long

A. 	1 and 3	
B. 	2 and 4
C. 	3 and 5	
D. 	4 and 6
Which of the below statement is true?
public void test(int x) {  
int odd = 1;  
if(odd) /* Line 4 */ {  System.out.println("odd");  }  
else  {  System.out.println("even");  } 
}

A. 	Compilation fails.
B. 	"odd" will always be output.
C. 	"even" will always be output.
D. 	"odd" will be output for odd values of x, and "even" for even values.
Which of the below statement is true?
public class While { 
public void loop(){  int x= 0;  while ( 1 ) /* Line 6 */  {  System.out.print("x plus one is " + (x + 1)); /* Line 8 */  
} 
}
}

A. There is a syntax error on line 1.
B. There are syntax errors on lines 1 and 6.
C.There are syntax errors on lines 1, 6, and 8.
D. There is a syntax error on line 6
How do you write an infinite loop using the for statement? 
How do you write an infinite loop using the while statement? 
Consider the following code snippet.if (aNumber >= 0)    if (aNumber == 0)        System.out.println("first string");else     System.out.println("second string");System.out.println("third string");    Exercise: What output do you think the code will produce if aNumber is 3? 
Write a test program containing the previous code snippet; make aNumber 3. What is the output of the program? Is it what you predicted? Explain why the output is what it is. In other words, what is the control flow for the code snippet?
Explain the If-else Statement ?
Explain Switch Case Statement?
Write switch case example?
Write syntax of while?
Write example of while loop?
Write syntax of do while?
Write example of do while?
Write explain Syntax of for loop?
Write example of for loop?