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

In summary, The code tries to find the value of Q_1 that is nearest to 30000, with Q_1 always being greater than or equal to 30000.
  • #1
WhiteWolf98
86
5
TL;DR Summary
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
  • #3
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
 
  • #4
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 WhiteWolf98
  • #5
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
 
  • #6
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:
  • #7
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.
 

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

1. How do I set the condition for a while loop to stop when reaching a certain value?

To make a while loop stop when reaching a certain value, you need to set a condition using a comparison operator such as == or >=. This condition should be placed in the while loop's parentheses. For example, if you want the loop to stop when the variable i reaches a value of 10, the condition would be i == 10.

2. Can I use a variable as the condition for a while loop to stop?

Yes, you can use a variable as the condition for a while loop to stop. This allows for more flexibility in controlling when the loop should stop. Just make sure the variable's value is updated within the loop so that the condition can eventually be met.

3. What happens if the condition for a while loop to stop is never met?

If the condition for a while loop to stop is never met, the loop will continue indefinitely. This can result in an infinite loop, which can cause your program to crash or become unresponsive. It is important to carefully consider the condition and ensure that it will eventually be met.

4. How can I make a while loop stop when reaching a certain value, but also execute the code at least once?

One way to make a while loop stop when reaching a certain value but also execute the code at least once is to use a do-while loop. The do-while loop will first execute the code and then check the condition. This ensures that the code will be executed at least once, even if the condition is not initially met.

5. Is it possible to stop a while loop from within the loop itself?

Yes, it is possible to stop a while loop from within the loop itself. This can be done by using the break statement, which will immediately end the loop when it is encountered. The break statement is typically used in conjunction with an if statement and a certain condition to control when the loop should stop.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
Replies
7
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
Back
Top