I'm writing some while loops but I feel like there is a better way to write them.

int i=3;
	while (i<31)
	{
		cout << i << " ";
		i+=3;
	} //this one is for the first 10 terms of a sequence starting with 3 and adding 3 each time.

int i=2;
	while (i<50000)
	{
		cout << i << " ";
		i*=2;
	} //this one is the first 15 terms starting at 2 and doubling each one.

Is there some way to let the program know how many variables to show without figuring out how far it has to go? Also, what if I wanted to write a loop where each number is subtracted by one more than the one before it (100, 99, 97, 94, 90...)?

Dani AI

Generated

A simple, reliable pattern when you must use a while loop is to drive the loop with a counter (number of terms) instead of trying to guess an end value. That makes the intent explicit and avoids off-by-one surprises.

// print first 10 terms: start=3, step=3
int start = 3;
int step = 3;
int terms = 10;
int printed = 0;
int current = start;

while (printed < terms) {
    cout << current << ' ';
    current += step;
    ++printed;
}

For the doubling sequence do the same but multiply. Use a wider integer type if numbers can grow large:

// print first 15 terms starting at 2, doubling each time
long long cur = 2;
int terms = 15;
int i = 0;

while (i < terms) {
    cout << cur << ' ';
    cur *= 2;
    ++i;
}

For the sequence 100, 99, 97, 94, 90,... update the value by subtracting a delta that increments each iteration:

int value = 100;
int delta = 1;
int terms = 5; // or use another stop condition
int count = 0;

while (count < terms) {
    cout << value << ' ';
    value -= delta;
    ++delta;
    ++count;
}

Troubleshooting note: the reason saw a decrement-by-2 pattern is that the loop both decreased the base (i) and increased the subtraction counter (j). That makes the printed value change by -2 each step. As hinted, modify only one quantity per logical update (either adjust the base by the current delta, or compute from start using a counter). Also heed ’s point: pre-counting terms is perfectly fine and often clearer.

Recommended Answers

All 5 Replies

Is it a requirement that you use a "while" loop?
Yes, there is a way to pre-determine the number of loops, but it's not more efficient.

Also, what if I wanted to write a loop where each number is subtracted by one more than the one before it (100, 99, 97, 94, 90...)?

use a counter that increments itself, then you use this counter to subtract the main variable

Is it a requirement that you use a "while" loop?
Yes, there is a way to pre-determine the number of loops, but it's not more efficient.

while loop is the requirement.

use a counter that increments itself, then you use this counter to subtract the main variable

I tried something like this:

int i=100, j=0;
	while (i>50)
	{
		cout << i-j << " ";
		j++;
		i--;
	}

but it's just decreasing by 2 all the way to 0.

don't decrement the value of i by 1 every time you only need to use j n the operation...
something like this:

while (i>50)
	{
		cout << i << " ";
		j++;
		i-=j;
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.