Thread: For loop yields no result(must see)

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    bbsr,odisha,india
    Posts
    27

    Question For loop yields no result(must see)

    Code:
    Friends,
    
    I need your help in this code:
    
    #include<stdio.h>
    void main()
    {
        int a,b;
        for(a=1,b=5;a==b;a++,b--)
         printf("%d",a);
    }
    
    the turbo c++ 1.01 compiler doesn't show any error, but it shows nothing in the output.Only a space.
    
    important: i have tired in visual c and borland also, but the same thing.Moreover, i need to print the value of variable a only when the value of a equals value of b.
    Last edited by suryap.kv1bbsr; 03-26-2010 at 04:56 AM. Reason: extra details

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Change:

    Code:
    a==b
    to:

    Code:
    a < b
    And it should work. What you are saying is essentially while this is true, do that, where this is your condition and that is the body of the loop. The problem is that from the first iteration of the loop the condition is false, that's why it wont run.

    Also, you should have int main, and return 0.
    Last edited by Subsonics; 03-26-2010 at 04:45 AM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    there are some free compilers that are not so ancient as turbo C...

    Look at Visual C Express for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    bbsr,odisha,india
    Posts
    27
    Quote Originally Posted by Subsonics View Post
    Change:

    Code:
    a==b
    to:

    Code:
    a < b
    And it should work. What you are saying is essentially while this is true, do that, where this is your condition and that is the body of the loop. The problem is that from the first iteration of the loop the condition is false, that's why it wont run.

    Also, you should have int main, and return 0.
    sorry,
    i need to print the value of variable a only when the value of a equals value of b.

    pls help me

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    bbsr,odisha,india
    Posts
    27
    Quote Originally Posted by vart View Post
    there are some free compilers that are not so ancient as turbo C...

    Look at Visual C Express for example
    i have used borland and visual c as well

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by suryap.kv1bbsr View Post
    sorry,
    i need to print the value of variable a only when the value of a equals value of b.

    pls help me
    Then I recommend that you print both a and b, in each iteration then you will see what is going on and also be able to figure out when and where that will happen.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Location
    bbsr,odisha,india
    Posts
    27
    Quote Originally Posted by Subsonics View Post
    Then I recommend that you print both a and b, in each iteration then you will see what is going on and also be able to figure out when and where that will happen.
    i have done that too

    Code:
    #include<stdio.h>
    void main()
    {
        int a,b;
        for(a=1,b=5;a==b;a++,b--)
         printf("%d %d",a,b);
    }
    but the output is blank

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Like I said, you have to change == to < or it wont work. The first thing that gets checked is that condition. ie. is a == b, the answer is no, a is 1 and b is 5 so the loop never runs.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by suryap.kv1bbsr View Post
    i need to print the value of variable a only when the value of a equals value of b.
    if this is only thing you need - you do not need a loop

    Code:
    int b = 5;
    
    int a = b;
    
    printf (" a = %d \n", a);
    so you better explain what you are REALLY trying to do.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    when u write a==b in for the compiler checks for 1==5 which is not true hence the program terminates and no output just blank space.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Location
    bbsr,odisha,india
    Posts
    27
    Quote Originally Posted by vart View Post
    if this is only thing you need - you do not need a loop

    Code:
    int b = 5;
    
    int a = b;
    
    printf (" a = %d \n", a);
    so you better explain what you are REALLY trying to do.
    i know that there are other ways of writing the program but i want to accomplish the following by a for loop:
    [logic]
    two variables a and b have values 1 and 5 respectively.
    both are incremented and decremented the same time.
    the computer first checks whether 1==5, since it fails, the loop is run again
    this time it checks whether 2==4,since it fails, the loop is run again
    this time it checks whether 3==3,since it succeeds, the values of a and b are printed
    [/logic]

  12. #12
    Registered User
    Join Date
    Mar 2010
    Location
    bbsr,odisha,india
    Posts
    27
    Quote Originally Posted by magestrium View Post
    when u write a==b in for the compiler checks for 1==5 which is not true hence the program terminates and no output just blank space.
    i want to accomplish the following by a for loop:
    [logic]
    two variables a and b have values 1 and 5 respectively.
    both are incremented and decremented the same time.
    the computer first checks whether 1==5, since it fails, the loop is run again
    this time it checks whether 2==4,since it fails, the loop is run again
    this time it checks whether 3==3,since it succeeds, the values of a and b are printed
    [/logic]

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be easier to just compute the arithmetic mean. Furthermore, what happens if one number is even and the other number is odd?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by suryap.kv1bbsr View Post
    i know that there are other ways of writing the program but i want to accomplish the following by a for loop:
    [logic]
    two variables a and b have values 1 and 5 respectively.
    both are incremented and decremented the same time.
    the computer first checks whether 1==5, since it fails, the loop is run again
    this time it checks whether 2==4,since it fails, the loop is run again
    this time it checks whether 3==3,since it succeeds, the values of a and b are printed
    [/logic]
    it means you want you loop run while a != b...


    and print the value of a when loop exits...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Mar 2010
    Location
    bbsr,odisha,india
    Posts
    27
    Quote Originally Posted by vart View Post
    it means you want you loop run while a != b...


    and print the value of a when loop exits...
    thankyou very much you are great!!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Using inFile to read from TXT yields gibberish
    By Zzaacchh in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2008, 07:59 PM
  3. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM