ADVERTISEMENT
ADVERTISEMENT

Ruby case statement – Find output programs (set 2)

This section contains the Ruby case statements find output programs (set 2) with their output and explanations.
Submitted by Nidhi, on February 12, 2022

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

Output:

Unknown number

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.


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

Output:

HelloWorld.rb:4: Invalid break
HelloWorld.rb: compile error (SyntaxError)

Explanation:

The above program will generate a syntax error because we cannot use the break statement in the when block.


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

Output:

Number is within the range of 41 to 50

HelloWorld.rb:14:in `<main>': undefined local variable or method `default' 
for main:Object (NameError)

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.


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

Output:

FORTY FIFTY SIXTY

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.


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

Output:

Weekday is SUNDAY

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.

Ruby Find Output Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.