1 / 9

Basic statements

Basic statements. Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 8.3.1-8.3.2. Assignment. Basic attribute for imperative languages, although syntax differs: A := B Pascal, Ada

Télécharger la présentation

Basic statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 8.3.1-8.3.2

  2. Assignment Basic attribute for imperative languages, although syntax differs: A := B Pascal, Ada A = B C, FORTRAN, PL/I, Prolog, ML, SNOBOL4 MOVE B TO A COBOL A  B APL (SETQ A B) LISP Note that LISP, an applicative language, has an assignment. Most LISP programmers do not write purely applicative programs. C has multiple ways to assign: A=B, A++, ... • How many C assignment operations can you identify? Basic statements

  3. Explicit sequence controls Early languages used labels on statements and explicitly determined location of branches (early FORTRAN): IF (A.GT.7) GOTO 100 ... 100 CONTINUE  Explicit control This is the “goto” controversy of the early 1970s. Languages do not need explicit control and concept is not used or needed even if present. • C includes a break statement for exit out of a loop. It is a limited form of goto, that also has its problems with correctness. Basic statements

  4. Control structures Basic control structures: Programs need 4 basic structures: • A sequencing between statements • A conditional for choosing alternatives • A Loop construct • A function call for calling subroutines. (We consider subprograms elsewhere in this course.) Basic statements

  5. Sequencing Compound statement: Typical syntax: begin statement1; statement2; ... end; Execute each statement in sequence. Sometimes (e.g., C) { ... } used instead of begin ... end Basic statements

  6. Conditional The if statement is typical: if expression then statement1 else statement2 if expression then statement1 The expression is evaluated and if true, the then clause is executed, otherwise the else clause is executed. If there is no else clause, then the next statement is executed. • Since a begin statement is a statement, the then or else clause can be an arbitrary number of commands. As with everything else in C, its austere syntax for if is: if (expression) statement1 else statement2; Basic statements

  7. Iteration Four iterations are most common: while expression do statement; - Evaluate expression and if true execute statement. Then repeat process. Repeat statement until expression; - Execute statement and then evaluate expression. Quit if expression is now true. For loop - Specify a count of the number of times to execute a loop: • for I=1 to 10 do statement; • for(I=0;I<10; I++) statement; • perform statement 10 times; Indefinite iterations (Ada): loop exit when condition end loop; foreach $X(@arrayitem){statement} – Perl for(var x in arrayitem) statement; - Java Basic statements

  8. Case statement A form of multiway branch (similar to if): case Tag is when 0 => begin statement0 end; when 1 => begin statement1 end; when 2 => begin statement2 end; when others => begin statement3 end; end case Basic statements

  9. Implementation of case Basic statements

More Related