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:-
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.
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.
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 (||).
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.
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.
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.
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
Post a Comment