Hello and welcome to this lecture on condition of statements.
Sometimes when you write code you may not want the code to run all the time or you may want certain
parts of the code to execute only when certain conditions have been met.
So that is what is referred to as conditional statements that means code that will run based on certain
conditions.
And the most common conditional statements are if statements and also else statements so the simplest
conditional statement is an if statement.
So basically you specify a condition that if the condition is met then a piece of code will execute
if the condition is not met then you can also attack and else statement an L statement basically will
execute the statement if the statement in the if condition is not met then he executes the else statement.
So I'm going to show you an example how this works.
So I've got visual studio open I'm going to open up a new project going to create a new producer I'll
go file new project and wait for it to load I'm gonna make it a Windows form and I'll just give you
the name of conditional
click okay and just wait for it to generate the project so Visual Studio has generated a basic form
for us what will it do I'm gonna add a button control so I'm going to click on the tool box and just
drag a button control into the tool box.
Okay I'm just going to write create and change the button name click on the properties and I'll change
it to
greats.
Okay.
This
Okay so double click on that button and when we double click it will generate an event.
Angela OK so we have got an event and here an event handler method.
This here's an event handler method.
The event it's going to respond to is a click.
So the event basically is something that a user or visitor performs on a Web site.
The common event on the button is a click when you see a button you click on it
when the button is clicked.
We want a piece of code to execute so that we can write the code in between this curly these two curly
braces here's where we're going to specify the code what I want to happen.
I have called the I want to create a some conditional code here based on certain grades that a student
will get in the exams.
So what I'll do I will define an initial mark.
And then if the total mark is too then gets is higher than the initial mark specified in message box
would display in the button showing.
If the user the student go to first class got a higher first class got a pass or got a fail or failed.
So these are the various messages are like to display.
So to start with I need to define a variable
or define variable.
You need a variable for the if statement to work with and I'll call the variable marks or just say final
marks.
Okay.
And I've set given an initial value of say
make eight fifty nine okay.
So now I am going to specify some conditional code.
So to do that I'll type in if and when use right conditional code you need to specify a condition so
the condition here is that I've defined a variable called final marks.
Some saying if the final marks this variable here of this student if the final mark
is greater than or equal to say 75
then this Scully this rest quickly line is complaining because with an if statement you need to have
curly braces and in between these curly braces it is where you write the code that will execute if this
condition I have specified here is met.
So if this student Mark is greater than 70 is greater or equal to 75 it will execute this code if it
is not it will execute another block of code.
So I say if the final marks of the student is greater than or equal to 75 I want the message to be displayed.
So to do that I'm going to use a message box.
This is a built in function or method in C sharp.
So I'll type a message
C notice I'm trying to type a message box or type a message box.
All right.
Dot need to assign a method is called a show method.
So message box to show and I need to specify what message it would display.
So if these students score is greater or equal to 75 I wanted to display
first class
or you got first class
okay then puts a semicolon there to end that statement.
So if this condition is met this code here will execute if the condition here specified is not met then
I can specify an else statement.
So this is how you specify an L statement.
You type in else
okay and insert and then you type in else this quickly line is complaining because I've not included
my curly braces so I need to specify the condition for the L statement inside here.
So if this condition here is not met it will display this message box ignore it all.
If this condition is not met it will skip this block of code.
It will not execute this at all.
Thus why is a conditional statement.
It will now jump on to this else statement.
So I'm going to define a code here I'm going to say.
Message
message box don't show.
I'm going to say you got
a
second
class
so this is a typical classic conditional statement saying if this condition is met.
If the student gets greater or higher than 75 it would display this.
If not it will display this message so I'll save that and execute the code.
OK I'll just wait for it to run.
So if I click on the button that says grades
it tells me you got a second class.
So because the condition was not met because initially final marks he offset is 59.
So I've said if the final mark is greater than 75 which is not because the math final marks is this
the reason is not greater than that.
That's why this block of code was skipped and he showed this block of code instead.
If I was to change that to say I change that to 78 then this if statement this condition will become
true and it will execute this and not even bother by the second code.
All right let's try that.
Okay.
So if I click button it says you got a first class okay because the first condition which is the if
statement has been met.
So he didn't bother executing the second part of the code.
So this is a very simple basic conditional statement.
Conditional statements are very important in your work as a programmer or developer.