While loop - Variable not changing in C

  • Thread starter Thread starter A.J.710
  • Start date Start date
  • Tags Tags
    Loop Variable
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to the use of a while loop in C, specifically focusing on the correct implementation of conditional statements to control loop execution based on user input. The context is a programming assignment that requires user input validation.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • A participant describes a problem with a while loop where a variable intended to control the loop's execution does not change as expected.
  • The participant shares a code snippet and explains that the loop should terminate if the first integer entered is 0, but it continues running instead.
  • Another participant points out a potential error in the conditional statement, suggesting that the participant may have used the assignment operator (=) instead of the comparison operator (==).
  • Further replies confirm that the correct syntax for comparison should be (a==0) rather than (a=0).
  • Participants clarify the difference between the assignment operator (=) and the comparison operator (==).

Areas of Agreement / Disagreement

Participants generally agree on the correct usage of comparison versus assignment operators, with no significant disagreement noted in the responses.

Contextual Notes

The discussion does not address any limitations or unresolved mathematical steps, focusing instead on the programming syntax issue.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those encountering issues with control structures and conditional statements.

A.J.710
Messages
56
Reaction score
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
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.
 
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.
 
== is used for comparison
= is used as an assignment operator
 
camel-man said:
== is used for comparison
= is used as an assignment operator


Got it, Thanks
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K