1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Understanding Static variable and for loop

Discussion in 'C' started by mac07, Dec 11, 2014.

  1. mac07

    mac07 New Member

    Joined:
    Dec 11, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Why the below code does not throw any error ?, as the 2nd argument of the for loop takes boolean type whereas it's getting data of int type .

    The code runs with any error and gives output as 5 2
    I don't know understand how it gives 5 2 ?

    Here is my understanding ----->

    In the 1st iteration
    for(7; 6; 4) {
    print 5
    }
    - -- - - - - - - - - - - - - - - -- - - -
    In the 2nd iteration
    for(3; 2; 0) {
    print 1
    }

    - -- - - - - - - - - - - - - - - -- - - -
    In the 3rd iteration
    Now what ? When does the for loop stop?
    - -- - - - - - - - - - - - - - - -- - - -

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int r();
    int main(){
    for(r();r();r()) {
    printf("%d ",r());
    }
    return 0;
    }
    int r(){
    int static num=7;
    return num--;
    } 
    

    please help ....... waiting
     
  2. mac07

    mac07 New Member

    Joined:
    Dec 11, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Sorry , I meant "The code runs without any error and gives output as 5 2"
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    It doesn't throw an error because it is syntactically valid (although it's a terrible piece of programming).

    Informally the code
    Code:
    for (A; B; C)
    {
     // code
    }
    
    is equivalent to:
    Code:
    A;
    while (B)
    {
    // code
    C;
    }
    
    So we can see A is only executed once. Your dry run executes it twice.

    So let's run through this one step at a time. A is the first r() call so num becomes 6. The return value is thrown away.

    while B: r() is called again, 6 is returned, num becomes 5. 6 is TRUE, so we go into the block.

    In the printf: r() is called; 5 is returned, num becomes 4. printf prints 5.

    C is executed: r() is called; 4 is returned and discarded, and num becomes 3.

    Back to the top of the loop. We don't execute A again; look at the equivalence code. B is executed: i.e. r() is called, which returns 3 and num becomes 2. 3 is TRUE so we go into the loop again.

    In the printf, r() is called which returns 2 and num becomes 1. printf prints 2.

    C is executed: r() is called, which returns 1 and sets num to zero. Back to the top.

    In the while again: r() is called, which returns zero and sets num to -1. Zero is FALSE so the loop terminates, and the program exits.
     
    shabbir likes this.
  4. mac07

    mac07 New Member

    Joined:
    Dec 11, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Silly mistake..

    But why the for loop in Java doesn't compile tho code? It says booean is expected or something like that.

    @xpi0t0s Thanks a lot ....
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Java is a different language and works differently. From the Java Language Specification on for loops:

    http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.14

    we can see it says:

    The Expression must have type boolean or Boolean, or a compile-time error occurs.

    Since r() does not have a return type of boolean, you get this error. You may be able to fix this with something like
    Code:
    r() != 0
    
    But I would suggest you focus on one language at a time, and make up your mind if you are learning C or Java, then stick to that until you've mastered the basics of the language (doesn't take long), then start on the other. There are lots of differences between C, C++ and Java.
     

Share This Page