Post by Abhinash ( Bboy NeO ) on Mar 13, 2017 7:03:07 GMT
Lets start ...
Looping is essential for any language. It allows you to perform the same block of code over and over, by constructing conditions on which the code should be repeated.
The first and most widely used loop is called a "for loop". It takes an initial value, a condition upon which it should stop, and an incremental step. Then it executes code until the conditions are no longer true. This lets you repeat the same block of code any number of times.
A for loop has three parameters.
Before the first loop executes, it runs our initial condition.
Then it begins looping our code with these steps:
1. Checks if the condition is true. If so, continues. If not, stops.
2. Runs the code.
3. Runs the "increment" parameter.
4. The whole process again starts from step 1.
A simple example of a for loop -->
Explanation:
1. The first parameter, i=1, sets the i variable to one. This happens before the looping starts.
2. Next, the "increment" parameter is checked. This parameter is a post-increment operator, so 1 will be added to i after the entire code block is evaluated.
3. Then the condition is checked. Is i<=10? It is currently 1, so it is indeed less than or equal to 10.
4. Since the condition is true, sum+=i is executed. This means i is added into sum.
5. The code block has finished, and i++ increments i to 2.
6. Now it repeats.
7. Is i<=10? Yes, it is 2. Now sum+=i runs again, and now sum is equal to 3.
8. The code block has finished, and i now increments to 3.
9. This happens until the increment parameter sets i to 11. The condition is no longer true, and the for loop is finished.
10. The sum variable now holds the number 55, which is the sum of 1 through 10.
That's it guys.
I hope you understood.
+Karma for me please if you got helped from me.
Thanks .
Looping is essential for any language. It allows you to perform the same block of code over and over, by constructing conditions on which the code should be repeated.
The first and most widely used loop is called a "for loop". It takes an initial value, a condition upon which it should stop, and an incremental step. Then it executes code until the conditions are no longer true. This lets you repeat the same block of code any number of times.
A for loop has three parameters.
for (initial; condition; increment)
{
//your code here
}
Before the first loop executes, it runs our initial condition.
Then it begins looping our code with these steps:
1. Checks if the condition is true. If so, continues. If not, stops.
2. Runs the code.
3. Runs the "increment" parameter.
4. The whole process again starts from step 1.
A simple example of a for loop -->
new i
new sum
for (i=1; i<=10; i++)
{
sum += i
}
Explanation:
1. The first parameter, i=1, sets the i variable to one. This happens before the looping starts.
2. Next, the "increment" parameter is checked. This parameter is a post-increment operator, so 1 will be added to i after the entire code block is evaluated.
3. Then the condition is checked. Is i<=10? It is currently 1, so it is indeed less than or equal to 10.
4. Since the condition is true, sum+=i is executed. This means i is added into sum.
5. The code block has finished, and i++ increments i to 2.
6. Now it repeats.
7. Is i<=10? Yes, it is 2. Now sum+=i runs again, and now sum is equal to 3.
8. The code block has finished, and i now increments to 3.
9. This happens until the increment parameter sets i to 11. The condition is no longer true, and the for loop is finished.
10. The sum variable now holds the number 55, which is the sum of 1 through 10.
That's it guys.
I hope you understood.
+Karma for me please if you got helped from me.
Thanks .