Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
C: Loops for data pairs using while


1. C: Loops for data pairs using while
If this page does not make sense to you after studying it and trying the code, then go back and study and try the code in the following.
There are times when adjacent pairs of data need to be considered or processed together. Consider the following sequence of integers. Here is a sequence of lines highlighting each of the pairs. The code below shows how to read in a series of integers and then add adjacent pairs together.

Note that a trailer loop is used where a negative value terminates the pairs. A variable is used to remember the last value so that adjacent pairs can be processed. Here is the C code.


2. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Note that if you fail to read in the rightValue1 at the end of the loop body, the loop will never terminate (assuming the loop body is executed at least once).

3. End of page