Control Structures - case Statements in Ruby

Control Structures – case Statements in Ruby

As we have seen in the previous lessons, we can check for multiple conditions using the if/elsif/else expression. A more simplified and flexible option is the case expression, which tests a value in when statements. You can have as many when statements as you need for a single case. Note that the case expression must be closed with the end keyword. case statements are used when you need to make different decisions based on a value.For example: Multiple values can be tested within a single when by separating the values with commas.For example: If you forget to put a comma between two values, Ruby will return a syntax error. syntax error, unexpected tINTEGER, expecting keyword_then or ‘,’ or ‘;’ or …

Read More
Logical Operators – and (&&), or (||), and not (!) in Ruby

Logical Operators – and (&&), or (||), and not (!) in Ruby

Logical operators are used to form more complex criteria that test more than one condition for an if expression. Today in this article we will discuss the logical operators used in Ruby language. Ruby has three logical operators: and (&&), or (||), and not (!). Ruby can use words instead of the logical operator symbols (and, or, not), but those have lower precedence and are generally avoided. and (&&) A conditional using and evaluates as true if, and only if, all of its operands are true. Otherwise, the entire conditional evaluates to false.For example:

or (||) The or (||) operator evaluates to true if either (or all) of its operands are true, and false if all operands are false.For …

Read More
Control Structures – unless Statement in Ruby

Control Structures – unless Statement in Ruby

Control Structure statement is the statement that controls the working flow of any program depending on its condition. Today in this article we will discuss one of the Basic Control Statement of Ruby i.e. unless unless Statements The unless expression is the opposite of an if expression. It executes the line of codes when a conditional is false. You can use an else block with the unless, just like you did with the if expression. The end keyword is also required to close the block.For example: The unless modifiers can also be used to execute code.For example:

Read More
Control Structures – If, elsif, else Statements in Ruby

Control Structures – if, elsif, else Statements in Ruby

Control Structure statement is the statement that controls the working flow of any program depending on its condition. Today in this article we will discuss the Basic Control Statements of Ruby i.e. if, elsif, else. if Statements You can use an if expression to execute the lines of code when a certain condition met. Each if has to have a corresponding end.For Example: The if modifiers can also be used to execute code.For example: elsif Statements The elsif (short form of else if) block is useful when you want to test multiple if conditions.A series of if elsif expressions can have a final else block, which is called if none of the if or elsif expressions are true. When an …

Read More