Thread: How to stop while loop before a certain condition is fulfilled? Need answer quick.

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    4

    How to stop while loop before a certain condition is fulfilled? Need answer quick.

    Ok, here's the deal. I wrote a code for my professor's class for the mark and it looks like this -
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int m,n, eps;
        float s;
        printf("m , n: ");
        scanf ("%d %d", &m, &n);
        while (m<n+1){
            if (m%2==0){
                s+=m*m*m;
            } else {
                s+=(float)1/(m*m);
            }
            m++;
        }
        printf("Result is %.2f ", s);
        return 0;
    }
    Apparently, I am supposed to add something new that will stop the condition after it's meaningless for the result. It would go something like (eps>=1e+5) or something. Please help, my mark depends on it.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    if it reaches that meaningless for the result just break out of it. requires
    1. if statment
    1. condition that will result as true
    1. break statment

    mix properly than watch it work.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    4
    Yeah, I figured it out. I tried if statement, but apparently professor didn't allow it. It should be done (for this exercise) as while (m<n+1 && 1.0/(m*m)>eps) for eps=1E-3. That's all there is to it.
    Last edited by Aleksandar; 12-11-2017 at 12:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop - stop condition
    By hzcodec in forum C Programming
    Replies: 6
    Last Post: 05-24-2014, 12:11 AM
  2. why this while condition doesnt stop the loop??
    By transgalactic2 in forum C Programming
    Replies: 40
    Last Post: 04-07-2009, 08:38 AM
  3. stupid question need quick answer
    By mbsupermario in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 06:17 AM
  4. Quick Answer...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-27-2002, 11:22 AM
  5. Need Answer Quick
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2002, 12:55 PM

Tags for this Thread