Welcome back
So in two previous tutorial, we've been doing the conditional "if" statement
so let's go ahead and make something meaningful out of it.
Let's go ahead and change this part
and say "if(a > b)", I would like you to output that a is greater than b
So let's do it like this
"A is greater than B"
and if they're not that, then I can write something here
but look -- We have three possibilities and we only have two conditions here.
We have three possible cases here
but we only have we've only covered basically two of them although
the last statement covers pretty much all of them how does this make sense
well you know if a is greater than be this will be printed out any other case
it will meet the program the program will immediately jumped to the machine
will immediately jumped to D else statement and execute whatever whatever
is in DL statement however look if I want to write here I would like to rate
here is less then be how would I write some how would a specific case where a
is equal to be well I could deepen else if else if
is to be inserted between can have as many as many else ifs as you like so you
can cover pretty much an infinite amount of cases see out is to be ok so now we
have three cases let's go ahead and combined us with user input can take
note this is my name for the previous tutorial go ahead and type in Sialkot
enter value a see in a and then let's give it a lot still the user to enter a
value for beat and here
be here
their ego so distraught run once and we'll go ahead do this pile with ctrl
f11 key so enter value for let's say that a will be one enter value for bee
bee will be three so now it should print out that a is less than be less than be
but suppose we wanted to do
bestest multiple times this program it finishes this state is pretty much
finishes this so you enter a value and as soon as the user enters the values
the computer jumped to this portion it finishes this conditional statement and
it executes the proper condition the condition that develops the true so I
there is a is equal to be or something else and this particular case we only
have three possibilities but you could have a hundred possibilities and then if
you missed one else would always cover it although you need to make that makes
some sense anyway now we are going to learn about loops now we would like to
put this in a repetitively where things are going to repeat themselves until we
tell them to stop one of the loops which we can use is while loop so let's go
ahead and apps let's go ahead and insert the while loop of this while and how
many times have you like this to repeat well we don't really know so let's set
it to repeat as many times as long as we let set of repetitive cycle as long as
we the user tell it to stop so let's see through or you could just write one here
one stands for true and 0 stands or falls
balls or editors most of the times it's false or true any easier to read but
owell that's just your preference rate would you like whether you want to write
one or true completely up to you and notice that we have to close this just a
little bit expand the screen a little bit so we have to open up a curly
bracket and close a curly bracket in order to encompass this piece of code so
that would be inside of the loop and I'm just gonna press attempts earlier and we
also want to user input to be within the loop so let's go ahead and place the
user
ok so now we have this and now we have this we have the user input and we have
a conditional statement down below
now all we need is a something to terminate will it say that the negative
numbers will turn me into the value for a or enter or enter minus 12 exit
copiers go to the end and a space here ok so we can press one to accept and how
would we exit so maybe in another we could we could theoretically tape else
if here and then type in either A or B is equal 2-1 and then we would just AP
in break if you are using else if we if you're using while loops or something of
a kind
the only case for you won't need these curly brackets is when you have a single
state in single single instruction that is right that is below it and that's it
that if you don't have curly brackets it will only take into consideration one
single instruction and it will pretty much disregard the loop or conditional
statement it will disregard the rest now what does break too well try to figure
it out i mean in English language it just breaks out it breaks out of the
loop and by that I mean it exists the field of this curly bracket and its
gonna jump straight to return
0 so let's go ahead and CEO something that tells us that the program is over
and done with it came over for the like that charlie center here but you see
this is a very inefficient way to do it why tried to figure it out by the video
if you like but I'm gonna go ahead and tell it to you now you see if we place
the else if they didn't hear the conditional statement the program will
indeed exit when we press minus one it will execute to break statement it will
exit the loop and then it will execute this command here and then it will
return a zero it will be over the function will end but our condition for
exiting to prove we want our condition to program to be when a user enters
minus 1 484 when a user enters minus 14 be since you have input here you will
first input a and even if you put this one for a it will go and ask you for the
input into it will go ahead and ask you to be so we have to import be valued as
well and then it will jump to the conditional statement and figure out
that a is minus one or the bees minus one and will break out of the loop
that's that's kinda deficient you know because we only need one of these values
and you are being forced to take the in both well simple solution being copied
cut the same here
appear so if a and it's one obviously just evaluating
and we can take place if underneath be aware we do not require 82 not required
you valuation of a look now we import a and if a business plan it will break
immediately and then we import be in case a is not equal 2-1 so i cn great
greeted integrated in be entered by the user inputs be and then he gets
evaluated so if B is once again it will break out of the loop and it will end
the program far more efficient way since you only need to enter one of them and
not both of them to be evaluated and then and only then if they get one of
them is minus one rule we actually this portion of the code now this is very
significant because you do need to pay little attention doesn't have much to do
with programming discussed has something to do with general logic of things right
things in a meaningful way so why why enter both of them and a battle read
both of them if we only need one of them to know whether we can proceed with the
program or not or whether we want to proceed with a programmer not so let's
go ahead and see how this works ok here we go
finished enter the value for a 4-1 to exit so let's go ahead and do it let's
go ahead and 210 and to develop or be tremendous 12 exit at 10:42 less than be
entered the value so what do we need to we need to add a new lines in order to
managed in order for this to look prettier so let's they've been minus one
game over it
X discuss successfully but you see a is less than be when it prints out the loop
repeats and its kinda ugly as he ran through the value for a 3-1 to exist but
it's right here there is a space it's quite ugly and mean you can't
differentiate did so what can we do well we could enter a new line for each one
of these see out statements that the one wit us are enough for these things have
deleted it actually every go control y is for and do so you're gonna get used
to pressing down so I was trying to say is that we could add and dealt to the
end of each of these lines and L
we only have to give them so that's a realistic option for us but is the head
something that we really want to do let's see if we can do something better
because imagine if you had ten thousand of these lines to each one of those ten
thousand you would have to add and then it really wouldn't be a realistic option
would it now rather instead he will do this and all its still not gonna be as
pretty as we like it to be but it's should do the job just fine control
now we can modify the loop additionally and do some other things for this for
this new line two basic the gone or week or as I said we could at them down here
you always end though you always had where you have to do the least work
dispose it would look pretty yet these down below let's just go ahead and see
how it works out one more time
20 and a is less than be this indeed works out so enter the value again and
or one is fun to exit so let's see if this is true
10 for a 10 is less than 20 so is indeed be these conditional statements weird if
and else if plus the while loop now indeed follow in my letter to follow in
developed or will do the for loop before we do that let's go ahead and see how
the increments work so if we go ahead and take the in here
wile so while in my eye out for it
index that's that's the naming standard I could have if you can name your
variables pretty much whatever you want there are some naming rules but you can
use any combination of lower lower and uppercase letters to name your variables
some critics a while because I can declare a variable here as well I don't
declare variables at the beginning of the program I can declare them anywhere
I like the place of my choosing be careful because if you declare variables
like this
these variables will only be valid for the duration of the loop and within the
loop once the loop over the variable is gone as well
so while I is less than and I would like this wouldn't like a continuous
since I will just say I would like to repeat this pent-up if it's if the user
can figure it out if the user can find it useful in 10 times so well then just
Germany so less than 10 and at the end of the loop to see what I'm doing here
the condition is well the integer is less than 10 but can anybody see
anything wrong with this we haven't assigned a value too high and that can
be problematic why
well because i has some random value at the moment as some garbage value of the
property but proper explanation for it and that's garbage value could be pretty
much anything so let's go ahead and take this out of here as the same not good
for us and let's declare it outside of the loop this is just one way to do it
let's say that I is equal to zero and then type in here is less than 10 now
this has sense and at the end of the loop at the very end of the loop we can
type in plus plus plus plus now what does this mean I plus plus plus plus
just show you it will be the same as me typing in equals I plus one that's a
instead of this wall line +1 I can just type in my plus + and that food it to
read by what they could have also taped in I thought plus one but this is a
shorter version of this is pretty much what everybody uses so what's gonna
happen here is going to become bigger by one every time this
loop repeats in the beginning I will be 0 first best a second pass it will be
one third passport pass it always increased by one until this statement
about the false basically until when I reaches 10 when I become stand this
statement false because I will no longer be less than 10 equal sign here so this
will say it is I do you quote or less than and in that case the statement
become false when I would reach 11 so we can just leave it like this at a lower
number for the sake of this tutorial so I can just show you cycle and I we could
basically more if statements like and basically say okay print out and L
only if this is not the first repeat ins if it's the first repeat since don't
print and L and then we will and then we wouldn't have this gap here I would
encourage you to do that on your own
just to eliminate this by adding another if statement here by adding another
statement somewhere in this in this area