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
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 AnalyticsMS 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 Administration | STEMMS 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 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 MediaMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMaster 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 ManagementMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc in Sustainable Global Supply Chain ManagementMSc 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 AdministrationMaster of Business AdministrationMSc in International FinanceMSc in International Management and Global LeadershipMaster of Business AdministrationBachelor of BusinessMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 90 ECTSBachelor of Business AnalyticsBachelor of Information TechnologyMaster of Business AdministrationMBA Business AnalyticsMSc in Marketing Analytics and Data IntelligenceMS Biotechnology Management and EntrepreneurshipMSc in Luxury and Fashion ManagementMaster of Business Administration (90 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS) MSc in International Corporate Finance MSc in Sustainable Luxury and Creative IndustriesMSc Digital Marketing
For College Students

For vs While Loop Java : Know the Difference

$$/$$

Let us now take a look at how the same problems can be solved via both, for and while loop.           

$$/$$

Video Transcript

 

Now we are going to take a look at some cases in which you can use the while loop for solving these problems, but you cannot use the for loop for these. Now, consider a situation in which you have a bucket and you have another container containing some water. You have a mug and you are supposed to take in some water from the container and pour it inside the bucket. So is there a way to solve it using the for loop? Well, if you recall, the for loop for loop is always of this type. For some variable is initialized here and then you take the variable till the final point. That is, you define the ending condition for that variable and you increment that variable. So this was the general template for writing a for loop. Now, since you don't know what is the size of the bucket or the size of the mug, do you know how many times you have to take in water from the mug and pour it inside the bucket? Well, if you don't know their volumes, then there is no way to determine in how many steps will the bucket be full. This clearly means that even though you might know the start point of this particular problem, but you don't know how many times you actually need to repeat this process. So in cases such as these, in which you don't actually know beforehand how many times you need to repeat some procedure, it is best to use the while loop for the same bucket. Example, we can initialize A where we can take in a variable called is full. Now, this is a Boolean variable called is full, which denotes whether the bucket is full or no. In the beginning the bucket is empty. So in the beginning my is full variable will be false. 

 

Now, I can say that while is full is equal to is equal to false, that is, my bucket is not full yet. Then during that, keep on pouring more water. So this process is going to repeat till my bucket becomes full and my is full variable becomes true. When the is full variable becomes true, this while loop will automatically stop getting executed and we would have reached a stage where our bucket will be full. So in this case, we used a while loop because we didn't know how many times do I need to take in water from the mug and pour it inside the bucket. So basically, the underlying idea here is that the while loop can be used to repeat a certain procedure using some conditions where you have to evaluate whether some condition is true or false, then you can use the while loop there. However, in those cases, for loop cannot be used because there is no way to check for any boolean values in for loops. In for loops are useful when you have to perform some mathematical operations. For example, you know that your numbers are going to vary from one till 10 or you have to find the sum of all numbers from one to 1000. So you know that this process is going to repeat some finite number of times. So at each step you can tell the for loop where to begin, where to end and what to increment. However, in these cases, such as the booleans, you never know beforehand how many times the procedure is going to repeat. Similarly, let us take in another example here. Suppose I ask the user to input two numbers x and y. Now I say that while the user keeps on inputting x and y and while x is greater than y, then keep on printing that x is greater than y. So what happens is that suppose in the first iteration user inputs four, comma five, then this statement is going to get printed or no will depend on this while condition. So it will check whether four is greater than five. Since this is not true, it will not be printed. Then the next time user inputs two numbers, suppose the user inputs seven and two. Then in that case while x greater than y will be true. And so this statement is again going to get printed. So the computer keeps on taking in put from user input of two numbers and keeps on printing whether x is greater than y. So even this case comes under how we can solve this problem using the while loop. 

 

Video Recap

 

  • The segment discusses the difference between using while loop and for loop in certain problem-solving situations.

  • For loops are useful in cases where a finite number of repetitions are known, such as mathematical operations or when numbers are within a specified range.

  • While loops are useful in situations where the number of repetitions is unknown, such as when dealing with Boolean conditions.

  • While loops are used to repeat a certain procedure until a specific condition is met.

  • An example of using a while loop is when pouring water from a mug into a bucket until the bucket is full.

  • Another example is when taking user input of two numbers and continuously printing if x is greater than y while x is greater than y.

 

Let us now take a look at cases where only a for or a while loop can be used.

$$/$$

In the next segment, you will learn how to code for loops in Java.