Skip to main content

CHAPTER 2 :- CONDITIONAL STATEMENTS

The Control Flow Statements 



As we move deeper into the programing and logic building we recognize a high demand to control the flow of the program rather than shift the flow of the program based on the type of inputs and various types of results that are obtained through various calculations. For instance, let us assume we want to check whether a person is allowed to vote or he/she is prohibited. To check this we have to get the age of the person as an input and process the input to conditional operations so that we can allow or deny a person to solve this we can just use the if statement. But what if we need to process a humungous amount of information at a time and shift the flow of control accordingly so to get us out of these problems Ruby provides a verity of Control Flow Statements.

if statements

if-else-end statements

if-elsif-else-end statements

switch-when statements

unless

inline unless and if statements



if statements:-

The "if" statement is used to handle the flow of control throughout the program based on some conditions that are decided by the developer. For example, if we want to check the age of a user, and based on that we want to make a decision.


Example:      

if-else-end statements:-

The if-else-end statements are used when we want to ensure that if the condition is false and we need to show an error message or we want the user to perform certain other operations based on the failure of that particular condition we can easily apply it. For example, if the age of the student is less than 18 he/she can not cast there vote. so when the age of the student is less than 18 he/she must see an error message that he/she can not cast a vote.

Example:        

if-elsif-else-end Statements With Logical Operators:-

This block is somewhat different than the traditional if-else-end block here we can easily check the conditions multiple times and based on their truthy value we can operate through the program.
We can also use logical operations that can help us to combine and check multiple operations at the same time. For example, if we want to prepare the result of a student and assign divisions based on there percentage we may want to conditions to be checked at the same time so that we can Apply two extremes in our code or a range in our code. This is done by adding the logical AND( &&)  and logical OR (||).


Example:

Case-when statements:-

when it comes to larger use the "if-elsif-else block" is not appropriate to be used so in that situation the case_when statements come to save the day. let say we want to create a huge list of different products and the user will input the id of the product he wants to purchase so to write this program in the if-else block (formate ) it would turn out to be huge so to get rid of this hard work we use case-when statements. This can use a range or a particular number.


Example:

unless statements:-

The "Unless" statement  is just opposite of the if statement this executes the code present in its block when a certain condition is not meant to be fully filled. For example, unless a is equal to be true the code under the unless statement will be executed.


Example:

inline unless and if statements:-

The inline coding with unless and if statement can be used when we have just a single line of code to be executed. 
In the example below the value, 65 will be assigned to b and the value 34 will be assigned to c.


Example:


summary:-

I hope through this you get an idea about various types of control flow statements. The topic discussed above are a the basics that are used in the Rails framework and after this reading and practice you will get an idea of how to use and write some basic programs in rails. Apart from these statements various types of operators are used which you should check out here.


Comments

Popular posts from this blog

CHAPTER 1 :- RUBY OPERATORS

A Quick Guide To Ruby Operators As we work with any other language and getting started with any other language we are so used to get through the various fundamentals of that particular language. One of these fundamentals are the operators that are used in that language so that we can apply logic to our code and add certain functionality to our application and through this, we can make our application more towards the solution of the problem, so let as us check out how many types of operators we have in Ruby programing language. Many of them are listed below Types Of Operators: Arithmetic operators Logical operators Comparison Operators Assignment Operators Bitwise Operators Ternary Operator Range Operators defined? Operator Dot"." and Double Colon"::" Operators RubySplat Operator Matching Operator = = and = = = Summary  Now let us have a brief look at all the forms of operators Arithmetic Operators -:      The arithmetic operators are used to perform arithmetic oper...

CHAPTER 3 : - TYPES OF VARIABLES

tTypes Of Variables In Ruby  There are five types of variables in ruby all of them are discussed below:-   Variable Scope      Name Begin With  1} Constants Variable [ A - Z ]    2} Local Variable [ a - z ]  or _(underscore)  3} Global Variable $ ( Doller )  4} Instance Variable @ (at)  5} Class Variable @@ ( double at  )  we will look at all those one by one. Constant Variable:- This is the only Language that allows a constant value to changed value under the execution. The name of these variables is in all capital letters.  Example:-                           ...