Program name: while.c
Language features of C contained in "while.c"
a "while" statement
a section of code (lines 14 and 15) that is repeated under the while
statement's control
Function: adds all the integers from 1 through n, where n is an
integer the user types in
Source code:
Explanations:
By default each line of code is executed exactly once when your program runs. Then the program moves on to the next line. But sometimes it is desirable to go back and re-run one or more lines a second time, or more. Repeating is called "looping" and is implemented here by C's "while" statement, in line 12. Below it in ines 13 and 16 are a pair of curly brackets. They enclose the lines of code to be repeated, lines 14 and 15. This program's job is produce the answer "what's the sum of the first several integers?" step by step. In each step it adds in another integer, starting with 1. The container into which it adds them in is the variable "answer". If you want it to produce the sum of the first 3 integers it must do this 3 times. If you want it to add the first 100, it must do it 100 times. line 14 takes whatever is in answer (which starts at 0), adds to it the next term, and puts the result back into answer (thus replacing/increasing the value of answer). The "next term" is initially 1. But in line 15 every time the loop runs, it ups it to the next integer getting ready for the next summing. How many times does it do this? That's controlled in the while statement by what's inside its parenetheses, namely "term <= n ". It's called the while's condition. As long as that's true, while keeps going. But if it becomes false, the loop quits and the program moves on (to line 17 where it prints the result). It does become false, because term is always increasing in line 15. Once the program has added in all the integers up to and including the user's chosen one, when it gets ready to go further it stops because the proposed next term is too big (beyond the user's chosen integer).
To do:
Key this program in, using a text editor. Save it in a file named "while.c". Then, in order to convert it into a form that contains correct Intel machine instructions, thus enabling the computer to run it, bring up the Windows command box and give this command:
bcc32 while.c
Alternatively use a browser to go to a C compiler web site. Cut your program's text from the text editor and paste it into the appropriate place in the web site. Tell the web site to compile (there's generally a button that says "compile" or maybe "run".)
If there are no error messages, a new file named "while.exe" will result. Then give the command:
while.exe
A question appears on the monitor, "Add all integers up through which one?" and the program waits for you to type one in. After you type it, and press the enter key, it adds all the integers starting from 1 going up through the one you type. Then it prints the result. For example if you type 5, the program prints "The sum of all the integers from 1 through 5 is 15".