Thread: Logic of loop example in C tutorials

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    2

    Logic of loop example in C tutorials

    In this segment of code Alex explains it's purpose:- "This way, if one player can't take her turn, the game doesn't stop for everybody; we just skip her and keep going with the next player's turn. "

    I see this is implemented is lines 5 and 6, but what I don't understand exactly is what line 3 and 4 does.. mostly the conditional statement on line 3.

    HAAAAALP please. Thank you.
    Code:
    1  for (player = 1; someone_has_won == FALSE; player++)
    2  {
    3        if (player > total_number_of_players)
    4        {player = 1;}
    5        if (is_bankrupt(player))
    6        {continue;}
    7        take_turn(player);     
    8  }
    Last edited by midnightcoffee; 11-09-2013 at 09:09 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Notice that the "increment" expression in the loop is player++. That increments player by 1 each time, so first it's 1, then 2, then 3, ...then 42, then 43, then 44....

    Have you ever played monopoly with 44 people? Doubtful. So if total_number_of_players is 4, once player gets to 5, line 3 becomes true, line 4 is executed and player is reset back to 1.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    2
    Oh I didn't realize the int assigned to player was to distinguish each player from the others. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with the logic in my loop?
    By tranman1 in forum C Programming
    Replies: 4
    Last Post: 03-10-2010, 08:48 PM
  2. Problem in loop logic!
    By symbee in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2006, 12:58 AM
  3. need some tutorials other than C/C++
    By InvariantLoop in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 02-16-2005, 11:21 AM
  4. PLEASE help with loop, this has logic errors
    By cerealstone in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2003, 02:11 AM
  5. Win tutorials
    By frenchfry164 in forum Windows Programming
    Replies: 4
    Last Post: 01-20-2002, 09:54 PM