हिंदी

Explain for loop with example

Advertisements

प्रश्न

Explain for loop with example.

दीर्घउत्तर
Advertisements

उत्तर

The for loop is a very rigid structure that loops for a pre-set number of times. In JavaScript for structure is very flexible, which makes this type is very useful.

for (initialization; condition; increment/decrement)
{
Body of the loop;
}

The for structure within parenthesis there are three parts each separated by a semicolon. They are,

  1. The first part of the loop initialize a variable which is also called a control variable. In most case, the control variable is declared as well as initialized.
  2. The second part is the conditional statement that determines how many times the loop will be iterated.
  3. The third and final part determines how the value of control variable is changed (Incremented/Decremented).

Example:
HTML CODE

<Html>
<Head>
<Title> Program - To test for statement in JavaScript </Title>
</Head>
<Body>
     <script language="javascript" type="text/javascript">
     var no1 = prompt("Please enter Table You want :", "0");
     document.write("<h2> Multiplication for your need </h2>");
     for( var no2=0;no2<=10;no2++)
     {
    document.write(no1+" x "+no2+" = "+no1*no2+"<br>");
     }
   </script>
</Body>
</Html>

OUTPUT:

shaalaa.com
Looping / Repetitive
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 15: Control Structure in Javascript - Evaluation [पृष्ठ ३३०]
Advertisements
Share
Notifications



      Forgot password?
Use app×