this conference will now be recorded now in the last class we have seen about uh switch statement in web so today we'll go with the remaining part like we'll go with the Dual Loop and for Loop so coming to do value Loop the syntax of double Loop is index of and developers we use the keyword to and we go with the set of statements next statement one statement to like that it went in which the condition at the last year while condition well condition so in the case of fry Loop the condition is tickled before the statements in the case of durable Loop the condition is checked after the statements so what is the difference in case of while loop the condition is checked before the statements whereas in case of do value the condition is ticket after the statements even we'll discuss today in what situation we use develop over volume that example also we'll discuss today the it as a simple example we want to print the numbers from 1 to 10. but to print the numbers from 1 to 10 we initially take a variable I equal to 1 and we start here we can do and we'll print the value of I and to go to the next number I plus stress then after the loop we'll take care y i less than or equal to so same like while loop only but the main difference is we check condition after the statements now let us understand with an example code in what situation we prefer to use the value power while loop so for that let us go with an program here write a program to accept a current balance and display the following options means menu like one deposit whenever we choose option one deposit we should ask for the amount to deposit and that amount should be added to the current balance to with the drop whenever we choose the option beta we should ask for the amount to withdraw and that amount should be subtracted from current balance three display balance should display the current balance fourth exit means to come out of application means the point is the menu should display the menu should display repeatedly typically until the user chooses Etc so in which this option exit should come out of application since we want to repeat the menu we have to keep the menu inside the loop whether it is file to part Dubai we will discuss in the book let us understand in the program let me copy the name as bank. java right now let us understand this with the program let me open Eclipse then choose the workspace so from tomorrow's class before we start the class I'll open this Eclipse before the class only right click enter closer now whenever we want to create a new program we are direct click on project and say new class go with the class name like we are going with the bank with the main method inside no import scanner class cover Dot clicking.
scanner and create object of scanner class and at is equal to new scanner like the keyboard go with the variables like balance amount which you want to deposit are better and user choice now the first step is we need to accept the current dependence so go with the message enter current balance then we'll say balance equal to s dot next link after accepting balance we want to display the menu to the user 200 display what menu to the user and that means we want to display repeatedly so we have to keep it inside the loop so let me consider this curly brace as Loop now so whether to use while loop or do well we'll discuss now insert the loop display the menu one means deposit so with the draw three display bar links for exit now ask the user to enter the choice interior choice we'll say here CH equal to s dot next state Now understand carefully so when the choice value what you enter if it is not equal to 4 then we have to repeat the menu we have to repeat the minivan Choice Big Value becomes 4 we should come out so how the condition should be if we use while loop how the condition should be while CH not equal to 4 when the value of CH what you are accepting if it is not equal to 4 then I should repeat but when we use while loop C what is happening we are getting errors here because we are accepting this CH value inside the loop statements not outside of loop so when we come to this value for the first time since there is no value to CH it's going with what error because we are accepting this condition value inside the loop statements so when you are accepting the condition value inside the loop statements in that situations we prefer to use what to do value when to think of develop when the value of condition since our conditional when the value of condition is accepted inside the loop statements in that situation it is suggested to go with develop when to think of develop uh can you please repeat once again I didn't get yeah see here if I use while loop when we go with the while loop what is the conditioner clear with the condition first of all when choice not equal to 4 we should repeat the loop Clear With It yes well right exit exit when the choice is exit we have to come out wins when the value of condition CH not equal to Fourth we have to repeat the mean clear with this condition yes yes that's right then if I use while loop are you getting error is right because did we accept CH value inside the loop statements or outside Loop statements in this program where you are accepting CH value inside loop or outside lip where you are accepting CH by inside the loop right inside the loop statement so when you are accepting the value of condition inside the loop statements so when I come to this value for the first attempt there is no value to CH here that's the reason we are getting error so when you are accepting this condition value inside the loop statements in this situation in this situation which Loop we have to use do while loop clear Swati yes I'm clear no as usual could so go with switch of ch the value of CH is one means deposit so when we choose the option deposit what we should ask enter amount to deposit then we'll say amount equal to s dot next what is the formula for deposit balance equal to balance Place amount then come out of it using break S2 what is case to withdraw so go with the message enter amount to withdraw Central amount to withdrome then we'll say amount equal to a starting extent can we withdraw the amount more than balance no so we have to check it here if the amount what you have entered if it is greater than balance amount what you enter if it is critical balance then we have to display the message to the user in sufficient balance encryption balance else means if the amount is less than balanced the formula is balance equal to balance minus amount okay three what is case three display balance simply go with the message current balance equal to the value of the balance and like S4 what is case for exit so we'll display the message thank you for banking default if the user enters any other option it goes to default where we will go with the message invalid choice and the code so when you run the code it will ask for current balance so I'm going with the 12000 now the menu will display here one means in deposit to make it a three display balance for exit if I choose the option 1 it will ask for the amount to be deposit we'll say eight thousand again the menu will come now if I choose option 3 display balance what is the balance now twenty thousand that is twelve thousand plus eight thousand twenty thousand now let us go with option two two minutes with the draw we want to withdraw 25 000 is it possible no because the previous balance is twenty thousand now we want to withdraw 25 it is not possible so goes with the message in sufficient balance now let us go with option two enter amount to withdraw P1 to withdraw 15 000 now if you take for balance twenty thousand minus fifteen thousand now the balance is by closing if I choose any option like 8 we'll go with message invalid choice and when I choose option 4 then only it comes out of application by going with message thank you for banking right so what is the point here when to think of developments if the value of condition the value of condition is accepted in the loop statements and it is suggested to use while loop winner we are accepting this condition value this is our condition where you are accepting this condition value inside the loop statements in that situations it is preferred to use domain Loop else we can go with value part 2 well no problem clear everyone any questions now let us move to the next one further a syntax of for Loop is and we go with initialization semicolon condition semicolon expression opening plus curly braces then we go with the set of statements like statement one segment group Electric statement example and same principle here no curly braces when it considers only one statement even for Value Park developers the same rule if no curly braces then it considers only one statement even for while loop also like example we want to print the numbers from 1 to 10. then how we study it first int I equal to 1 this is called as initialization equal to one is initialization semicolon then since we want to print up to 10 so we go with the condition I less than or equal to 10 semicolon I plus plus this is our expression and here we'll print the value of Phi is going to the value of I let me explain how the flow goes here so initially initially I equal to 1 that is called as what initialization so first it goes to I equal to 1 after initialization the control will go to condition after initialization the control will go to condition one less than or equal to 10 s then it comes to statements since there is only one statement using curly braces optional so which when you say sopi which is going to print about after statements control will go to expression I plus plus I will become node 2. then again comes and checks the condition so 2 less than or equal to 10 s then sopi it's going to print to centralization is executed only one time after that the flow will be condition statements expression condition statements expression like that it continues then now it has printed to again expression I plus plus three three less than or equal to 10 s then it's going to print 3 like that 9.
9 plus plus 10 10 less than or equal to 10 s then it's going to print 10. again A plus plus 11 11 less than or equal to 10 false comes out of loop so when I value becomes more than 10 when a value becomes more than 10 means 11 so when a value becomes level condition is false comes out of Rupiah let me take one more example take care for lip like this for INT I equal to 1 I less than or equal to 10 I plus plus and we are ending with semicolon now we'll print the value of I what is the output of this program here object we have ended only with semicolon then what is the output of this program yes n even what is output of this piece of code error error it goes with error first line or second line error in first line or second line second line second line very good the error will go in second line many people will say first link but not first line Second Lane so when you inhibit semicolon fertile for Loop no problem to for Loop no mistake to for Loop so why we get error in second line here is see where we have declared variable I inside for Loop how to know declaration when you say data type and variable that is called as declaration so we are declaring I variable inside for Loop and when we end with semicolonier only this line is for Loop this line does not belong to for Loop but we are using I variable which is not declared because I is declaring for means I can use only within fur if I am trying to use I variable outside of for ghost bit error and the message is cannot find a symbol variable I know let us declare I variable out signal I am declaring available outside of for Loop and will write now like this for I equal to 1 I less than or equal to 10 I plus plus ending with semicolon now if you print the value of I then what is the output we get here now here no error it won't go with error because we have declared available outside means I can use infer Loop and even outside of for Loop then what do you think the value of I it's going to print here that's the value of I it is going to display so the output is part again 11. why because this line of code will get executed only when the condition is false so when the condition becomes false whenever it becomes more than 10.
so what is more than 10 11 so 11 less than or equal to 10 false comes out and prints the value of I level let me explain the flow first I is 1 1 less than or equal to 10 s since there are no statements it will go to I plus plus I will become not two two less than or equal to 10 s again A plus plus 3 like that it continues so when I value becomes 11 11 less than or equal to 10 false comes out and prints the value of I which is 11. okay so same questions I'm going to ask you in tomorrow's class also please recap this now let us go with some example programs write a program to write a program to find the factorial of a given number we want to find what factorial of a given number so if you take the number as 5 or if you take 5 factorial what it means Pi into 4 into 3 into 2 into 1 so multiplying from given number to 1 or 1 to given number will give the factorial here we are doing multiplication operation so take a variable F initialize the value to 1 year when we go with addition we have to take a variable initialize the value to 0 but when we go for multiplication we have to initialize the value to 1 because if you go with 0 something multiplied by 0 is 0 only so for multiplication we have to go with y let's go with the code at the real dot Java right click on Project ends in new class here with method import scanner create objective scanner connect the keyboard it provide the message entry number will say int n equal to s dot next the number what we accept for that we want to find factorial so today's take a variable is equal to 1 then take a for Loop which starts from given number I equal to N means it starts from given number until it reaches 1. then if you take the number as 5 5 to 1 so 5 to 1 is what decrement so we are see here I minus minus and the formula is simple F equal to F into I once the for Loop is completed we'll print the value of a square Factor there let me explain how the flow goes it will ask here and drain embed will say means ns5 and instant value of f is one here that's here I equal to N means I equal to 5 5 greater than or equal to 1 s then comes to the statement F equal to F into I so the value of f in cell even I is 5 the value of f will become now for after statement control will go to I minus minus I will become not 4.
4 greater than or equal to 1 is again F equal to F into I f is partner of Phi I value is now 4 the value of f will become 20. again I minus minus three three greater than or equal to 1 s again F equal to F into I so if value is 20 I is no 3 the value of f will become 60. again M minus minus 2 2 greater than or equal to 1 s again if equal to F into I which is 16 to 2 100 B again A minus minus 1 1 greater than or equal to 1 s again F equal to F into I is 120 into 1 120 again I minus minus zero zero greater than or equal to when false comes out and prints the factorial F value which is 120 irrelevant the flow or sequences when you are having sequences we make use of for Loop so that when we run if we go with the number 5 which is going to print Factor L as 120 wait any questions no let us write the same code in a method so what you are doing here you are accepting input and doing process and displaying output in same main method so main is doing multiple tasks so we can divide the work here so we have to take this logic and put it inside a method so when we keep this logic inside a method the code will become reusable so how to go with methods is very simple how to go with methods is just to take the logic out is to take the logic Out Create a method here like this fact and put the logic inside now to this fact method what are the number we have accepted we have to pass it here like this you can say fact of passing C product of n so if n is 5 that 5 will come and store into the argument here whatever the N value we are passing that will come and store into this argument here we'll find the factorial of this number after finding factorial we return the result back here so written F so what is use of written keyword once do the calculation is complete the result value will Gap will give it back to this method how to give the result value back to the method using written keypad that now what is the written type of this method now what is the written type of factor method hint very good the written type is what integer f is what type of variable integer so before the method name we have to mentioned the written type so what are the value you are writtening that will come and store into this method so you can store it into some variable and you can print this this is the way how we start going with the methods then now we are getting error here the reason is since many static since many static a static method in a static method if you call any other methods directly the other method also should be what static only it's a rule I'll discuss again later the point what you have to remember is static method and axis an axis only other Target members directly when we come to object oriented I'll discuss this one once again properly a little bit the number as six it's going to print Factor less 720.
okay right so try to write the programs as it means whatever I am giving try to write that course using Matrix however I'll be taking more number of examples in future also how to go with methods let us go with one more example create a program to display multiplication table for a given number means if we take the number as 5 we want to display the output like this by into 1 equal to Phi 5 into 2 equal to 10 like that 5 into 9 equal to 45 M into 10 equal to 50. in this what is fine you are given number what is 5 where you are given number and this one newton it's a sequence so we can manage with the for loop with variable I then despite 10 45 15 this is your product so what's the formula for product n into I will give the product let us write this code I click on Project NC new Class M table method import scanner create object of scanner class electric keyboard except the number what are the number we have accepted for this number we want to print the table so it's very simple take a far Loop which starts from 1. until it reaches 10 then I press Plus Now find the product or to find product and into I will give product now while displaying C Hotel print first we have to print n value then the start symbol as it is in double quotes then I value then equal to symbol as it is in double quotes and payment we need to ask here entry number will say fine means in this part comes to for loop I equal to 1 1 less than or equal to 10 s then comes to the statement P equal to 5 into n so what is the value of P Now by Phi then how we are printing C first in value which is 5 okay start symbol we kept in double quote so it is going to print as this then the value of I variable which is 1 okay equal to symbol in double quotes which will print as it is when P value which is okay now the control goes to I plus plus I will become node 2.
2 less than or equal to 10 s comes to statement P equal to 5 into 2. so what is the value of P now 10. so it prints n value which is 5.
start symbol as it is in double quotes then I value 2 equal to symbol as it is then P value continuous derivative when we run click the number five it's going to print with the table if we run the code again if we set to win it's going to print 12th table here any questions let us move to the next one break and continue breakfast use it to come out of Loop continue is used to continue the loop Breakers use it forward to come out of flow continues to continue the loop like examples to write a while condition then we go with some statements and we'll say break and some statements so whenever the control C is break it comes out of loop the control will come out of and execute the statements of total now if we write like this well condition and we go with the statements and we'll say continue and some statements whenever the control C is continue the control will jump back to condition from here the country will go back to condition so whenever the control C spray comes out of loop continue will go back to condition let us understand using the programs write a program to accept the members from user until that user index 0. and find the sum of these name bits what is a task here will be keep on accepting the numbers from user and we want to find the sum of those numbers to terminate to terminate the accepting the user has to enter 0 when user enters 0 then it stops accepting the number from reset break demo right click on Project ends in New class break name Limited be it import scanner create objective scanner connect to keyboard declare the variables like the number and some Mass initially now take a while loop like this while true well true this we call it as infinite Loop infinite Loop means the condition never becomes false when the condition never becomes false we call infinite Loop so when we go with infinite Loop there should be some way to come out that some way to come out is break here now in the loop go with the message entry number to stop enter 0 to stop in death zero we'll study it into invariable n equal to starting equipment if the number what the user has sent it if it is zero then we want to terminate the loop so how to terminate Loop using directly it is not zero added to sum here remember what you take you added to that's it once we come out of loop print the value of sine here let me explain so initial value of sum is point zero well true means the condition is true it lasts for a number we'll say the number as 5. it's 5 equal to 0 no when it comes to this statement sum equal to 0 plus 5 which is 5.
now the control goes to loop again true again it will ask for the number we'll see now 8. eight equal to 0 no then that 8 will get added to the previous sum Phi now the sum is 13. again the loop repeats again it will ask for remember Wilson of 6.
6 equal to 0 no then sum equal to some plus n so it will take the new number 6 adds to the previous sum 13 now the latest term is 19. again the loop repeats it will ask for a number to stop enter 0 this term will say 0. comes out of loop prints the value present in sum which is 19 foreign yeah and we can type eight six now this time will say 0 which terminates the loop and prints the same as what 19.
so whenever you want to come out of loop we have to make use of what given break keyboard clear available any questions right here while true is called as infinite while loop right do if you write while true this is infinite do while loop it's simple for semicolon semicolon in simple calibrations just semicolon semicolon this we call infinite Guadalupe right follow how to go with infinite for Loop we are too simple to semicolonics let us go with one more example code write a program to display the following output like this 1 2 3 4 6 7 8 9 10. so we want to print the numbers from 1 to 10 and one number is missing what is that five is missing so we want to print from 1 to 10 except 5. you see the code continue them right click on Project NC new class continue and method where we will take the variable I equal to 1 then save y less than or equal to 10.
we have to figure that I is 5 if I is 5 because we don't want to print 5 if I is 5 then say I plus plus and go back to condition again go back to conditioner to go back to condition continue if I is not right if I is not Phi printed the value of I here into the value of I go with a separatory space then I plus plus that's it here how it comes first I is what one 1 less than or equal to 10 s then one equal to 5 no then it comes here and prints the value of I which is 1. and I plus plus I will become not two less than or equal to 10 S 2 equal to 5 no then comes comes 0 and prints I value which is 2. till again I plus plus 3 3 4 after 4 I plus plus 5 5 less than or equal to 10 S 5 equal to 5 now the condition is true now it is true I plus plus it will become 6 continue means goes back to conditional so what's the value of sine of six six less than or equal to 10 s 6 equal to 5 no then it's going to do the remaining print operation so whenever we want to come out of loop make use of break keyboard whenever we want to go back to condition make use of continue cuboid got it everyone any questions let me go with the assignment so with this we complete our basics part and go with assignment if you do this assignment you are good with Logics also write a program to display the following output draw the line and see here number factorial draw the line and that number we should write 1 factorial of 1 is 1.
2 factors of 3 is 2 3 capital of three is six for 24. 5 120 after this after displaying factorial between one to five then again we should draw the line and say here come equal to sum of all these factorials click so just we have to find the sum of factors from one to five but you should get the output in this format write a program to display multiplication tables from 1 to 10. we want to display table one table two like we have to send the table write a program to check whether a given number is prime or not what are prime numbers the numbers which are divisible by one end itself like example 2 3 5 7 11 13 meters the numbers which are divisible by one end itself they are prime numbers create a program to find sum of prime numbers from 2 to 100.