for in an if and if yes how?

This is a discussion on for in an if and if yes how? within the C Programming forums, part of the General Programming Boards category; That subject line just rolls right over the tongue eh! Greetings! I would just like to know if I can ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Smile for in an if and if yes how?

    That subject line just rolls right over the tongue eh! Greetings!
    I would just like to know if I can use a "for" in an "if" statement. If yes then what is the correct method. What also about a "for" in an "if" inside a "while". Mostly I wonder about the braces and if the same rules would apply as if they were alone. ............ Thks all!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    while(1)
    {
           if (SomeBollux)
                {
                    for(i=0;i<SomeBollux;++i)
                        {
                                DoSomethingUseful();
                                DoSomethingElseUseful();
                         }
                }
           else
                 {
                    for(i=0;i<SomeOtherBollux;++i)
                         {
                               DoSomethingLessUseful();
                               DoSomethingDownrightUseless();
                          }
                  }
          if(GetOutOfWhileLoop) break;
    }
    Something like that?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Nesting is allowed, just keep you code laid out properly to help you design it, and don't nest too far, it will soon become too complex.

    Code:
        if (Something == true)
        {
            while (SomethingElse == true)
            {
                for (i = 0; i < 10; i++)
                {
                    /* Do stuff */
                }
            }
        }
    [edit]Goddammit, beaten again
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    23
    Thanks greatly All! I Completely understand now.

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