Govur University Logo
--> --> --> -->
...

How does control flow work in Ruby? Provide examples of conditional statements and loops.



In Ruby, control flow refers to the order in which statements are executed based on certain conditions or loops. It allows the program to make decisions, repeat actions, and perform different sets of instructions based on specific criteria.

Conditional Statements:
Ruby provides several conditional statements that control the flow of execution based on the truth or falsity of certain conditions.

1. if Statement:
The `if` statement allows the program to execute a block of code only if a certain condition is true. It has the following syntax:

```
ruby`if condition
# code to execute if condition is true
end`
```
Example:

```
ruby`x = 5
if x > 3
puts "x is greater than 3"
end`
```
2. if-else Statement:
The `if-else` statement extends the `if` statement by providing an alternative block of code to execute when the condition is false. It has the following syntax:

```
ruby`if condition
# code to execute if condition is true
else
# code to execute if condition is false
end`
```
Example:

```
ruby`x = 5
if x > 10
puts "x is greater than 10"
else
puts "x is less than or equal to 10"
end`
```
3. elsif Statement:
The `elsif` statement allows checking for multiple conditions in a sequence. It is used when there are multiple conditions to be checked, and only one of them can be true. It has the following syntax:

```
ruby`if condition1
# code to execute if condition1 is true
elsif condition2
# code to execute if condition2 is true
else
# code to execute if all conditions are false
end`
```
Example:

```
ruby`x = 10
if x > 15
puts "x is greater than 15"
elsif x > 5
puts "x is greater than 5"
else
puts "x is less than or equal to 5"
end`
```
Loops:
Loops allow executing a block of code repeatedly until a specific condition is met or a specific number of iterations is completed.

1. while Loop:
The `while` loop repeatedly executes a block of code as long as a given condition is true. It has the following syntax:

```
ruby`while condition
# code to execute while condition is true
end`
```
Example:

```
ruby`count = 0
while count < 5
puts "Count: #{count}"
count += 1
end`
```
2. for Loop:
The `for` loop allows iterating over a specified range or collection. It assigns each element of the range or collection to a variable and executes the block of code for each iteration. It has the following syntax:

```
ruby`for variable in range_or_collection
# code to execute for each iteration
end`
```
Example:

```
ruby`fruits = ["apple", "banana", "orange"]
for fruit in fruits
puts fruit
end`
```
3. each Method:
The `each` method is commonly used in Ruby to iterate over collections like arrays, hashes, and ranges. It applies a given block of code to each element of the collection. It has the following syntax:

```
ruby`collection.each do |variable|
# code to execute for each element
end`
```
Example:

```
ruby`numbers = [1, 2, 3, 4, 5]
numbers.each do |number|
puts number
end`
```
Control flow in Ruby enables developers to create dynamic programs that respond to different conditions and repeat tasks efficiently. By using conditional statements