For Loop
Introduction
You have learnt about looping in Javascript. Now, We will now use another Looping Syntax i.e. For Loop
Syntax :
Explanation :
- The initializing expression is the initial expression that declares any variable and executes it only once.
- The condition expression is evaluated.
- If the value of the conditional expression is true, the loop statements execute.
- If the value of the condition is false, the for loop terminates.
- the incrementExpression updates the value for Next Iteration.
Flow Chart
Example 1:
First Program is to Print "Hello World" 10 Times.
Here,
We have declared a variable i
Next, We have used a For statement with :
initialExpression as i=1
condition as i≤=10
and incrementExpression as i++ i.e i=i+1
This Will be Executed 10 Times.
Example 2:
We will make a program that gives the sum of first 10 natural numbers.
Explanation:
Example 3:
Now We Will Study About Nested Loop
Nested Loop Refers to the Loop inside a Loop, you can create any number of Loop inside Any Number of Loops.
Syntax
Example 4
Suppose You Want to Print Table of Number 1,2,3,4,5
What you gonna do create a loop from 1 to 5 for table of 1 and do same for 2,3,4 and 5
But don't you think it will take a lot of time and to write the code for all the tables.
So, Is there any shortcut Method to do so??? Yes!!! Here comes the use of Nested for loop.
Let's See How it Works.
Did you See How Easy It Was... That's The Power of Looping Code Short Result Long.
Let's Understand It.
Here First We have Declared 2 Variables tableNo and rowNo