While loop - Variable not changing in C

In summary, the code is supposed to check if the first integer entered is zero, but it keeps running the rest of the program.
  • #1
A.J.710
53
1
I am not sure if this is supposed to go in the homework forum or not since it is more of an off physics topic so sorry if it is incorrect.

I have a programming assignment for class and I am having a lot of trouble with a while loop. For this part of the program I have to prompt the user to enter 4 non zero integers. If one of them is 0 I have to prompt them to re enter the integers. If all of them are 0 the program has to be terminated. I am doing it in a while loop as instructed but I can't get the variable to change when the statement is true.

I am not nearly done. I am just testing for the first variable so far just to get the hang of it.

Here is little section of the program that I cannot get to work. Basically if the first integer entered is 0 it should end the while loop and end the program, if not, it continues with the rest of the program which is basically just adding two rational numbers and finding the GCD to simplify them. (that part is obviously not shown here).

The issue I am having is that "again" never changes to 0 and it just keeps running the rest of the program and then going back to the printf function in the while loop.

int main( )
{
int a, b, c, d, again, azero, pzero;
int num, den, g, sum;
again = 1;


while (again==1)
{
printf("Please enter four non zero integers \n");
scanf("%d %d %d %d",&a, &b, &c, &d);

if ((a=0))
{
again = 0;
}
else
 
Physics news on Phys.org
  • #2
A.J.710 said:
if ((a=0))

Think again about this... hint: you did a similar comparison correctly elsewhere in the code you showed us.
 
  • #3
Is it supposed to be (a==0)?

jtbell said:
Think again about this... hint: you did a similar comparison correctly elsewhere in the code you showed us.
 
  • #4
== is used for comparison
= is used as an assignment operator
 
  • #5
camel-man said:
== is used for comparison
= is used as an assignment operator


Got it, Thanks
 

FAQ: While loop - Variable not changing in C

Why is my variable not changing in a while loop?

There could be several reasons for this. One possibility is that the loop condition is never met, so the loop never executes. Another possibility is that the variable is not being properly updated within the loop, or that there is an error in the code that is preventing the variable from changing. It's important to carefully check the logic of your code and make sure the variable is being updated as intended.

How can I troubleshoot a variable that is not changing in a while loop?

The first step is to carefully examine your code and make sure the loop condition and variable updates are correct. You can also use a debugger to step through your code and see the values of the variable at each iteration of the loop. Additionally, you can add print or logging statements to track the value of the variable as the loop executes.

What is the best way to update a variable in a while loop?

The best way to update a variable in a while loop is to use a statement that changes the value of the variable in some way. This could be a simple assignment statement, or it could involve performing some operation on the variable and then assigning the result back to the variable. It's important to make sure the update statement is placed in the correct location within the loop so that it is executed at each iteration.

Can a while loop cause a variable to not change?

Yes, a while loop can potentially cause a variable to not change. If the loop condition is never met, the loop will not execute and the variable will not be updated. Additionally, if there is an error in the code that is preventing the variable from being updated, the loop may continue to execute without changing the variable's value.

How can I fix a while loop that is not changing a variable correctly?

If you are having trouble with a while loop not properly changing a variable, the first step is to carefully review your code and make sure the loop condition and variable update statements are correct. You may also want to use a debugger or add print/logging statements to track the variable's value as the loop executes. If you are still unable to resolve the issue, consider seeking help from a more experienced programmer or reaching out to online communities for assistance.

Similar threads

Replies
3
Views
1K
Replies
1
Views
9K
Replies
21
Views
2K
Replies
3
Views
908
Replies
12
Views
2K
Replies
10
Views
2K
Replies
17
Views
2K
Back
Top