Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Other Programming (https://www.askmehelpdesk.com/forumdisplay.php?f=437)
-   -   What is the value of iSum after running the following code? (https://www.askmehelpdesk.com/showthread.php?t=424481)

  • Dec 12, 2009, 02:39 PM
    kourtnee86
    what is the value of iSum after running the following code?
    iSum = 0
    For iCnt = 1 to 5
    iSum = iSum + iCnt
    If iCnt = 5 then iSum = 200
  • Dec 13, 2009, 05:49 AM
    Perito

    First, you need a Next statement.

    Sum = 0
    For iCnt = 1 to 5
    iSum = iSum + iCnt
    Next iCnt
    If iCnt = 5 then iSum = 200

    iCnt is incremented at the end of each loop (that's when the comparison is made that ends the loop), so when the FOR loop ends, the value of iCnt will be 6.

    iSum will be 15 (1 + 2 + 3 + 4 + 5)

  • All times are GMT -7. The time now is 02:05 PM.