Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 64515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:48:07+00:00 2026-05-10T18:48:07+00:00

In what areas of programming would I use state machines ? Why ? How

  • 0

In what areas of programming would I use state machines ? Why ? How could I implement one ?

EDIT: please provide a practical example , if it’s not too much to ask .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:48:07+00:00Added an answer on May 10, 2026 at 6:48 pm

    In what areas of programming would I use a state machine?

    Use a state machine to represent a (real or logical) object that can exist in a limited number of conditions (‘states‘) and progresses from one state to the next according to a fixed set of rules.

    Why would I use a state machine?

    A state machine is often a very compact way to represent a set of complex rules and conditions, and to process various inputs. You’ll see state machines in embedded devices that have limited memory. Implemented well, a state machine is self-documenting because each logical state represents a physical condition. A state machine can be embodied in a tiny amount of code in comparison to its procedural equivalent and runs extremely efficiently. Moreover, the rules that govern state changes can often be stored as data in a table, providing a compact representation that can be easily maintained.

    How can I implement one?

    Trivial example:

    enum states {      // Define the states in the state machine.   NO_PIZZA,        // Exit state machine.   COUNT_PEOPLE,    // Ask user for # of people.   COUNT_SLICES,    // Ask user for # slices.   SERVE_PIZZA,     // Validate and serve.   EAT_PIZZA        // Task is complete. } STATE;  STATE state = COUNT_PEOPLE; int nPeople, nSlices, nSlicesPerPerson;  // Serve slices of pizza to people, so that each person gets /// the same number of slices.    while (state != NO_PIZZA)  {    switch (state)  {    case COUNT_PEOPLE:          if (promptForPeople(&nPeople))  // If input is valid..            state = COUNT_SLICES;       // .. go to next state..        break;                          // .. else remain in this state.    case COUNT_SLICES:          if (promptForSlices(&nSlices))           state = SERVE_PIZZA;         break;    case SERVE_PIZZA:        if (nSlices % nPeople != 0)    // Can't divide the pizza evenly.        {                                         getMorePizzaOrFriends();   // Do something about it.            state = COUNT_PEOPLE;      // Start over.        }        else        {            nSlicesPerPerson = nSlices/nPeople;            state = EAT_PIZZA;        }        break;    case EAT_PIZZA:        // etc...        state = NO_PIZZA;  // Exit the state machine.        break;    } // switch } // while 

    Notes:

    • The example uses a switch() with explicit case/break states for simplicity. In practice, a case will often ‘fall through’ to the next state.

    • For ease of maintaining a large state machine, the work done in each case can be encapsulated in a ‘worker’ function. Get any input at the top of the while(), pass it to the worker function, and check the return value of the worker to compute the next state.

    • For compactness, the entire switch() can be replaced with an array of function pointers. Each state is embodied by a function whose return value is a pointer to the next state. Warning: This can either simplify the state machine or render it totally unmaintainable, so consider the implementation carefully!

    • An embedded device may be implemented as a state machine that exits only on a catastrophic error, after which it performs a hard reset and re-enters the state machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Visual Studio 2010 Pro for simple C programming, I would like
I was programming a small notepad like app with some extra functionalities. I am
I'm new to Flash Pro programming, and am trying to create a button-type symbol
I am a UI Designer. My key areas of expertise are in XHTML, CSS
I'm newbie to Game programming. I'm trying to develop a simple shooting game. The
I've starting programming on ASP.NET MVC Framework a year ago. Recently. I've learning Ruby
I seem to have a problem, I have a growing business to run but
I've noticed the images in the products content type in ubercart shop have 2
He's the deal: I'm making some theorical research on .NET and Java platforms for
The scenario is this: My company has 2000 customers, and we need to send

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.