Home > Tutorials > C Tutorials > Programming In C: Infinite and Nested Loops

Programming In C: Infinite and Nested Loops

Infinite Loops

If you want the viewer to decide when to exit a loop, use an infinite for loop.

The infinite for loop sets no requirements inside the for statement, like this:

for( ; ; )

Many people shop online, using the internet. Here is a program to help you keep track of how much money you’ve spent. In this program, we initialize four variables:

  1. loop = 0 to set an infinite loop
  2. done to ask the viewer when to stop the infinite loop
  3. amount = 0.0f for each purchase amount
  4. total = 0.0f to add up all the purchase amounts

In this program, we will keep adding amounts to the total by using total += amount; The +=adds the latest amount to the total.

Here is the program:

#include<stdio.h>
void main( )
{
int loop = 0;
char done;
float amount = 0.0f;
float total = 0.0f;
printf(“\nTo calculate the total of your online purchases, enter each amount.”);
for( ; ; )
{

printf(“\nType an amount: “);

scanf(” %f”, &amount);
total += amount;
++loop;
printf(“\nAre you done? (y or n): “);

scanf(” %c”, &done);
if(done == ‘y’)

break;

}
printf(“\nYour total purchases equals $%.2f.\n”, total);

Save your source code as infinite.c. Compile it. Link it. Run it.

NOTE: If you experience problems, add a space before the %c in the second scanf line. Compile, link, and run the program again.

Nested For Loops

Remember when we nested a conditional inside a conditional, in the example about whether you remember the Reagan and Kennedy years? Well, we can also make nested for loops.

To make nested for loops, we must initialize and set up two different counters: one for theouter loop and one for the inner loop.

Let’s say you own a flower shop and you’re having a sale on roses. Each rose costs $1.75. We can display a price list using nested for loops. The outer loop will list the number of roses. The inner loop will give the prices. Here is the result we want:

1
2
3
etc.

$1.75
$3.50
$5.25
etc.

The formula for the outer loop is simple: begin at 1 and increment (++) on each loop, until you reach the amount of roses that the customer wants.

for(outer=1; outer<=amount; outer++)

The formula for the inner loop is more complex: begin at 1 and multiply by 1.75 for each loop.

for(inner=1; inner<=outer; inner++)

total = outer * 1.75;

The inner loop multiplies the outer loop counter by 1.75, to determine the total.

We need to initialize several variables in this program:

  1. the outer loop counter: int outer = 1;
  2. the inner loop counter: int inner = 1;
  3. the amount of roses the customer wants to buy: int amount = 0;
  4. the total price for each amount of roses: float total = 0.0f;

Here is the program:

#include<stdio.h>
void main( )
{
int outer = 1;
int inner = 1;
int amount = 0;
float total = 0.0f;
printf(“\nHow many roses do you want to buy?: “);
scanf(“%d”, &amount);
printf(“\nHere is our price list:”);
for(outer=1; outer<=amount; outer++)
{

total = 0;
for(inner=1; inner<=outer; inner++)
total = outer * 1.75;
printf(“\n%d \t $%.2f”, outer, total);

}

Save your source code as roses.c. Compile it. Link it. Run it.

 

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close