Like Tree1Likes
  • 1 Post By anduril462

Using an if in a for loop

This is a discussion on Using an if in a for loop within the C Programming forums, part of the General Programming Boards category; Hi, everyone. I want to conditionally increment a value within a for loop, like this: Code: for (attempt = 1; ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Using an if in a for loop

    Hi, everyone. I want to conditionally increment a value within a for loop, like this:

    Code:
      for (attempt = 1; attempt <= uinumberofattempts || h->prefs.persevere;
           if (attempt) attempt++)
    However, using a statement instead of an expression is not allowed. This is what I've come up with. Is this acceptable or hacky? The voids are necessary to suppress clang compiler warnings about unused values.

    Code:
      for (attempt = 1; attempt <= uinumberofattempts || h->prefs.persevere;
           attempt ? (void) attempt++ : (void) 0)

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    attempt = 1;
    while (attempt <= uinumberofattempts)
      { 
    
         // do your stuff here
    
         // loop control
         if (!h->perfs.persevere)
           attempt++; }
    using... if (attempt) attempt++ ... does nothing. It just increments the counter since attempt is never 0 and if persever is true it will eventually overflow.
    Last edited by CommonTater; 04-07-2011 at 01:17 AM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    126
    Quote Originally Posted by CommonTater View Post
    Code:
    attempt = 1;
    while ((attempt <= uinumberofattempts) || (h-> perfs.persevere))
      { // do your stuff here
         if ( whatevercondition )
           attempt++; }
    using... if (attempt) attempt++ ... does nothing. It just increments the counter since attempt is never 0.
    My idea is that attempt will reach 0 when it overflows. Then it stays at 0 and lower down I have switch (attempt) that will print out "We lost count" instead of "%d", attempt.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Go back and look at my post... I did a little re-edit.

    Working like that it will *never* overflow.

    And FWIW .... when a signed int overflows, it does not necessarily overflow to 0... An overflow is undefined behavior, anything can happen.
    Last edited by CommonTater; 04-07-2011 at 01:25 AM.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    126
    Quote Originally Posted by CommonTater View Post
    Go back and look at my post... I did a little re-edit.

    Working like that it will *never* overflow.
    Ah, but I want it to count the attempts and print them to screen even if the 'persevere' option is set.

    And the attempt++ thing - I want it to be in the control statement because there are several 'continue's within the loop. This way I know it's going to execute every iteration.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok ... well, have fun with that.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Masterpiece

    Behold my masterpiece:

    Code:
      unsigned int attempt;
    
      for (attempt = 1; attempt <= uinumberofattempts || h->prefs.persevere;
           attempt ? (void) attempt++ : (void) 0)
        {
          switch (attempt)
    	{
    	case 0:
    	      printf ( "We've lost count of the number of tries.\n") 
    	  break;
    	case 1:
    	 printf ("Attempting to login to Wikipedia.\n");
    	  break;
    	default:
    	printf ( "Attempting to login to Wikipedia (try #%d).\n",
    	       attempt);
    	  break;
    	}
        }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And you win the award for the weirdest code I've ever seen.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Thank you

    Quote Originally Posted by CommonTater View Post
    And you win the award for the weirdest code I've ever seen.
    Thanks, Tater. I'm delighted that you have honoured me in this way.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Richardcavell View Post
    Thanks, Tater. I'm delighted that you have honoured me in this way.
    For the 4th month in a row....

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Code:
     for (attempt = 1; attempt <= uinumberofattempts || h->prefs.persevere;
           attempt += attempt > 0)
    And I assume 'attempt' would have a chance at getting set to zero somewhere in the loop.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Code:
     for (attempt = 1; attempt <= uinumberofattempts || h->prefs.persevere;
           attempt += attempt > 0)
    And I assume 'attempt' would have a chance at getting set to zero somewhere in the loop.
    That's the problem... from his previous posts, it would appear he intends to just let it overflow and hope it wraps around to 0.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Oh I understand now. And then it should just stay pinned at 0.

    Yeah it might work. Perhaps attempt += !!attempt
    Increment as long as it's non zero. That's kinda neat actually. Though limited in its utility.

    Quote Originally Posted by CommonTater View Post
    And FWIW .... when a signed int overflows, it does not necessarily overflow to 0... An overflow is undefined behavior, anything can happen.
    Gimme a break. Nothing will happen. Check the assembly code. I know of no machine architecture that generates an interrupt on overflow.

    You should be on a C standards committee. All academic cobbledygook and zero real-world common sense.
    Last edited by nonoob; 04-07-2011 at 10:22 AM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Gimme a break. Nothing will happen. Check the assembly code. I know of no machine architecture that generates an interrupt on overflow.
    Who said anything about interrupts?

    What happens when you overflow a signed int? Does it go to 0, -1, -INT_MAX ??? I should think that's pretty much implementation specific behavior which may not be the same from one machine to the next. That is... you can't trust it.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Why not just have the loop stop at INT_MAX? It's one number before overflow, and that hardly matters, since we're talking about 2 billion login attempts to Wikipedia, which will probably blacklist you as spam/DoS attack long before then.
    CommonTater likes this.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21