‫Welcome back.
‫In this video, we'll introduce the loops in Java.
‫The while loop is a type of loop that allows you to repeat a block of code As long as a specific condition
‫is true, the loop will keep executing the code until the condition becomes false.
‫It's a powerful tool for repetitive tasks or for performing actions while a certain condition remains
‫valid.
‫The basic syntax of a while loop is as follows.
‫We start with the while keyword and here we put the condition and two curly braces.
‫The condition will be tested.
‫If it is true, then the while loop will keep executing this block of code.
‫I want from you to write these notes on your copy book and your notebook in order to refer to them later
‫on when you need to review anything related to while loop or conditions or the basic syntax of Java.
‫Let's create a simple example of using while loop to print numbers from 1 to 5.
‫I'll start with the end.
‫Number equal to one.
‫This is a variable called number of type and a value of one.
‫While I'll replace this condition.
‫For number checking for a number.
‫If it is less than or equal to five, I need to print out System.out.println the number.
‫I need to print the number.
‫But wait.
‫And here I need to increase the number by one.
‫This is a post increment.
‫I can put like this number number equal to number plus one.
‫Or I can make a shortcut for this and put plus plus.
‫So this will increase the number by one every time the loop will be executed.
‫So in this example, the while loop starts with the condition number is less than five.
‫Yes, it's one one less than five.
‫As long as the number is less than or equal to five, the loop will continue to execute.
‫Inside the loop.
‫We print the value of number and then we increment it by one with a number plus plus.
‫This process repeat until the number becomes six.
‫So it becomes two becomes three four.
‫The condition is true.
‫So go and execute the loop again.
‫Five five is less than or equal.
‫Yes, it's equal to five.
‫Then go and print again.
‫Five and increase number by one number becomes six.
‫Six is less than or equal to five.
‫No, this is false.
‫So it will skip.
‫It will break.
‫It will exit the loop.
‫Let's try to.
‫Run the program and see the result.
‫And here.
‫You can see one, two, three, four and five.
‫Every time we print the number and increase it by one, every iteration in this loop will increase the
‫number and the print.
‫It's crucial to ensure that the loop condition can eventually becomes false.
‫Otherwise, you may end up with an infinite loop which continuously runs and never stop.
‫Let me show you.
‫If we remove this increment condition or increasing the condition number is less than five, it will
‫execute the number, it will print it and we'll go up.
‫Check the number.
‫Yes.
‫Number becomes one again.
‫Number will will still equal to one.
‫So this condition is always true.
‫So this loop will generate infinite bit loop infinite execution of this condition.
‫And this is eventually harmful and never stops.
‫Let's try to run our program and generate an infinite loop.
‫You can see now that we're generating an infinite number of ones and you can see the program is still
‫printing number one because there is no limit to stop this loop.
‫So please pay attention because it's very crucial and very serious problem.