Program 1:
case 50
when 11...20
puts "Number is within the range of 11 to 20";
when 21...30
puts "Number is within the range of 21 to 30";
when 31...40
puts "Number is within the range of 31 to 40";
when 41...50
puts "Number is within the range of 41 to 50";
else
puts "Unknown number";
end
Program 2:
case 49
when 11...20
puts "Number is within the range of 11 to 20";
break;
when 21...30
puts "Number is within the range of 21 to 30";
break;
when 31...40
puts "Number is within the range of 31 to 40";
break;
when 41...50
puts "Number is within the range of 41 to 50";
break;
else
puts "Unknown number";
end
Program 3:
case 49
when 11...20
puts "Number is within the range of 11 to 20";
when 21...30
puts "Number is within the range of 21 to 30";
when 31...40
puts "Number is within the range of 31 to 40";
when 41...50
puts "Number is within the range of 41 to 50";
default
puts "Unknown number";
end
Program 4:
case 50
when 10,20,30
puts "TEN TWENTY THIRTY";
when 40,50,60
puts "FORTY FIFTY SIXTY";
when 70,80,90
puts "SEVENTY EIGHTY NINETY";
else
puts "Unknown number";
end
Program 5:
str = "SUN";
case
when "sun"
puts "Weekday is SUNDAY";
when "mon"
puts "Weekday is MONDAY";
when "tue"
puts "Weekday is TUESDAY";
when "wed"
puts "Weekday is WEDNESDAY";
when "thu"
puts "Weekday is THURSDAY";
when "fri"
puts "Weekday is FRIDAY";
when "sat"
puts "Weekday is SATURDAY";
else
puts "Unknown week";
end
Answer Program 1:
Output:
Explanation:
In the above program, we created case blocks. In the code there is no case-matched because when we used "..." for the range it excludes the last digit in the range. That's why the else part gets executed and printed "Unknown number" message.
Answer Program 2:
Output:
Explanation:
The above program will generate a syntax error because we cannot use the break statement in the when block.
Answer Program 3:
Output:
Explanation:
In the above program, we created case blocks. Here, case 41..50 matched the given number with range, and print the appropriate message on the console screen. After that, runtime error gets generated due to default.
Answer Program 4:
Output:
Explanation:
In the above program, we created case blocks. Here, we used multiple values with the when block to match the given value. Then it matched with the "when 40,50,60" block and printed a "FORTY FIFTY SIXTY" message.
Answer Program 5:
Output:
Explanation:
In the above program, we created a variable str initialized with "SUN". Then we matched the value of the str variable with the when block and printed the "Weekday is SUNDAY" message.
need an explanation for this answer? contact us directly to get an explanation for this answer