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 ConcentrationMaster of Business Administration
For College Students

The For Loop in Java

$$/$$

Let us now look at another kind of loop, the for loop.

$$/$$

Video Transcript

 

Till now you have learnt how to use the while loop to solve the robot and bricks problem. Now we are going to take a look at this problem wherein you have to print the list of all the even numbers between one to 10. So for that you can use another kind of loop in Java, which we call as the for loop. So basically the for loop in Java is useful in those cases in which you need to repeat a certain action. Now here again, you have to tell the computer to check for all numbers between one to 100. And in case a number is even, then you need to print it. So basically, in order to write a for loop for this, you can say that for this is the keyword for used in Java, you say for. Then you need to tell the for loop where you would be beginning executing this for loop. From what point? So in the beginning you need to start from this one. This one will go on from one, two, three till 100. So in the beginning I have to initialize a variable starting from one. Let me call it my variable. Int I is equal to one. This denotes where my starting variable is going to begin at at the beginning of this for loop, then I put a semicolon and then I have to tell this for loop till what point do I need to keep on executing this for loop? So I say that for int I equal to one and semicolon I less than equal to 100. That is, till the time I does not become 100. Till that time, keep on incrementing I by one. So notice carefully the condition within this curved braces. What happens is that in the beginning you are specifying the point where to begin. You specify that please begin from int I equals to one and then keep on repeating this loop till you reach a point where I becomes 100. And after each iteration or after each step keep increasing I by one. This increment can be denoted by I plus plus. Now, what will happen inside each iteration that you have to specify within curved braces? So I open a braces here and then I say that if I need to specify that if my number is even then printed. So I say that if I percent two is equal to equal to zero, that is the remainder. When I divide I by two is zero, then this means that it is an even number and I print i. This basically completes my if condition. Now, do you think I need to increment my I variable now in order to move on to the next step? Well, no. This for loop basically ensures that I gets incremented automatically on its own after this step is over. Because I have specified here I plus plus explicitly I don't need to increment my I from one to two. So now this for loop is complete so let us run through it and see. In the beginning my I will be one and I is less than equal to 10 is also true so then what will happen? It will check that if I person two is equal to is equal to zero so basically is one person two is it equal to zero? Well no, on dividing one by two, the remainder is not zero so then one will not be printed and my I will now move on from one it goes till two, it gets incremented to two. Then in the next iteration again it checks that if two person two is equal to is equal to zero, that is if two is divisible by two since it is true it prints two on the console so basically if this is my console, two will get printed here. Now again this I plus plus ensures that in the next step two increments from two to three. Then again it goes inside this for loop and checks this if condition it checks if three is divisible by two. Now since the remainder when three is divided by two is one which is not equal to zero so three is not printed here again in the next step three gets incremented till four. This if condition will be checked for the remainder when four is divided by two, it is indeed zero because four is divisible by two. So four will be printed here on the console. So in this way this process keeps on getting repeated till I reach my ending state and what will be my ending state? My ending state will be when my I becomes equal to 100. So basically this condition holds true. So then what happens is that if 100 is divisible by two, then print 100. So basically in the last step 100 is going to get printed here on the console. Now in the next step when this I plus plus happens that is 100 gets incremented 201. So basically the condition enclosed within these curved braces of the for loop no longer holds true because I is no longer less than equal to 100. So this entire piece of code no longer gets executed and the while loop terminates. So in this way you have seen that you first need to initialize your starting point. You need to specify the ending point where you want the for loop to end. After that you need to specify what will happen after each iteration. So I must increase from one to the next value. So we say I plus plus and then inside you can do whatever you want, that is you can check. In this case we wanted to print even numbers so we say we check for each and every number whether it is even and if it is, then we print it. So this is how you can use the for loop to print the list of all the even numbers between one to 10.

 

Video Recap

 

  • Learn to print a list of even numbers between 1 to 10 using Java's for loop.

  • Initialize a variable to start the loop and specify the ending point.

  • Use the "if" condition to check if a number is even or not.

  • Increment the variable using "i++" after each iteration.

  • The for loop automatically increments the variable, no need to increment it manually.

  • The loop terminates when the condition is no longer true.

 

You saw how similar a for loop is to a while loop. You will be using this loop quite often from now on. Let us now try to solve the same problem using a while loop.

$$/$$

Now you know about two types of loops in Java. In the next segment, you will look at when should you use each of these loops and how they can be used interchangeably in some cases.