Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Unwanted addition in my C program using "while"

SOLVED

Hello all. I am brand new to programming and would like just an explanation as to exactly why I am getting a specific result

After learning if, else, while, and a couple other things I decided to think of my own little simple program to see how I do without copying from the book. Figured it would help me gain some basic critical thinking for programming when I'm the one creating my own problems and fixing my own problems. Everything went according to the way I wanted it to, however it seems that somehow I am getting one final unwanted addition outside my while loop, which is what I'm trying to figure out.

Here's the code (My program goal parameters written in the comment on top)

Code:
```/* A program that counts from 0-100 in increments of 2, 0-150 in increments of 3, and 0-50 in increments of 1 */
#include <stdio.h>
main()
{
int doubles = 0, triples = 0, singles = 0;

while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
}
printf("dude, that's a lot of doubles,  triples, and singles\n");
printf("doubles total: %d\n", doubles);
printf("triples total: %d\n", triples);
printf("singles total: %d\n", singles);

return 0;
}```
When I build and run, everything is just peachy except the doubles total is 102, the triples total is 153, and singles total is 51, when they should all respectively be 100, 150, and 50 (or at least that was the plan anyway)

If I remove my four final printf statements, the totals become what I want them to...but I don't understand how the printfs cause the values to change.

The only explanation I can come up with is that the inclusion of the printfs actually for some reason cause the while to be looped one final [unwanted] time???

EDIT: I'm 27 and work full time; this is not a homework assignment and I am not a student. (I'm teaching myself via some reading materials and online resources)
Just didn't want any miscommunication since school is starting back up and homework assignments aren't allowed on the forums.

• Its a simple issue with your loop condition: ` while (doubles <= 100){`. When doubles reaches 100, it proceeds to loop for a final time. Simply compare to < 100 and it will give you the final results of 50, 100 and 150.

• Originally Posted by Fou-Lu
Its a simple issue with your loop condition: ` while (doubles <= 100){`. When doubles reaches 100, it proceeds to loop for a final time. Simply compare to < 100 and it will give you the final results of 50, 100 and 150.
Although I understand what you're saying, Im still unsure because if the four printf statements are taken away, i get 50,100,150.

So what Im saying is when doubles reaches 100 and those four printf statements at the bottom are taken out of the program, the loop does NOT execute that one last time.

Alternatively, with those printf statements in there, when doubles reaches 100, the loop DOES execute one last time

Im trying to figure out how in the heck a printf causes this

• if the four printf statements are taken away, i get 50,100,150.
The increment statements come after the first three printf statements.

Code:
``` while (doubles` < `100){
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);

}```
and see what happens

• ## Users who have thanked abduraooft for this post:

AGENT_P6 (09-05-2012)

• Hey again and thanks for the replies.

I understand that I should be using "is less than 100" rather than "is equal to or less than 100" in order to get the desired results

What I'm trying to figure out is why this
Code:
```/* A program that counts from 0-100 in increments of 2, 0-150 in increments of 3, and 0-50 in increments of 1 */
#include <stdio.h>
main()
{
int doubles = 0, triples = 0, singles = 0;

while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
}
printf("dude, that's a lot of doubles,  triples, and singles\n");
printf("doubles total: %d\n", doubles);
printf("triples total: %d\n", triples);
printf("singles total: %d\n", singles);

return 0;
}```
will give me the totals of 102, 153, and 51 WHEN THIS:

Code:
```/* A program that counts from 0-100 in increments of 2, 0-150 in increments of 3, and 0-50 in increments of 1 */
#include <stdio.h>
main()
{
int doubles = 0, triples = 0, singles = 0;

while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;

}
return 0;
}```
will give me 100, 150, and 50. Why does the inclusion of the last 4 printf statements cause the loop to execute 51 times, when the EXCLUSION of the last 4 printf statements causes the loop to only execute 50 times

Sorry for the confusion, as this stuff is new to me.

I fully understand that I should be using "less than" rather than "equal to or less than". The purpose of my inquiry is only to understand WHY my the first set of code in this post makes for totals of 102, 153, and 51, yet the second set of code in this post results in 100, 150, and 50 when they are BOTH using the "equal to or less than" operator

Sorry to be a pain...I'm just really inquisitive and want to know exactly why things work the way that they do

• The first one shows 1 to many versus just removing them since the values are incremented in the loop after the printfs. When condition is <=100, then it increments them once more leaving them with the values 51, 102 and 153.
Removing the = sign will leave the variables with the values 50, 100, and 150 after the loop, so you can still add the last four printf's if desired.

• ## Users who have thanked Fou-Lu for this post:

AGENT_P6 (09-05-2012)

• Code:
```while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
}```
This code will print out (on its last iteration through the loop) 100, 150, 50, but immediately after you print those values out, you then execute:
Code:
```        doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;```
Which sets doubles to 102, triples to 153 and singles to 51. Correct?

Then the loop exits, and in one example you print the final values of these variables (102, 153, 51). In the other example, you don't print them out so you don't notice that you incremented them.

Dave

• ## Users who have thanked tracknut for this post:

AGENT_P6 (09-05-2012)

• Originally Posted by tracknut
Code:
```while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
}```
This code will print out (on its last iteration through the loop) 100, 150, 50, but immediately after you print those values out, you then execute:
Code:
```        doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;```
Which sets doubles to 102, triples to 153 and singles to 51. Correct?

Then the loop exits, and in one example you print the final values of these variables (102, 153, 51). In the other example, you don't print them out so you don't notice that you incremented them.

Dave
This explains it

It makes sense now. The program always counted to the wrong numbers, but only one set of code had the final printfs to make me aware of that 51st loop.

Told u i was new to this *facepalm*

Im going to click the thanx button for all of u tho, as all of your posts helped me learn and understand more.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•