Category: 3. Ruby Control Statement
-
Ruby Comments
Ruby comments are non executable lines in a program. These lines are ignored by the interpreter hence they don’t execute while execution of a program. They are written by a programmer to explain their code so that others who look at the code will understand it in a better way. Types of Ruby comments: Ruby…
-
Ruby redo Statement
Ruby redo statement is used to repeat the current iteration of the loop. The redo statement is executed without evaluating the loop’s condition. The redo statement is used inside a loop. Syntax: Example: Output: Ruby retry Statement Ruby retry statement is used to repeat the whole loop iteration from the start. The retry statement is…
-
Ruby Break Statement
The Ruby break statement is used to terminate a loop. It is mostly used in while loop where value is printed till the condition is true, then break statement terminates the loop. The break statement is called from inside the loop. Syntax: Example: Output: Ruby Next Statement The Ruby next statement is used to skip…
-
Ruby Until Loop
The Ruby until loop runs until the given condition evaluates to true. It exits the loop when condition becomes true. It is just opposite of the while loop which runs until the given condition evaluates to false. The until loop allows you to write code which is more readable and logical. Syntax: Example: Output:
-
Ruby while Loop
The Ruby while loop is used to iterate a program several times. If the number of iterations is not fixed for a program, while loop is used. Ruby while loop executes a condition while a condition is true. Once the condition becomes false, while loop stops its execution. Syntax: Example: Output: Ruby do while Loop…
-
Ruby for Loop
Ruby for loop iterates over a specific range of numbers. Hence, for loop is used if a program has fixed number of iterations. Ruby for loop will execute once for each element in expression. Syntax: Ruby for loop using range Example: Output: Ruby for loop using array Example: Output:
-
Ruby Case Statement
In Ruby, we use ‘case’ instead of ‘switch’ and ‘when’ instead of ‘case’. The case statement matches one statement with multiple conditions just like a switch statement in other languages. Syntax: Example: Output: Look at the above output, conditions are case sensitive. Hence, the output for ‘Saturday’ and ‘saturday’ are different.
-
Ruby If-else Statement
The Ruby if else statement is used to test condition. There are various types of if statement in Ruby. Ruby if statement Ruby if statement tests the condition. The if block statement is executed if condition is true. Syntax: Example: Output: Ruby if else Ruby if else statement tests the condition. The if block statement…