Repeat steps with a For loop
Add the whole numbers from 1 to 100 with For( and End, and show the total 5050.
For(I,1,100) runs the lines between it and the matching End once for each value of I: 1, 2, 3 and so on up to 100. A fourth number sets the step, for example For(I,1,99,2) for odd numbers only.
To add things up, start a total at 0 before the loop, add to it inside the loop, and show it after End.
Classic Graphing 84
Start on the main screen: Press 2nd then mode (QUIT) to get back to the home screen. Pressing clear on a menu also backs out of it.
Create a program called SUM: PRGM, left arrow, 1, then S (LN), U (5), M (÷) and ENTER.
prgm◀1ln5÷enterLine 1, 0→S: type 0, press STO→, then ALPHA, LN for S, and ENTER.
0sto→alphaSlnenterLine 2, For(I,1,100): PRGM, 4 (For(), then I (ALPHA, x²), comma, 1, comma, 100, close the bracket and ENTER.
prgm4alphaIx²,1,100)enterLine 3, S+I→S: the body of the loop adds I to the total.
alphaSln+alphaIx²sto→alphaSlnenterLine 4, End: PRGM, 7 marks the end of the loop.
prgm7enterLine 5, Disp S: PRGM, right arrow, 3, then S.
prgm▶3alphaSlnQuit and run it: 2nd, MODE, then PRGM, 1, ENTER.
2ndQUITmodeprgm1enter
You should see1 + 2 + … + 100 = 5050.
Change 100 to another number to add up to that instead. The program runs in the background, so long loops never freeze the keypad. Press ON to stop one.
Tips
- Every For(, While, Repeat and If-Then needs its own End. A missing End gives ERR:SYNTAX.
- While repeats as long as a condition is true; Repeat runs at least once and stops when its condition becomes true.
Related guides
- Make decisions with If, Then and ElseWrite a program that tells you whether a number is negative, using If, Then, Else and End.
- Create your first program with NEWMake a program called HELLO that shows a message, then run it from the EXEC tab.
- Sum a sequence or seriesAdd up the first terms of a sequence, such as 1 + 3 + 5 + … + 19, which comes to 100.