In this article, I discuss the next major construct in C — the loop. I’ll start by introducing a new program template related to loops, the Input and Process Until Done template, and then I’ll demonstrate how to implement this template using one C looping construct — the while statement.

The Input and Process Until Done Template

One of the most common things you do in a computer program is repeatedly as the user for data and process that data until there is no more data to input. The Input and Process Until Done template provides an outline for how to write the code for this task.

Here is the pseudocode for the template:

Repeat the following until finished:

Read a value

Process the value

An example, which I’ll demonstrate in C down below, is to determine the average grade on a test by inputting all the test scores and then computing the average once the last test grade is entered. Here is some possible pseudocode for this problem:

While there are more grades to enter:

Prompt the user to enter a grade

Get the grade from the keyboard

Add the grade to the running total

Compute the average

Now let’s see how to implement this template using the while statement.

#c-programming-language #learn-to-code #c-programming #c #learn-to-program

Learning C: The Input and Process Until Done Template and the while Loop
1.25 GEEK