COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
Master of Business Administration (90 ECTS)Master of Business Administration (60 ECTS)Master in Computer Science (120 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMaster of Science in AccountancyMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business AdministrationMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science MBA Business Technologies MBA Leading Business Transformation Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMaster in Logistics and Supply Chain ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMS in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMBA in Strategic Data Driven ManagementMaster of Business AdministrationMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSBachelors in International ManagementMS Computer Science with Artificial Intelligence and Machine Learning Concentration
For College Students

Java While Loop: Examples

$$/$$

Let us now take a look at another example where you can use a while loop.

 

You can download the file below which will help you through the following video.

$$/$$

Video Transcript

 

Here I am trying to declare a variable int NUM starting from one and I try to print all the numbers from one to 100. So I say I first declare an integer NUM outside of this while loop. I say int NUM is equal to one. Then I say that while NUM is less than equal to 10. Notice that this is less than equal to and not less than. Why is that? Because I need to print all the numbers from one to 10 including one and 100 both. Therefore, my while loop will get executed till NUM reaches 100. Therefore I write While NUM is less than equal to 100. Then I print out the value of NUM and in the next step I increment my NUM variable. Remember that this is another way of updating my NUM variable. If instead of this I would have written something like this NUM is equal to NUM plus one, then it would also have been the same statement. So basically saying that this statement is equivalent, this statement is equivalent to this statement. This is just an alternative way of updating the NUM value by one. So let me delete this statement. Let us recheck our program. We define a variable NUM outside which starts from one and goes till 100 including 100. And I print out the value of NUM. And in the next step I update my NUM also. So let us try to run this program and see what it prints on the console. See on the console, beginning from one, two, three, all the numbers between one to 10 are printed including the last number which is 100. Let us now see what would have happened if I would have forgotten to add my equal to sign here. So I remove the equal to sign and then I run this so you can see that all the numbers starting from one, two, three get printed till number 99. And why does 100 not get printed? Well, this happens because when NUM is equal to 99, this while condition holds true. Since 99 is less than 100, then 99 gets printed on the console. And in the next step, NUM value reaches 100. As soon as NUM value reaches 100, then when the next time while loop is executed, it finds that since 100 is not less than 100, therefore this while condition no longer holds true and this while loop is broken. That is, we come out of the while loop.

 

Now, there are few things which you need to be careful while writing any while loop. One common mistake is to be able to forget to initialize this particular variable. You must always remember to initialize whichever variable you are declaring outside the while loop. Similarly, you should make sure that the while loop terminates. That is, the while loop runs only a finite number of times. So you have to be very careful while writing the condition which is there inside the while bracket. And lastly, in order to avoid infinite loops, you must never forget to update the value of your changing variable in this while loop. My NUM variable is the changing variable. So I must remember always to update my value of NUM. Suppose if I had forgotten to update the value of NUM, I remove the statement here. Then you would see that the program runs into an infinite loop. See, one is getting printed infinite number of times. You can see the moving scroll bar here. So basically, one is getting printed a large number of times. This while loop is not getting terminated. It is because I forgot to update my value of NUM. Therefore, NUM remains stuck only at one. This condition always holds true, and it keeps on printing one an infinite number of times. Let me stop this program by clicking on the stop icon here. Yeah. So basically, you need to always update whichever variable you're changing inside the while loop.

 

Video Recap

 

Learn how to use while loops in C++ programming with this tutorial. Declare a variable int NUM and print all numbers from 1 to 100 using a while loop. Initialize the variable outside of the loop and update it inside. Be careful to initialize the variable, write a condition for the loop, and make sure it terminates to avoid infinite loops. The segment shows what happens when you forget to update the variable, and provides tips to avoid common mistakes.

 

You learned how to print numbers in a sequence in a while loop. There is a simpler method to accomplish this via something called the “for” loop. You will learn more about the for loop in the upcoming segment.

 

Let us now take a look at another case where while loop will be very handy in reducing the amount of redundant code we would need to write.

 

Please download the file below which will help you understand the upcoming video better.

 

$$/$$

Note: The program shown to calculate factorial considers only numbers from 1 to 10. i.e. it is calculating the value of "10!".

You have now learned how a while loop works. A set of statements is executed until a condition is met. The condition can be such that the loop is executed a specific number of times and a counter is incremented at every execution.

Let us take a look at a case we would wish to avoid, an infinite while loop.

$$/$$