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

In summary, the programmer is asking a question about how to make a program terminate. They use a while loop with a condition that is not true to exit the loop.
  • #1
jam12
38
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
  • #2
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.
 
  • #3
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.
 
  • #4
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 */
}
 
  • #5
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:
  • #6
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.
 
  • #7
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:
  • #8
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.
 

1. What are numerical methods in C code?

Numerical methods in C code refer to the techniques and algorithms used to solve mathematical problems, such as solving equations or finding the roots of a function, using numerical approximations and calculations.

2. What are the advantages of using numerical methods in C code?

The advantages of using numerical methods in C code include their efficiency and accuracy in solving complex mathematical problems, as well as their ability to handle large amounts of data. They also allow for automation and can be easily implemented in computer programs.

3. What are some common numerical methods used in C code?

Some common numerical methods used in C code include Newton's method, the bisection method, and the secant method. These methods are used for finding roots of a function, while other methods such as Gaussian elimination and LU decomposition are used for solving systems of linear equations.

4. How do you choose the appropriate numerical method for a problem?

The choice of numerical method depends on the specific problem at hand. Factors to consider include the type of problem (e.g. finding roots, solving equations, etc.), the size of the problem, and the level of accuracy required. It is important to understand the strengths and limitations of each method to select the most appropriate one for a particular problem.

5. Is it necessary to have a strong understanding of mathematics to use numerical methods in C code?

While a strong understanding of mathematics can certainly be helpful, it is not always necessary to use numerical methods in C code. Many numerical methods have been developed and optimized for use in programming languages, making it possible for those without a deep understanding of mathematics to still use them effectively. However, a basic understanding of mathematical concepts and principles is recommended for a more thorough understanding of how these methods work.

Similar threads

  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
6
Views
2K
Replies
11
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top