How to make a while loop stop when reaching a certain value

Click For Summary

Discussion Overview

The discussion revolves around programming logic for controlling a while loop in a coding context, specifically how to stop the loop when a calculated value, Q_1, approaches a target value of 30,000. Participants explore various looping constructs and conditions, including the use of while loops and for loops, as well as logical operators.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a while loop that fails to stop as Q_1 becomes negative, indicating a potential issue with the loop's exit condition.
  • Another participant suggests using a for loop instead of a while loop, but does not elaborate on how it would apply to the problem at hand.
  • Some participants propose defining a range for Q_1 and using a while loop to check if Q_1 falls within that range, but express confusion about the implementation.
  • There is a discussion about using logical operators, specifically the logical AND (&&), to check if Q_1 is within specified limits, with one participant initially misunderstanding the condition.
  • A later reply clarifies that the condition should check if Q_1 is greater than a lower limit and less than an upper limit, correcting a previous error in logic.
  • Another participant elaborates on the goal of finding the value of Q_1 closest to 30,000, providing a more complex while loop structure that includes breaking conditions based on comparisons to the critical value.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to implement the desired functionality. There is no consensus on whether to use a while loop or a for loop, and confusion remains regarding the correct use of logical operators in the context of the problem.

Contextual Notes

Some participants indicate uncertainty about the behavior of the while loop when Q_1 approaches the target value, and there are unresolved questions about the correct implementation of logical conditions. The discussion includes various assumptions about the behavior of Q_1 and its relationship to the critical value.

WhiteWolf98
Messages
89
Reaction score
8
TL;DR
I basically want Q in the code to be 30,000 or something near to 30,000 (perhaps between two values, for example between 29,750 and 30,250). I don't know how to define the range though, and the code keeps going on infinitely
Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;

Q_1 = (m_1)*C_p1*(T_1in-T_1out);

while Q_1 ~= 30000
    T_1outnew = T_1out + 0.1;
    Q_1 = (m_1)*C_p1*(T_1in-T_1outnew);
    T_1out = T_1outnew;
end

T_1outnew keeps increasing, but the code never stops; and naturally, Q becomes negative
 
Physics news on Phys.org
I'm not sure how a for loop would help me here, I just want Q to be in a range. What if I defined another variable, which was a range, and used the while loop with that? For example:

Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;
Y = 29750:30250;

Q_1 = (m_1)*C_p1*(T_1in-T_1out);

while Q_1 ~= Y
    T_1outnew = T_1out + 0.1;
    Q_1 = (m_1)*C_p1*(T_1in-T_1outnew);
    T_1out = T_1outnew;
end

It still doesn't work, but I don't see how to use the for loop because I just want a check to see if something is between two numbers
 
WhiteWolf98 said:
I'm not sure how a for loop would help me here, I just want Q to be in a range. What if I defined another variable, which was a range, and used the while loop with that? For example:

Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;
Y = 29750:30250;

Q_1 = (m_1)*C_p1*(T_1in-T_1out);

while Q_1 ~= Y
    T_1outnew = T_1out + 0.1;
    Q_1 = (m_1)*C_p1*(T_1in-T_1outnew);
    T_1out = T_1outnew;
end

It still doesn't work, but I don't see how to use the for loop because I just want a check to see if something is between two numbers
Let's say you want Q_1 to be within the two values lower_lim and upper_lim. You can change your code to this:
Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;
lower_lim = 29750;
upper_lim = 30250;

Q_1 = (m_1)*C_p1*(T_1in-T_1out);

while (lower_lim < Q && Q < upper_lim)
    T_1outnew = T_1out + 0.1;
    Q_1 = (m_1)*C_p1*(T_1in-T_1outnew);
    T_1out = T_1outnew;
end
 
  • Like
Likes   Reactions: WhiteWolf98
I'm unfamiliar with &&, so I searched it up quickly. Since lower_lim < Q is not true, won't that stop the code there? I don't get any output anymore
 
Never mind, I apologise, while (lower_lim < Q && Q > upper_lim). One of the signs needed to be flipped.

Thanks a lot for your help, Wrichik
 
Last edited:
WhiteWolf98 said:
I'm unfamiliar with &&
That is logical AND. If you are unfamiliar with logical operators, I would advise you to stop here and go look them up. They are fundamental to any programming language.
WhiteWolf98 said:
Never mind, I apologise, while (lower_lim < Q && Q > upper_lim). One of the signs needed to be flipped.
That is not the way to check whether Q_1 is in a certain range.

You want Q_1 to be between 29750 and 30250. More explicitly, you want it to be greater than 29750 AND less than 30250.

With the code you posted, you are checking whether Q_1 is greater than 29750 AND greater than 30250. Does this seem correct?

I sense some ambiguity building up here. Let's look at what you wrote in the first post of this thread:
WhiteWolf98 said:
Summary:: I basically want Q in the code to be 30,000 or something near to 30,000
30000 is the critical point. You want your loop to give you the value of Q_1 that is nearest to 30000.

In the code inside the while loop, the value of Q_1 will decrease. At one point, it will be just greater than 30000 (say that value is Q_1a), and then it becomes just less than 30000 (Q_1b). If Q_1a - 30000 is less than 30000 - Q_1b, it means Q_1a is closer to 30000 than Q_1b. Otherwise, it means that Q_1b is closer to 30000. We will choose the final value of Q_1 accordingly, and break out of the loop.
Matlab:
m_1 = 1;
C_p1 = 2219;
T_1in = 100 + 273.15;
T_1out = 75 + 273.15;

Q_1 = (m_1) * C_p1 * (T_1in-T_1out);

old_Q1 = Q_1;
criticalValue = 30000;

while true
    T_1out = T_1out + 0.1;
    Q_1 = (m_1) * C_p1 * (T_1in - T_1out);
    
    if (Q_1 == criticalValue)
        % Q_1 is as near 30000 as possible -- it is equal to 30000. So
        % break from loop.
        break;
    elseif (Q_1 > criticalValue)
        old_Q1 = Q_1;
    else
        if ((old_Q1 - criticalValue) < (criticalValue - Q_1))
            Q_1 = old_Q1;
        end
        break;
    end
end
The value of Q_1 comes out to be 29956.4999999942 using the above code. The code assumes that Q_1 is > 30000 in line 6.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
5
Views
8K
Replies
7
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
4
Views
4K