Thread: Simple for loop game engine please with break (simple question)

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Simple for loop game engine please with break (simple question)

    Code:
    // game_loop_test.c
    
    
    #include <stdio.h>
    
    
    int main()
    {
    	// Repeat block of three printf to console three times then break
    	
    	for (int tries = 1; tries < 3; tries++)
    	{
    		printf("Start Game: \n");
    		printf("Game Loop program");
    		printf("\n");
    
    
    		if (tries == 3)
    		{
    			break;
    		}
    		tries++;
    	}
    
    
    	return 0;
    }
    Please, how's everyone doing, this is a simple re-write of a game engine of a Hangman game game engine of a for loop of a longer program written in C. Please, I want the block of printf statements to loop and break once 3 is hit. Also, would even be better if the counter counted down from 3...2...1. (used decrement operator). Thanks.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    I think this is what you are looking for:
    Code:
    // game_loop_test.c
    
    #include <stdio.h>
    
    int main()
    {
        // Repeat block of three printf to console three times then break
    
        for (int tries = 3; tries > 0; tries--)
        {
            printf("Start Game: \n");
            printf("Game Loop program");
            printf("\n");
    
    /*       if (tries == 3) // NOT needed. The for() loop does this.
            {
                break;
            }
            tries++;
    */
        }
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple loop question
    By serge in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2012, 04:28 PM
  2. Stuck in simple while loop, can't break or return?
    By Adam Rinkleff in forum C Programming
    Replies: 6
    Last Post: 07-28-2011, 11:07 PM
  3. Simple loop question
    By TonyG in forum C Programming
    Replies: 2
    Last Post: 06-01-2011, 04:36 AM
  4. creating an AI engine for a simple game
    By cyb3r in forum Game Programming
    Replies: 1
    Last Post: 01-26-2009, 01:19 PM
  5. Simple Game Engine Programming
    By AoA in forum Tech Board
    Replies: 1
    Last Post: 05-26-2006, 12:50 AM

Tags for this Thread