Hello and welcome to this lecture.
In this lecture I'll be explaining what conditional statements are in the Ruby programming language.
What are conditional statements.
Conditional state measures are basically used to perform different actions based on different conditions.
In Ruby and most programming languages they use conditional statements.
So when you write code you may want to perform different actions for different types of decisions.
You do this using conditional statements.
There are three main types of conditional statements in Ruby.
The first is known as the if statement.
Basically what this appears is that the piece of code inside this state may execute if the condition
specified inside the IF statement turns out to be true.
If the condition is false then the second type of conditional statement is executed which is the else
statement.
So who else will execute the code block if the condition in the block is wrong or it evaluates to be
false.
If it's true then there is no action for the house because the code will have executed are there is
another conditional statement that is else.
If so what this does.
It basically specifies a new set of condition.
If the condition in the first block is false so it was both fresh new condition to test the condition
in the first block is faulkes.
Let's quickly illustrate how this works in the first example here.
Quite a variable x which we said is less than 17 so we think if it is less than 17 then display below
17.
If he is not listed in the second block of call what is it tell us if he if x is greater than 27 which
is in new condition then display over 27 else display 17 to 20.
So every time you write a conditional statement you must type an end so that Ruby knows you've ended
the statement.
Ruby is quite unique compared to more so that programming language should only curly braces and all
that Ruby knows what to do once he gets to where each of the conditional statements are equal to what
he has to do.
As long as you put the end when this statement meant Ruby would do the rest.
Another quick example is this we say we test in a quick application here to check the drinking age limit
so we've got conditional statement.
To do that so this is just a simple fit meant to see if this city is equal to London.
Then set the drinking age to 18.
Are the wise set the drinking age to 21.
I've got interactive Ruby so I am going to illustrate how conditional stick meant work.
I've already pre written my move so I'm just going to pace in airline at a time to save me typing.
So first I got a variable and I've set the variable name to blue line.
I press enter.
So begin the conditional testing and the first is if it meant So I'm testing the condition.
Usually when you test conditions you use in comparison operators here.
So I'm seeing even the name that name you they mean if it is equal to blue line then I wanted to do
whatever I specify inside this conditional state so the condition turns out to be true.
It all executed this piece of code.
If it is false I need to add a health condition it will execute the code I put within the health statement.
So if the condition is wrong it will display this message on the screen.
Nothing will happen yet because I have not ended the statement so Ruby doesn't know if I want to write
in.
So now I can end this statement.
Now this statement has ended.
If I press enter to see you are not blue line.
What that means is that it is executing the code in the else statement because the condition in the
statement was not true.
All right I'm going to do another conditional this time going to include the else.
If so and not a condition here empty.
If the name is blueline.
Then I want this message to be displayed.
Blue line gives a good discount if this condition is true if it is not.
I need to specify and else so I'm specifying an else if Okay.
So this darton a new condition yes this testing in new condition here case so if this name if this condition
is not met and put a new quantity on here then name me the name is sequel's to London uni then I need
to specify the piece of code that will execute.
So if the condition in the ELSE is true it will display come on London if it is not true.
I need to specify and else and then specify the code that will display in the else statement.
So the condition is not true in the Healthy of record I want to display.
Is this inside a statement of C not London you or Kim.
Now I mean to end the statement by typing in an end stedman's So that's it for press Enter now the conditional
STD May will execute.
So you can see now says blue line gives a good discount cause the condition in the if statement is true
evaluated to true.
So the piece of code in the else.
If anybody else when not executed only become only the code inside here block.
So that's basically how conditional statements work.
Thanks for watching and buyoff for there.