-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional_control_flow.rb
More file actions
121 lines (107 loc) · 4.07 KB
/
Copy pathconditional_control_flow.rb
File metadata and controls
121 lines (107 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Conditional Statement
# This program demonstrates the use of conditional statements in Ruby.
# Conditional statements allow you to execute different blocks of code based on certain conditions.
# You can use if statement, switch statement, switch expression, ternary operator, unless statement, and single line if statements to create conditional logic in your Ruby programs.
# If statement
# The if statement is used to execute a block of code if a specified condition is true. You can also use elsif and else to handle multiple conditions.
# If conditions
# - `if`: Executes the block of code if the condition is true.
# - `elsif`: Executes the block of code if the previous condition(s) were false and this condition is true.
# - `else`: Executes the block of code if all previous conditions were false.
# The syntax for an if statement is as follows:
# if condition
# # code to execute if condition is true
# elsif another_condition
# # code to execute if another_condition is true
# else
# # code to execute if all conditions are false
# end
number = 10
if number > 0
puts "The number is positive."
elsif number < 0
puts "The number is negative."
else
puts "The number is zero."
end
# Ternary operator
# The ternary operator is a shorthand way of writing an if-else statement. It takes three operands: a condition, a result for true, and a result for false.
# The syntax for the ternary operator is: `condition ? expression_if_true : expression_if_false`.
age = 18
message = age >= 18 ? "You are an adult." : "You are a minor."
puts message
# Unless statement
# The unless statement is the opposite of the if statement. It executes a block of code only if the specified condition is false.
# The syntax for an unless statement is as follows:
# unless condition
# # code to execute if condition is false
# else
# # code to execute if condition is true
# end
is_raining = false
unless is_raining
puts "It's a nice day outside!"
else
puts "Don't forget to take an umbrella!"
end
# Switch statement
# Ruby does not have a built-in switch statement like some other programming languages, but you can achieve similar functionality using a case statement. The case statement allows you to compare a value against multiple conditions and execute the corresponding block of code.
# The syntax for a case statement is as follows:
# case variable
# when value1
# # code to execute if variable == value1
# when value2
# # code to execute if variable == value2
# else
# # code to execute if variable does not match any of the values
# end
day = "Monday"
case day
when "Monday"
puts "It's the start of the week."
when "Friday"
puts "It's almost the weekend!"
else
puts "It's just another day."
end
# Switch expression
# In Ruby 2.7 and later, you can use the case statement as an expression, which means it can return a value. This allows you to assign the result of a case statement to a variable.
# The syntax for a case expression is similar to a case statement, but you can assign the result to a variable.
# variable = case variable
# when value1
# expression_if_value1
# when value2
# expression_if_value2
# else
# expression_if_no_match
# end
day = "Tuesday"
message = case day
when "Monday"
"It's the start of the week."
when "Friday"
"It's almost the weekend!"
else
"It's just another day."
end
puts message
# Or you can use then after when to write the case statement in a single line.
# variable = case variable
# when value1 then expression_if_value1
# when value2 then expression_if_value2
# else expression_if_no_match
# end
day = "Wednesday"
message = case day
when "Monday" then "It's the start of the week."
when "Friday" then "It's almost the weekend!"
else "It's just another day."
end
puts message
# Single line if statement
# You can also write an if statement in a single line using the modifier form. This is useful for simple conditions and can make your code more concise.
# The syntax for a single line if statement is: `puts "message" if condition`.
number = 5
puts "The number is positive." if number > 0
puts "The number is negative." if number < 0
puts "The number is zero." if number == 0