Pages

Monday, November 16, 2015

BASIC Program For Sum of Numbers Using IF Then Statement

5 CLS
10 Sum = 0
20 I = 1
30 Input "Enter a Number"; N
40 Sum = Sum + N
50 I = I + 1
60 IF I > 10 Then 80
70 Goto 30
80 Print " The sum is:" Sum
90 End

The first line CLS clears the screen of the previous program that was executed. Sum is initialised to 0. Always advisable to initialise it to 0. 'I' is the counter. You can choose to initialise it to 0 or 1. The keyword 'INPUT' obtains a number from the user by displaying a message 'Enter a number'.


Next line, the counter is incremented, and the compiller moves to the next line and assigns 0 + Entered number to Sum. The 'IF' statement sets a condition and if the condition is true, the program continues to line 80, if the condition is false, that is the counter is below ten, it goes back to line 30 requesting another number from the user.


The condition must be satisfied before the program prints the result to the screen.