2024-09-24 22:39:42 +00:00
|
|
|
#!/usr/bin/env bellos
|
|
|
|
# File: control_structures.bellos
|
|
|
|
|
|
|
|
# Demonstrating if statements and loops
|
|
|
|
|
2024-10-03 04:14:47 +00:00
|
|
|
# If-else statement
|
|
|
|
echo "If-else statement:"
|
|
|
|
x=10
|
|
|
|
if [ $x -gt 5 ]; then
|
|
|
|
echo "x is greater than 5"
|
2024-09-24 22:39:42 +00:00
|
|
|
else
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "x is not greater than 5"
|
2024-09-24 22:39:42 +00:00
|
|
|
fi
|
|
|
|
|
2024-10-03 04:14:47 +00:00
|
|
|
# Nested if-else
|
|
|
|
echo "\nNested if-else:"
|
|
|
|
y=20
|
|
|
|
if [ $x -gt 5 ]; then
|
|
|
|
if [ $y -gt 15 ]; then
|
|
|
|
echo "x is greater than 5 and y is greater than 15"
|
|
|
|
else
|
|
|
|
echo "x is greater than 5 but y is not greater than 15"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "x is not greater than 5"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# While loop
|
|
|
|
echo "\nWhile loop:"
|
|
|
|
counter=0
|
|
|
|
while [ $counter -lt 5 ]; do
|
|
|
|
echo "Counter: $counter"
|
|
|
|
counter=$((counter + 1))
|
|
|
|
done
|
|
|
|
|
2024-09-24 22:39:42 +00:00
|
|
|
# For loop
|
2024-10-03 04:14:47 +00:00
|
|
|
echo "\nFor loop:"
|
|
|
|
for i in 1 2 3 4 5; do
|
|
|
|
echo "Iteration: $i"
|
2024-09-24 22:39:42 +00:00
|
|
|
done
|
|
|
|
|
2024-10-03 04:14:47 +00:00
|
|
|
# For loop with range
|
|
|
|
echo "\nFor loop with range:"
|
|
|
|
for i in $(seq 1 5); do
|
|
|
|
echo "Number: $i"
|
2024-09-24 22:39:42 +00:00
|
|
|
done
|
2024-10-03 04:14:47 +00:00
|
|
|
|
|
|
|
# Case statement
|
|
|
|
echo "\nCase statement:"
|
|
|
|
fruit="apple"
|
|
|
|
case $fruit in
|
|
|
|
"apple")
|
|
|
|
echo "It's an apple"
|
|
|
|
;;
|
|
|
|
"banana")
|
|
|
|
echo "It's a banana"
|
|
|
|
;;
|
|
|
|
"orange")
|
|
|
|
echo "It's an orange"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown fruit"
|
|
|
|
;;
|
|
|
|
esac
|