In: Computer Science
Instructions: Code a for statement that increments from 1 through 10, but print only the even numbers using a fall-thru switch. You can catch the odd numbers using the option in the switch that handles everything else. Name your program ForFallThruSwitch.java. Use Java Style Guide in line advancing and spacing.
--------------------OUTPUT RESULTS--------------------
Not printing odd numbers!
2 is an even number.
Not printing odd numbers!
4 is an even number.
Not printing odd numbers!
6 is an even number.
Not printing odd numbers!
8 is an even number.
Not printing odd numbers!
10 is an even number.
//class header
//Begin class scope.
//main method header
//Begin method scope.
//for header using i as loop-control variable
//Begin for scope.
//Code switch expression.
//Begin switch scope.
//First case.
//Next statement.
//Next statement.
//Next statement.
//Next statement.
//Print the number and that it is even, e.g., “2 is an even number.”
//Next statement.
//Code option that handles the odd numbers.
//Print "Not printing odd numbers!”
//Close switch scope.
//Close for scope.
//Exit program.
//End method scope.
//End class scope.
import java.util.*;
import java.lang.*;
import java.io.*;
class ForFallThruSwitch
{
public static void main (String[] args)
{
for(int i=1;i<=10;i++)
{
//fall through switch
switch(i){
//handles all even cases
case 2:
case 4:
case 6:
case 8:
case 10: System.out.println(i+" is an even number.");
System.out.println();
break;
//handles all odd cases
case 1:
case 3:
case 5:
case 7:
case 9: System.out.println("Not printing odd numbers!");
System.out.println();
break;
}
}
}
}
Output:-