Thread: for cycle with fixed data

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    3

    for cycle with fixed data

    is there some way to elaborate this line so that it works
    Code:
    //------------------------------------------------------------------------------
    int main()
    {
    int aaa  ;
    for (aaa=0 ; aaa=3 , aaa=6 ,aaa=9 ;aaa+++)
    {
    printf("\n %d \n", aaa);
    }
        return 0;
    }
    
    //----------------------------------------------------------------------------

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    Oh... there is ONE error: The expression a+++ is wrong. Everything else "works".

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    3
    Quote Originally Posted by flp1969 View Post
    Oh... there is ONE error: The expression a+++ is wrong. Everything else "works".

    .
    Code:
    int main()
    {
    int aaa  ;
    for (; aaa=3 , aaa=6 , aaa=9 ;)
    {
    printf("\n %d \n", aaa);
    }
        return 0;
    } 
    .


    strange to me print 9 alone boh!!!

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    3
    i mean something like that

    Code:
    [int main()
    {
    int aaa  ;
    for (aaa=0 ; aaa<=9 ; aaa++ )
    
    
    if (aaa==3 || aaa==6 || aaa ==9)
    {
    printf("\n %d \n", aaa);
    }
        return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    Maybe
    for ( aaa = 3 ; aaa <= 9 ; aaa += 3 )

    Cut out all the excess.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    Quote Originally Posted by ilkamalo View Post
    strange to me print 9 alone boh!!!
    I said it works, not if it's doing what you want...

    The for statement works with 3 arguments: initialization, condition to remain in loop and update:
    Code:
    for (init; cond; upd) { ... }
    .
    It is a simplified way to write:
    Code:
    init;
    while (cond)
    {
      ...
      upd;
    }
    What you are doing is:
    Code:
    // for (; aaa = 3, aaa = 6, aaa = 9;) ...
    while ( aaa=9 ) { printf( " %d\n", aaa ); }
    Since the comma operator will evaluate the sub-expressions from left to right, 'aaa=3, aaa=6' will be discarded. Note the '=' operator. When you do an assignment, the resulting value (9) is considered as true, so the loop never ends, and aaa will be always 9.

    It is not "strange". It is what you said the compiler to do.
    Last edited by flp1969; 03-18-2019 at 01:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Graph (contains cycle)
    By Siggi in forum C Programming
    Replies: 7
    Last Post: 05-08-2015, 01:40 PM
  2. Problem with while cycle
    By Lima in forum C Programming
    Replies: 5
    Last Post: 10-29-2010, 09:57 AM
  3. Cycle
    By Gordon in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2007, 10:58 AM
  4. Cycle counters
    By cosmo1996 in forum C Programming
    Replies: 3
    Last Post: 08-14-2007, 09:33 AM
  5. How to Cycle Through Structs
    By Grantyt3 in forum C++ Programming
    Replies: 13
    Last Post: 08-27-2005, 04:29 PM

Tags for this Thread