Thread: For Loop problem

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    For Loop problem

    Hi, this is probably a simple and question but can I use a For loop and inside it several Else If statement. Below is what I'm trying to do in general.

    For(0 < 10)

    If(String compare...==0)
    return()

    Else if(String compare...==1)
    return()

    Else if(String compare...==2)
    return()
    ...
    ...
    ...
    Else if(String compare...==9)
    return()

    Any Advice would be greatly appreciated.

    Thanks

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    yeah you can use if-else thingies multiple times inside for loop, but gotta use proper syntax.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    2
    thanks mate

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Use accolades to make clear what you mean...

    If you want to stop the loop whenever one of the 'if's' is satisfied, use the command 'break;'

    Code:
    for( int i=0; i<10; i++) {
    
      if(String compare...==0)
        break; // stops the loop
    
      else if(String compare...==1)
        break;
    
    }
    With so many if/else statements, you could consider a switch :

    http://www.cplusplus.com/doc/tutorial/control.html

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    11
    If you are using multiple if statements I think using the switch would be the best option. It would clean up the program and make it more efficient than multiple if statements

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM