Thread: For not while?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    99

    For not while?

    I successfully completed this problem with a while loop, but I was wondering if I could do it with a for loop. Why is the following not working?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define BIL 1000000000
    
    int main(void)
    {
        float num_one;
        float num_two;
        float math_answer;
        int loop_test;
        int i;
    
      
        printf("Give me two numbers:  ");
    
     
        for(i = scanf("%f%f",&num_one, &num_two); (-BIL<=num_one<=BIL) && (-BIL<=num_two<=BIL); scanf("%f%f",&num_one, &num_two));
        {
            math_answer = (num_one-num_two)/(num_one*num_two);
            printf("Here is the (first # - the second #) divided by their product:  %f\n", math_answer);
            printf("Give me two more numbers or q to quit:  ");
        }
    
        printf("Quitting :)");
        return EXIT_SUCCESS;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for(i = scanf("%f%f",&num_one, &num_two); (-BIL<=num_one<=BIL) && (-BIL<=num_two<=BIL); scanf("%f%f",&num_one, &num_two));
    1. a <= b <= c does not do what you think it does.
    What you end up with is a <= (0 or 1, the boolean result of b <= c)

    2. The ; at the end really messes with your head.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Any while loop can be rewritten as a for loop, and vice versa.

    You haven't specified what you mean by "not working". But, clearly, the reason will be that your translation from one loop type to another is flawed.

    I'll bet that you have also blown it with testing the condition
    Code:
    (-BIL<=num_one<=BIL) && (-BIL<=num_two<=BIL)
    If you expect this to test if num_one is between -BIL and BIL and then test if num_two is between -BIL and BIL.

    Instead, it will test -BIL <= num_one (which will give a value of zero or one) first. It will then compare that zero/one is less than BIL (which will always be true). Similarly for num_two. Because of that, you have written an infinite loop (the condition is always true, regardless of input).

    The semi-colon at the end of the for() statement will ALSO mean that the following block will never be executed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    For your consideration, I don't consider typing scanf() twice in a loop to be neater. The code repetition means that you could change one scanf() one day and not change the other. Most loops can be written this way
    Code:
    while ((i = scanf("%f%f", &num_one, &num_two)) == 2)
    Secondly if you want to C-ify a mathematical statement like -BIL<=num_one<=BIL you have to use AND.
    Code:
    ( -BIL <= num_one && num_one <= BIL ) && ( -BIL <= num_two && num_two <= BIL )

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Note the following is probably a mistake:

    Code:
    for (i = 0; i < N; i++);
    {
       foo();
    }
    In this code, foo() gets called exactly once, regardless of N. Note the semicolon in this case is a "Null statement" to do nothing inside the for loop.

    That, and what the other posters said regarding the familiar math expression LOW < x < HIGH

    In C, this becomes: (LOW < x && x < HIGH)

    As far as your code, I don't understand what you want to do with BIL. For checking whether the user enters 2 floats, the scanf() function should return 2:

    Code:
        printf("Give me two numbers:  ");  
        while(scanf(" %f %f",&num_one, &num_two) == 2)
        {
            getchar(); // discard extra whitespace
            math_answer = (num_one-num_two)/(num_one*num_two);
            printf("Here is the (first # - the second #) divided by their product:  %f\n", math_answer);
            printf("Give me two more numbers or q to quit:  ");
        } 
        printf("Quitting :)");

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Thanks for all the input - it was really helpful! Sorry about the stupid semicolon and paranthesis error....

Popular pages Recent additions subscribe to a feed