TYPE EQUIVALENCE 1) Coercion 2) Casting 3) Conversion.

Slides:



Advertisements
Similar presentations
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Intermediate Code Generation
Programming Languages and Paradigms
Statement-Level Control Structures
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
COMP205 IMPERATIVE LANGUAGES
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Control Flow C and Data Structures Baojian Hua
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
ISBN Chapter 8 Statement-Level Control Structures.
1) Fixed count loops 2) Variable count loops 3) Post-test Loops 4) Terminating a loop COMP205 IMPERATIVE LANGUAGES 11. PROGRAM CONSTRUCTS 2 (REPETITION)
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
DAT602 Database Application Development Lecture 5 JAVA Review.
Fundamentals of C and C++ Programming Control Structures and Functions.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Statement Level Flow of Control GOTOs and Other Weirdness Copyright © by Curt Hill.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Variables and Constants.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
W E E K F I V E Statement-Level Control Structures.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Programming Techniques
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Def: A control structure is a control statement and
COMP205 IMPERATIVE LANGUAGES
EGR 2261 Unit 4 Control Structures I: Selection
Expressions and Control Flow in JavaScript
Chapter 8: Control Structures
Imperative languages Most familiar: computation modifies store
Chapter 3: Understanding C# Language Fundamentals
Structured Program
Govt. Polytechnic,Dhangar
CSC215 Lecture Control Flow.
Chapter 4: Control Structures I (Selection)
Expressions and Assignment
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Chapter8: Statement-Level Control Structures April 9, 2019
C Language B. DHIVYA 17PCA140 II MCA.
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

TYPE EQUIVALENCE 1) Coercion 2) Casting 3) Conversion

Operators require their operands to be of a certain type (similarly expressions require their arguments to be of the same type). In some cases it may be appropriate, when a compiler finds an operand that is not quite of the “right” type to convert the operand so that it is of the right type. This is called coercion. C supports coercion, Ada does not. COERCION

It is sometimes necessary for a programmer to force a coercion. This is referred to as a cast. This is supported in C (but not Ada). Example: CASTS void main(void) { float j = 2.0; int i; i = (int) (j * 0.5); }

Where casting is not supported the most common alternative is explicit conversion. This is supported by Ada. Ada conversions have the following form: where T is the name of a numeric type and N has another numeric type. The result is of type T. The distinction between conversion and casting is that the latter relies on the compiler’s coercion mechanism to achieve the end result while a conversion obtains the desired result explicitly through a call to a conversion function. CONVERSION T(N)

1. Program Statements 2. Assignment 3. Sequences 4. Selection a) If statements b) Case statements COMP205 IMPERATIVE LANGUAGES 10. PROGRAM CONSTRUCTS 1 (SELECTION)

Statements are the commands in a language that perform actions and change the state. The state of a program is described by the values held by its variables at a specific point during its execution. Example statements: 1) Assignment statements, which change the values of variables. 2) Sequence statements, which define the flow of a program. 3) Selection statements, which define alternative courses of action. 4) Repetition statements, which define program loops. The last three are often referred to as program constructs. Statements are usually separated by some operator (symbol), in Ada and C this a ;. STATEMENTS

The value of a variable can always be modified using an assignment statement: The source of a value in an assignment is usually an expression referred to as a assignment expression. Some imperative languages support multiple assignment. ASSIGNMENT STATEMENTS i = 2;I := 2; I, J := 2; DESTINATIONASSIGNMENT OPERATOR SOURCE

Certain assignment statements such as: are so common that some imperative languages (but not Ada) provide a short hand notation for them: The operators are referred to as the post-increment and post-decrement operators. THE C POST-INCREMENT AND PRE-INCREMENT OPERATORS j = j+1; k = k-1 J := J+1; K := K-1; i++;i--;

PROGRAM CONSTRUCTS All programming language utilise program constructs. Program constructs are statements like any other. In imperative languages they are used to control the order (flow) in which statements are executed (or not executed). There are a number of recognised basic programming constructs that can be classified as follows: 1) Sequences 2) Selection 3) Repetition To which we can also add routine invocation.

SEQUENCES A sequence statement tells the processor which statement is to be executed next. By default, in imperative languages, this is the statement following the current statement (or the first statement in the program). If we wish to "jump" to some other statement we can use a goto statement. This was popular in the early days of computer programming (1950's to early 1960's). BASIC goto statement: Also encouraged in languages such as Fortran and Cobol. GOTO 400

GOTO STATEMENT CONSIDERED HARMFULL (Dijkstra 1968) goto statements make it difficult to determine the state of variables at a given point during processing. As a result errors are often obscure and difficult to locate. For this reason some imperative languages, for example Modula-2, have abandoned the goto statement. However there are still some legitimate reasons why a goto may be desirable, for example to facilitate error handling or to terminate a deeply nested sequence of loops. Both Ada and C support a goto statement but it is best avoided.

ROUTINE INVOCATION Routine (or procedure) invocation is particular to imperative languages. When a sequence of statements forms a conceptual unit about which it is possible and useful to think and reason in isolation it is convenient to encapsulate the sequence in a named routine (procedure) and to replace it with a procedure call in the original code. Unlike a goto statement, routine invocation guarantees that the flow of control will eventually return to the point from which the routine was called (the procedure call).

SELECTION A selection statement provides for selection between alternatives. We can identify two basic categories of selection construct: 1) If statements 2) Case statements To which we can add pattern matching although imperative languages do not usually support this (logic and some modern functional languages do).

IF-ELSE SELECTION STATEMENTS

IF-ELSE STATEMENTS An if statement, sometimes referred to as a conditional, can be used in two forms: 1) If X then Y 2) If X then Y else Z Where X is a Boolean expression (or a sequence of Boolean expressions) and Y and Z are program statements of some description. We can also identify a number of variations of the above which are particular to individual programming languages. For example: if... then...elseif... then... elseif... then... else...

IF-ELSE EXAMPLES if (x < 0) { y = -x; z = 10+x; } else { y = x; z = 10-x } if X < 0 then Y := -X; Z := 10+X; else Y := X; Z := 10-X; end if; CAda

if x < 0 then begin y:= -x; z:= 10+x; end; else begin y:= x; z:= 10-x end; IF (x < 0) THEN y := -x; z := 10+x ELSE y := x; z := 10-x END PascalModula-2

IF-ELSE SUMMARY

#include void main(void) { int number; char type[8]; scanf("%d",&number); if (number < 100) { if (number < 10) strcpy(type,"small"); else strcpy(type,"medium"); } else strcpy(type,"large"); printf("%d is %s\n",number,type); } NESTED “IF- ELSE” C EXAMPLE

CASE SELECTION STATEMENTS

CASE STATEMENTS Generally speaking an "if... then... else..." statement supports selection from only two alternatives; we can of course nest such statements, but it is usually more succinct to use a case statement. Case statements allow selection from many alternatives. Selections may be made according to: a) A distinct value of a given selector, b) An expression involving the selector, or c) A default value.

CASE STATEMENTS Cont. A selector must be of a discrete type (typically an integer, a character or an enumerated type). C only supports selection by distinct value or default (uses the keyword default ). Ada, in addition to selection by distinct value and default (keyword others ) supports selection according to: a) A number of Alternatives (each separated by a bar | ), and b) Ranges expressed using the.. operator. Neither Ada or C support selection through expressions involving the selector.

with CS_IO; use CS_IO; procedure CASE_EXAMPLE is SELECTOR : NATURAL ; begin PUT_LINE("Input selector"); GET(SELECTOR); case SELECTOR is when 0 => PUT_LINE("O value"); when 1..3 => PUT_LINE("range 1..3"); when 4 | 5 => PUT_LINE("4 or 5"); when others => PUT_LINE("other"); end case; end CASE_EXAMPLE; ADA CASE EXAMPLE 1) Single value 2) Range of values 3) Alternatives 4) Default

typedef enum {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } WEEK_T; void main(void) { WEEK_T day; printf("Input day of the week, 0 = Monday, Etc.\n"); scanf("%d",&day); switch (day) { case 0: case 1: case 2: case 3: case 4: printf("Week day\n"); break; case 5: printf("Saturday\n"); break; case 6: printf("Sunday\n"); break; default: printf("Error\n"); }} C CASE EXAMPLE 1) Range of values 2) Single values 3) Default

SUMMARY 1. Sequences 2. Selection a) If statements b) Case statements