Nena001
Nena001

Reputation: 1

Understanding Do While Loop in ASP using VBscript

<%
option explicit 
dim n, sum
n = 1
do while (n <= 10)
sum = sum + (n * n)
n = n + 1
loop
response.write (sum)
%>

The output on this code is 385.

I understand that we gave n the value of 1,

then the do while states execute while n is less than or equal to 10

then sum has the value of sum plus (n times n)

n is then has a + operator of 1

loop this until n is no longer less than of equal to 10

then output the sum 385

I don't understand how this we get this output.

Upvotes: 0

Views: 2099

Answers (2)

Win
Win

Reputation: 62260

Here is how it is calculated..

enter image description here

Upvotes: 2

Razvan
Razvan

Reputation: 36

sum     n
0       1       n<=10 TRUE
1       2       n<=10 TRUE
5       3       n<=10 TRUE
14      4       n<=10 TRUE
30      5       n<=10 TRUE
55      6       n<=10 TRUE
91      7       n<=10 TRUE
140     8       n<=10 TRUE
204     9       n<=10 TRUE
285     10      n<=10 TRUE
385     11      n<=10 FALSE

Exits loop and print 385

Upvotes: 0

Related Questions