Slide: 1 Copyright © AdaCore Basic Types Presented by Quentin Ochem university.adacore.com.

Slides:



Advertisements
Similar presentations
Slide: 1 Copyright © AdaCore Record Types Presented by Quentin Ochem university.adacore.com.
Advertisements

Slide: 1 Copyright © AdaCore Packages Presented by Quentin Ochem university.adacore.com.
Slide: 1 Copyright © AdaCore Arrays Presented Quentin Ochem university.adacore.com.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Data Types.
Basic Elements of C++ Chapter 2.
Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC.
Variables and Constants
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Names Variables Type Checking Strong Typing Type Compatibility 1.
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CPS120: Introduction to Computer Science
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
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.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename.
Chapter 2 Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Introduction ABAP Fields and Variables. Slide 2 Fields (Introduction) In ABAP, fields (or data objects) are named locations in memory Variables store.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
A variable is a name for a value stored in memory.
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
Computer Programming BCT 1113
Tokens in C Keywords Identifiers Constants
Data Types, Arithmetic Operations
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 2 Variables.
Lectures on Numerical Methods
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Chapter 2 Variables.
Variables and Constants
Presentation transcript:

Slide: 1 Copyright © AdaCore Basic Types Presented by Quentin Ochem university.adacore.com

Slide: 2 Copyright © AdaCore A Few Syntactical Notes

Slide: 3 Copyright © AdaCore Identifiers Ada identifiers are case insensitive –HELLO = hello = HellO Start with a letter Ends with a letter or a number May contain non-consecutive underscores Which of the following are legal? –Something –My__Id –_Hello –A_67_9 –_CONSTANT –09_A_2 –YOP_

Slide: 4 Copyright © AdaCore Identifiers Ada identifiers are case insensitive –HELLO = hello = HellO Start with a letter Ends with a letter or a number May contain non-consecutive underscores Which of the following are legal? –Something –My__Id –_Hello –A_67_9 –_CONSTANT –09_A_2 –YOP_

Slide: 5 Copyright © AdaCore Comments Ada provides end of line comments with -- There is no block comment (/* */) -- This is an Ada comment// This is a C++ comment

Slide: 6 Copyright © AdaCore Numbers The underscore is allowed for numbers –1_000_000 = Numbers can be expressed with a base (from 2 to 16) –10#255# = 2#1111_1111# = 8#377# = 16#FF# Float literals must have a dot –With a digit before and after the dot. –1.0 /= 1

Slide: 7 Copyright © AdaCore Variable Declaration Defined by one (or several) names, followed by :, followed by type reference and possibly an initial value Elaboration is done sequentially Initialization is called for each variable individually “:=“ on a declaration is an initialization, not an assignment (special properties, mentioned later) A : Integer; B : Integer := 5; C : constant Integer := 78; D, E : Integer := F (5); int A; int B = 5; const int C = 78; int d = F (5), e = F(5); A : Integer := 5; B : Integer := A; C : Integer := D; -- COMPILATION ERROR D : Integer := 0; A, B : Float := Compute_New_Random; -- This is equivalent to: A : Float := Compute_New_Random; B : Float := Compute_New_Random;

Slide: 8 Copyright © AdaCore Simple Scalar Types

Slide: 9 Copyright © AdaCore Ada Strong Typing Types are at the base of the Ada model Semantic ≠ Representation Ada types are named Associated with properties (ranges, attributes…) and operators The compiler will warn in case of inconsistencies A : Integer := 10 * Integer (0.9); A : Integer := Integer (Float (10) * 0.9); int A = 10 * 0.9

Slide: 10 Copyright © AdaCore Ada Type Model Type Elementary Type AccessScalar Discrete EnumerationInteger Signed Modular Real Floating PointFixed Point Ordinary Decimal Composite Type Array Record Protected Task

Slide: 11 Copyright © AdaCore Ada Type Model Type Elementary Type AccessScalar Discrete EnumerationInteger Signed Modular Real Floating PointFixed Point Ordinary Decimal Composite Type Array Record Protected Task

Slide: 12 Copyright © AdaCore Scalar, Discrete and Real Types A scalar type is a “single” value –Integer, floating point, enumerations are all scalars Scalar types are divided between “discrete” types (that have a finite number of values) and “continuous” types Some scalar types are associated with numerical operations Scalar Discrete EnumerationInteger Signed Real Floating Point

Slide: 13 Copyright © AdaCore Standard Types Signed Integers –Short_Integer, Integer, Long_Integer, Long_Long_Integer Enumerations –Character, Boolean Floating Points –Short_Float, Float, Long_Float, Long_Long_Float

Slide: 14 Copyright © AdaCore Custom Types Declaration Integer types are defined by a range of values Enumeration types are defined by a list of values Float types are defined by a minimal number of significant (decimal) digits A range will decrease performances of floating point types type Score is range ; type Percentage is range ; type Distance is digits 10; type Temperature is digits 5 range _000_000.0; type Color is (Red, Green, Blue, Yellow, Black); type Ternary is (True, False, Unknown);

Slide: 15 Copyright © AdaCore Creating a Type from an Existing Type It is possible to create a new type based on an existing type The new type can restrict the range of values type Math_Score is new Score; type Math_Score is new Score range ; type Primary_Color is new Color range Red.. Blue;

Slide: 16 Copyright © AdaCore Type Conversion In certain cases, types can be converted from one to the other –They’re of the same structure –One is the derivation of the other Conversion needs to be explicit Conversion may introduce a verification V1 : Float := 0.0; V2 : Integer := Integer (V1); type T1 is range ; type T2 is range ; V1 : T1 := 0; V2 : T2 := T2 (V1); -- Run-time error!

Slide: 17 Copyright © AdaCore Ada Operators Common to Signed and Floating Point Types Specific to Signed Types =/=<<=>>=+-*/abs** modrem -11 mod 5 => 4-11 rem 5 => -1

Slide: 18 Copyright © AdaCore Attributes An attribute is a standard property of an Ada entity Accessed through ' Different set of attributes are available depending of the category of the type Example S : String := Integer'Image (42);

Slide: 19 Copyright © AdaCore Sample of Attributes Specific to all Scalar Scalar Discrete EnumerationInteger Signed Real Floating Point Attribute NameDocumentation FirstReturns the first value of the type LastReturns the last value of the type Image (X)Converts a value to its corresponding String Value (X)Converts a String to its corresponding value Min (X, Y)Returns the maximum of two values Max (X, Y)Returns the minimum of two values Pred (X)Returns the previous value Succ (X)Returns the next value RangeEquivalent of T’First..T’Last

Slide: 20 Copyright © AdaCore Sample of Attributes Specific to Discrete Types Scalar Discrete EnumerationInteger Signed Real Floating Point Attribute NameDocumentation Pos (X)Returns the position of the value in the type Val (X)Returns a value according to its position V : Character := Character’Val (0); W : Next_Character := Character’Val (Character’Pos (V) + 1);

Slide: 21 Copyright © AdaCore Sample of Attributes Specific to Floating Point Conversion Float to Integer is using Rounding, not Truncation! Scalar Discrete EnumerationInteger Signed Real Floating Point Attribute NameDocumentation Ceiling (X)Returns the smallest integral value after X Floor (X)Returns the largest integral value before X Truncation (X)Truncates towards 0 Rounding (X)Rounds to the closest integer Remainder (X, Y)Returns the remainder of the Euclidian division

Slide: 22 Copyright © AdaCore Quiz

Slide: 23 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (1/10) V : Integer := 7; V : Integer := V + 5;

Slide: 24 Copyright © AdaCore NO Is this correct? (1/10) V : Integer := 7; V : Integer := V + 5; Compilation error, V is already declared

Slide: 25 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (2/10) type N is range -2 ** ** 256;

Slide: 26 Copyright © AdaCore NO Is this correct? type N is range -2 ** ** 256; (2/10) This is likely to be to big on most systems today, so the compiler won’t be able to represent this and will issue an error.

Slide: 27 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (3/10) V : Float := 5.0;

Slide: 28 Copyright © AdaCore YES Is this correct? (3/10) V : Float := 5.0; No Error

Slide: 29 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (4/10) ClassRoom : constant Natural := 5; Next_ClassRoom : Natural := classroom + 1;

Slide: 30 Copyright © AdaCore YES Is this correct? (4/10) ClassRoom : constant Natural := 5; Next_ClassRoom : Natural := classroom + 1; No Error, Ada is case insensitive

Slide: 31 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (5/10) type T1 is new Integer range ; type T2 is new T1 range ;

Slide: 32 Copyright © AdaCore NO Is this correct? type T1 is new Integer range ; type T2 is new T1 range ; (5/10) A range cannot be extended, only reduced

Slide: 33 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (6/10) X : Float := Float'Succ (0.9);

Slide: 34 Copyright © AdaCore YES Is this correct? (6/10) Correct. Succ is available for floating point number, and will provide the closest floating point value above the representation of 0.9. X : Float := Float'Succ (0.9);

Slide: 35 Copyright © AdaCore What’s the output of this code? (7/10) F : Float := 7.6; Div : Integer := 10; begin F := Float (Integer (F) / Div); Put_Line (Float’Image (F));

Slide: 36 Copyright © AdaCore What’s the output of this code? (7/10) F : Float := 7.6; Div : Integer := 10; begin F := Float (Integer (F) / Div); Put_Line (Float’Image (F)); 0.0

Slide: 37 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (8/10) type T is (A, B, C); V1 : T := T'Val ("A"); V2 : T := T'Value (2);

Slide: 38 Copyright © AdaCore NO Is this correct? (8/10) type T is (A, B, C); V1 : T := T'Val ("A"); V2 : T := T'Value (2); Compilation errors. T’Val returns a value from a position T’Value returns a value from a String

Slide: 39 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (9/10) type T is (A, B, C); V1 : T := T'Value ("A"); V2 : T := T'Value ("a"); V3 : T := T'Value (" a ");

Slide: 40 Copyright © AdaCore YES Is this correct? (9/10) type T is (A, B, C); V1 : T := T'Value ("A"); V2 : T := T'Value ("a"); V3 : T := T'Value (" a "); No Error. Conversions are case-insentitive, automatically trimmed.

Slide: 41 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (10/10) type T is range 1.. 0; V : T;

Slide: 42 Copyright © AdaCore YES Is this correct? (10/10) type T is range 1.. 0; V : T; No Error. T has an empty range, useful in some circumstances.

Slide: 43 Copyright © AdaCore university.adacore.com