Loops in Programming
For basic Programming Guide read this
Before properly starting the explanation of concept. This explanation is for people who don’t have any programming background. All new starters having difficulty in understand loop concept. You might have a confusion in mind like, what is a loop? let’s remove all your confusions.
The concept of loops is same in all programming languages
Loop is basically nothing but a programming concept in which you repeat a code with one repeating condition. That repeating condition stops the loop when it becomes false.
For beginners it is difficult to understand the above definition. Let’s take an example.
Suppose we have a line of code in C++ language.
This line of code do nothing special just print the word Topnewsage on screen. What we want to achieve is to write this word 500 times on screen.
What possible options do we have to perform this task?. One is to write this line 500 time one after another. It will work. But obviously it’s not a good solution. So using the concept of loop we can achieve our task just by adding a single line of code. Amazing isn’t it ..!
That’s where the magic of loop comes into action. Let’s solve our problem to write this 500 times.
Now, the above code prints word “TopNewsage” 500 times. Let’s explain the newly added magical line “for(i=1;i<=500;i++)“.
Exploration of ” for(i=1;i<=500;i++) “
Let’s first clear the doubts. ‘i’ is just an integer variable. The word ‘ for ‘ is Keyword. Keyword are fix(reserved) words used in programming to identify special things. The whole line ” for(i=1;i<=500;i++) ” is called as loop statement. And it is of ‘ for ‘ loop, So we call it ‘for loop statement’ to identify it . There are more types of loops other then just for. We will talk about them later as all are almost the same.
The for loop statement have 3 parts in it. (1) i=1; (2) i<=500; (3) i++ . Let’s explain these one by one
1) ‘ i=1; ‘ . This is the initial value we have provided to our for loop statement to start from.
2) ‘ i<=500 ‘ . This is an condition for our for loop statement . We always provide a True condition here to start the loop.
3) ‘ i++ ‘ . This is an programmatic/ Direct way to write ” i = i +1” means ‘ add one in variable i ‘.
How these 3 things work together
You give initial value then provide an condition and an incremental value. Incremental value to make middle condition false to stop the loop. That’s easy right 🙂 .
Loop statement first check initial value in our case it’s ” i=1 “. So loop statement understands that i is 1. Then it goes and check the condition which is i<=500. So it’s a true condition as we have provided initial value as 1 to i. Now it will repeat everything written below (in our case it’s just one line of code cout<<“TopNewsage”; ). It can have multiple lines of code here and this loop statement will execute all one after another. After performing this task it goes to incremental statement ‘ i++ ‘ which will increase the value of i from 1 –> 2. Now again it will checks for middle condition. And it is still true as i=2 is less then or equals to 500 (i<=500). Again it will repeat all the statements below it and goes to incremental and checks the condition. When the condition becomes false. It will stop the loop.
You see that it’s looping /repeating that’s why it is called as loop statement.
It will never go to initial value to reverify it. Just repeat between condition and incremental value to repeat the code below for loop statement.
Types of loops
- Do while
All types work on same principle just have bit different style of doing it. You can easily explore these after you have grip on for loop.
Task for you to try
Print an mathematical table of 2 on computer screen using the for loop statement .
The output should look like
2 x 1 = 2
2 x 2 = 4
Submit your results here below in comment sections. Like share and subscribe. If you still have any problem understanding the concept feel free to ask 🙂