Thread: Basic For

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Basic For

    This question must be silly though...
    I myself find it very silly...
    will this not work for multiple conditions
    I am really puzzled at this one
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
       int i,j;
       for(i=1;i<=5;i++)
       {
       for(j=1; j<=5 ,j>=i ;j++)
       {
        cout<<"\n"<<i<<"-"<<j;
        }
    }
    
    return 0;
    }
    will the comma in for loop not symbolise AND...
    Ofcourse i used AND explicitly...
    But i need to know why this is not working
    ---I may be wrong
    Last edited by dpp; 06-28-2009 at 11:29 PM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    The indentation is difficult to read. The comma is not equivalent to AND. Even if it was, that's a very strange condition; it looks like when i is 1 the j loop will run 5 times, if i is anything else though, the j loop will not run. I can't tell what you are trying to accomplish.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by dpp View Post
    This question must be silly though...
    I myself find it very silly...
    will this not work for multiple conditions
    I am really puzzled at this one
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
       int i,j;
       for(i=1;i<=5;i++)
       {
       for(j=1; j<=5 ,j>=i ;j++)
       {
        cout<<"\n"<<i<<"-"<<j;
        }
    }
    
    return 0;
    }
    will the comma in for loop not symbolise AND...
    Ofcourse i used AND explicitly...
    But i need to know why this is not working
    ---I may be wrong
    The comma operator is very much different than the AND. In your loop comma operator will be evaluated in left to right associativity. SO finally j>=i condition will only matter for the loop. For example if you initialize j=0 in the second for loop nothing will be printed.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    That was a dummy example to know the use of comma...

    All i need is:
    Code:
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
    {
    ------------------------------------------
    
    ---
    --
    
    }
    }
    The loops should only run for i<j...

    1.well i know i can give an 'if' condition explicatly to check that...
    But i don't need that...
    I need to do the checking in the loop condition itself

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    So where's the problem. Just put a condition in the first for loop i<j and initialize j at the definition place itself.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    what value will i initialise to 'j'...
    it has to loop through in the second for loop in the next step
    I dont get it clearly...
    sorry

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    I did solve it using AND in the second for loop but..I need to know the optimistation without AND

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I think it'll depend on whether how many times you want the loop to work. What exactly do you want to do?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    Code:
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=5;j++)
    {
    if(i<j)
    cout<<i<<"-"<<j;
    }
    }
    I need to have the similar logic without explicit 'if' condition

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So you want j to be at least one larger than i?

    Code:
    for (i = 1; i <= 4; ++i) {
        for (j = i + 1; j <= 5; ++j) {
        ...
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a basic Linux web page designer (editor)
    By BobS0327 in forum Tech Board
    Replies: 5
    Last Post: 12-30-2006, 05:30 PM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM