How to Terminate a While Loop in C Programming with a Printed Message or Value?

Click For Summary

Discussion Overview

The discussion revolves around how to properly terminate a while loop in C programming while also printing a message or value. It includes aspects of programming style, loop control mechanisms, and testing the accuracy of a root-finding algorithm, specifically in the context of the Newton-Raphson method.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant inquires about terminating a while loop in C by printing a value or message, providing an initial code snippet.
  • Another participant suggests using the 'break' statement to exit the loop and mentions that calling 'exit(0)' is possible but not recommended for good programming style.
  • A different participant proposes using 'return' to exit the function, which can also terminate the loop.
  • One suggestion is to incorporate the exit condition into the loop condition itself to manage termination more effectively.
  • A participant expresses gratitude for the advice and seeks further guidance on testing the accuracy of the root found using the Newton-Raphson method, specifically asking about the appropriate code to implement this.
  • Another participant explains that to test the root, one can evaluate the function at the root and check if the absolute value is less than epsilon.
  • A follow-up question arises regarding the relationship between the loop termination condition and the accuracy of the root found, indicating some confusion about the logic.
  • One participant requests to see the current version of the code to better understand the issue being faced.

Areas of Agreement / Disagreement

Participants generally agree on the use of 'break' and 'return' for loop termination, but there is some uncertainty regarding the relationship between the loop's exit condition and the accuracy of the root found. The discussion remains unresolved regarding the specific logic of the code and its implications.

Contextual Notes

There are limitations in the discussion regarding the assumptions made about the function's behavior and the definitions of terms like 'root' and 'accuracy'. The exact implementation details of the code are not fully provided, which may affect the understanding of the problem.

jam12
Messages
37
Reaction score
0
Im using the c programming language and just wanted to ask a quick question. In a while loop how do you make the program terminate by printing a value or a message here's my code

while ((fabs(func(x))>epsilon))
{

if(deriv(x)==0) {
print the last value of x and stop the whole program}

else {

y=(func(x)/deriv(x));

x=x-y;

printf("%d\n",iteration);

iteration=iteration+1;

printf("%lf\n",x);

}

}
 
Last edited:
Technology news on Phys.org
You can break out of a while-, for- or do-while-loop by using break. You can exit the program it the middle of everything by calling exit(0), but this is not considered good programming style.
 
filiplarsen said:
You can break out of a while-, for- or do-while-loop by using break.
You can also call a return to break out of the function midway, as a function can have multiple returns.
 
Or you can make the exit condition part of the loop condition:

Code:
while ((fabs(func(x))>epsilon) && (deriv(x) != 0))
{
    y=(func(x)/deriv(x));
    x=x-y;
    printf("%d\n",iteration);
    iteration=iteration+1;
    printf("%lf\n",x);
}

/* Now that you're out of the loop, figure out why you exited */

if (deriv(x) == 0)
{
    printf ("oops, the derivative was zero!\n");
{
else
{
    /* carry on normally */
}
 
thanks guys, i needed to use break.
When i have found my root (Program for Newton Raphson method), do you know how i can test it is accurate to a correct level of precision?, Like what kind of code would i go about writing? I used epsilon=1e-7.
 
Last edited:
If you're trying to find the root of some equation f(x) = 0 for whatever function f you're finding the root of, testing the root is easy -- just evaluate f(r), where r is your root. If |f(r)| < epsilon, you're good.
 
thanks Mark44,
But i was wondering, that my while loop ends when this condition is not true: (func(x))>epsilon and then displays the root x=r. So how could it be that abs(f(r))<epsilon if x=r is the root found?
 
Last edited:
What's your code look like now? I can't explain why your code is doing something if I don't see the current version of the code.
 

Similar threads

Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
11
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
47
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
3K