Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal.

Similar presentations


Presentation on theme: "Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal."— Presentation transcript:

1 Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal

2 State machines Lets take a look at the programs that we’ve written so far. We should realize that we haven’t been to strict about erroneous input. Our programs lack fundamental error handling. What are some standard ways for dealing with this problems?

3 The phone conversion program Given a phone number in the letter form, print it out using digits. (This is one of the problems from last time quiz). We need to design a robust program which can perform this task.

4 Defining the input format First thing to do is to enforce user input. There are many different ways to format phone number, so just pick one: (XXX)XXX-XXXX We have area code in parenthesis then three digits, dash, then four last digits.

5 Observe the details to extract states. (XXX)XXX-XXXX Area code - enclosed in () First three digits are followed by ‘-’ Finally four last digits. What are the states and what is a state machine?

6 State machine State machine is a part of the program, usually used to keep track of where we are in the certain process. For example, in the phone number problem, we can be reading the area code, which is one state, or we can be reading first three digits of a phone number which is a different state.

7 State transition diagram S0 S1 S2 S3 a b c If you are in state 0 and you see a transition to state 1 If you are in state 0 and you see b transition to state 2 If you are in state 0 and you see c transition to state 3

8 Parsers (essentially a ‘format- enforcer’) - Read next input character - Look in what state you are and what is the character that you read. Decide if this character is a valid input at the moment, figure out what is the next state. If the input was not valid, issue an error message.

9 Phone number parser (XXX)XXX-XXXX States: 1. Init (didn’t see any input) 2. Area code (reading area code) 3. First three digits (reading first three digits) 4. Last four digits (reading last four digits)

10 Phone number parser (XXX)XXX-XXXX What are the transitions? When we see ‘(‘ transition from State 1 to State 2 (Init ----> read area code) When we see ‘)’ transition from State 2 to State 3 ( Read area code --> read first 3 digits)

11 Phone number parser (XXX)XXX-XXXX What are the transitions? When we see ‘-‘ transition from State 3 to State 4 ( Read first 3 digits --> read last four digits) The characters ‘(‘, ‘)’ and ‘-’ are signal characters that help us identify transitions.

12 How do we code the state machine? State machines are usually encoded using case statement, in pseudo code: case iState of State1 : do something State2 : do something State3 : do something etc.

13 Why use state machines? They provide logical, methodical approaches to sequential input processing. It is easier to design state machine, then have a a lot of if-then-else statements. Lets try to design the state machine for the phone number problem.

14 Phone number example state machine Init Area code Last four Letters Input is ‘(‘ Input is digit and count <= 3 First three letters Input is ’)’ and count = 4 Input is digit and count <= 3 Input is ’-’ and count = 4 (XXX)XXX-XXXX Input is digit In every state, if the input is not correct issue the error and halt.

15 Optional (hard) problem for next time Write a program that converts letter phone numbers into digit phone numbers using the state machine that we discussed.

16 While loops We have already encountered construct in Pascal which are not necessary but convenient. For example case statement. Pascal provides two more for-loop-like constructs of which we will learn one.

17 Format While ( ) DO BEGIN … END; { of while loop }

18 While loop This simply allows us to perform set of operations while certain condition is true. Why is this useful? Lets see an example.

19 While loop is actually more powerful than for-loop Can we simulate while-loop with for-loop? Can we simulate for-loop with while loop?

20 While loop is actually more powerful than for-loop We can’t simulate while loop with for-loop because we will not know when we should terminate. On the other hand, we can simulate for-loop with while loop like this:

21 Simulating the for-loop with while loop For I := 1 to 10 do … I := 1; while ( I != 10 ) do begin … I := I + 1; end;

22 Home work Program 6 is due Monday, November 30 Read chapter 8, sections 1,2 ignore whatever you don’t understand. Attempt the optional problem (spend at least 1/2 hour thinking about it)

23 Program from this lecture Count words


Download ppt "Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal."

Similar presentations


Ads by Google