While Loop
Introduction
While Loop is used to execute task multiple times until the specified condition is true
Flow Chart
Syntax:
Explanation :
In place of Condition, you will write the condition until the task should be executed
Now, Lets see some Examples :
Example
Print "Hello World" 20 times.
Here,
We have declared a variable i=1
Next, We have used a while statement with a condition i<=10 Thatmeans, until and unless the value of i becomes 10 the console.log("Hello world"); and i++ will be executed.
We have used i++ to increment the value of i by 1 i.e. i=i+1
Example
We will make a program that gives sum of first 10 natural numbers.
Here,
We have declared variables sum=0 , number=0 and i=1 then we have used while loop to iterate i until its values becomes equal to numbers Every time value of i will Add to the Value of sum
Then, Value of sum is printed to the Console Tab.
Explanation: