Home
Preface
Chapter Selection
Programming Examples
Homework Problems
Homework Solutions MEA's
Search Useful Links

Programming Examples

Chapter 3 Programming Examples

Comparing an Integer Value
1	puts "Enter a value ==> "
2	input = gets  
3	input = input.to_i 
4	if (input > 5)
5		puts 'You have entered a value greater than 5.'
6	else
7		puts 'Please try again and enter a value greater than 5.'
8	end
9	
 
 
Comparing Two Integers
1	puts "enter a value for x ==>"
2	x = gets
3	x = x.to_i
4
5	puts "enter a value for y ==>"
6	y = gets
7	y = y.to_i
8
9	if x > 5 and y > 5
10		puts "x and y are greater than 5."
11	else
12		if x > 5
13			puts "only x is greater than 5."
14		else
15			if y > 5
16				puts "only y is greather than 5."
17			else
18				puts "both x and y are smaller than or equal to 5."
19			
20			end
21		end
22	end
23	
 
 
Comparing Two Integers Using Case Statement
1	puts "enter a value for x ==> "
2	x = gets
3	x = x.to_i
4
5	puts "enter a value for y ==> "
6	y = gets
7	y = y.to_i
8
9	case
10	when x > 5 and y > 5 then 
           puts "x and y are bigger than 5"
11	when x > 5 and y < 5 then 
           puts "only x is bigger than 5"
12	when y > 5 then 
           puts “only y is bigger than 5”
13	else
14	   puts "both x and y are smaller than 5"
15	end