Chapter 3 The General Structure of Ada Programs. General Form of an Ada Program With package1; With package2;... With packagen; Procedure pname IS - -

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chap-7 Robust_Input. A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Chapter 2: Introduction to C++.
Basic Input/Output and Variables Ethan Cerami New York
By Dr. Awad Khalil Computer Science & Engineering Department
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Chapter 2: Using Data.
Overview of C++ Chapter C++ Language Elements t Comments make a program easier to understand t // Used to signify a comment on a single line.
CPSC- 120 Principle of Computer Science I Computer = Hardware + Software.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Chapter 7 Other Loop forms, Procedures; Exception Handling.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chap 7- Control Structures : The WHILE Statement.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration.
Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename.
Chapter 2 Variables.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Variables and Arithmetic Operators in JavaScript.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 JavaScript: Introduction to Scripting
H:\>| WITH Ada.Text_IO; PROCEDURE Hello_Initials IS
Basic Elements of C++.
Variables and Arithmetic Operators in JavaScript
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Variables in C Topics Naming Variables Declaring Variables
Chapter 2: Basic Elements of Java
copyright © Michael B. Feldman, All Rights Reserved
Variables in C Topics Naming Variables Declaring Variables
CS150 Introduction to Computer Science 1
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
C:\> sort_3_numbers.exe |
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Chapter 3 The General Structure of Ada Programs

General Form of an Ada Program With package1; With package2;... With packagen; Procedure pname IS - - comments declaration of variables, constants, etc... Begin Program statement; Program statement;... Program statement; End;

With clause b To inform the compiler for a package used in the program b at least one “with” clause in Ada program b package for Text Input and Output WITH Ada.Text_IO; b package for Integer Text Input and Output WITH Ada.Integer_Text_IO; b package for Floating Text Input and Output WITH Ada.Float_Text_IO;

Identifiers or names b Begin with a letter b Consist of only letters, digits, and underscores b Cannot use two or more underscore characters in succession b Cannot use an Ada reserved word or pre-defined identifier as an identifier b Pick meaningful names for identifiers b no restriction on the length of an identifier

Type of data b Character b Integer b Float

Example-1 Displaying Initials Program WITH Ada.Text_IO; PROCEDURE Hello_Initials IS | Requests, then displays, user's first and last initials. --| Initial1 : Character; -- objects that hold initials Initial2 : Character;

Cont. of example-1 BEGIN -- Hello_Initials -- Prompt for (request user to enter) user's initials Ada.Text_IO.Put(Item => "Enter your two initials> "); Ada.Text_IO.Get(Item => Initial1); Ada.Text_IO.Get(Item => Initial2); -- Display user's initials, with a greeting Ada.Text_IO.Put(Item => "Hello "); Ada.Text_IO.Put(Item => Initial1); Ada.Text_IO.Put(Item => Initial2); Ada.Text_IO.Put(Item => ". Enjoy studying Ada!"); Ada.Text_IO.New_Line; END Hello_Initials;

Reserve Words and Identifiers b Reserve Words WithProcedureISBeginEND

b Predefined Identifiers Ada.Text_IOPutNew_LineCharacterGet

Input and Output b package for Text Input and Output WITH Ada.Text_IO; b package for Integer Text Input and Output WITH Ada.Integer_Text_IO; b package for Floating Text Input and Output WITH Ada.Float_Text_IO;

Read Float Number b To read a float number into identifier “Inches” WITH Ada.Float_Text_IO; Inches : Float; Ada.Float_Text._IO.Get ( Item => Inches);

Read Integer Number b To read an integer number into identifier “Age” WITH Ada.Integer_Text_IO; Age : Integer; Ada.Integer_Text._IO.Get ( Item => Age);

Read Character b To read a character into identifier “Initial1” WITH Ada.Text_IO; Initial1 : Character; Ada.Text._IO.Get ( Item => Initial1);

Write Float Number b To write a float number into identifier “Inches” WITH Ada.Float_Text_IO; Inches : Float; Ada.Float_Text._IO.Put ( Item => Inches);

Integer and Character output WITH Ada.Integer_Text_IO; Age : Integer; Ada.Integer_Text._IO.Put ( Item => Age); WITH Ada.Text_IO; Initial1 : Character; Ada.Text._IO.Put ( Item => Initial1);

Example-2 Displaying the User’s Name WITH Ada.Text_IO; PROCEDURE Hello_Name IS | Requests, then displays, user's name --| FirstName: String(1..10); -- object to hold user's name

Cont. of example 2 BEGIN -- Hello_Name -- Prompt for (request user to enter) user's name Ada.Text_IO.Put (Item => "Enter your first name, exactly 10 letters."); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => "Add spaces at the end if it's shorter.> "); Ada.Text_IO.Get(Item => FirstName); -- Display the entered name, with a greeting Ada.Text_IO.Put(Item => "Hello "); Ada.Text_IO.Put(Item => FirstName); Ada.Text_IO.Put(Item => ". Enjoy studying Ada!"); Ada.Text_IO.New_Line; END Hello_Name;

Constant Object Declaration Form some_constant : CONSTANT type := value; Example Pi : CONSTANT Float := ;

Assignment Statement Result_Variable := expression; Example X := Y + Z + 2.0; X := X +1;

Example-3 Converting Inches to Centimeters WITH Ada.Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Inch_to_CM IS | Converts inches to centimeters CMPerInch : CONSTANT Float := 2.54; Inches : Float; Centimeters : Float;

Cont. of example-3 BEGIN -- Inch_to_CM -- Prompt user for value in inches Ada.Text_IO.Put (Item => "Enter a length in inches> "); Ada.Float_Text_IO.Get (Item => Inches); -- Compute equivalent value in centimeters Centimeters := CMPerInch * Inches; -- Display result Ada.Text_IO.Put (Item => "That equals "); Ada.Float_Text_IO.Put (Item => Centimeters); Ada.Text_IO.Put (Item => " centimeters"); Ada.Text_IO.New_Line; END Inch_to_CM;

New Line Procedure Form Ada.Text_IO.New_Line ( Spacing => positive number); Example Ada.Text_IO.New_Line ( Spacing =>3);

Put Procedure ( Integer) with Width Ada.Integer_Text._IO.Put ( Item => variable, Width => field width); ( Item => variable, Width => field width);ExampleAda.Integer_Text._IO.Put ( Item =>How_Long, Width => 5); ( Item =>How_Long, Width => 5); See page 93 for more examples

Put Procedure ( Float) with Fore, Aft, Exp Ada.Float_Text._IO.Put ( Item => variable, Fore => n, Aft => n, Exp => n ); ( Item => variable, Fore => n, Aft => n, Exp => n );ExampleAda.Float_Text._IO.Put ( Item =>Centimeter, Fore => 5, Aft,=> 2, Exp => 0); ( Item =>Centimeter, Fore => 5, Aft,=> 2, Exp => 0); rather than E+01 See Page 94 for more examples

Data Types and Expressions b Integer Natural and Positive are subtypes of Integer.Natural and Positive are subtypes of Integer. REM operation 3 REM 2 is 13 REM 2 is 1 17 REM 3 is 217 REM 3 is 2 2 REM 3 is 22 REM 3 is 2

Exponentiation X ** 2 is 9 ( if X is 3 )X ** 2 is 9 ( if X is 3 ) X ** 3 is 27 ( if X is 3 )X ** 3 is 27 ( if X is 3 ) Y ** 2 is 1.44 ( if Y is 1.2 )Y ** 2 is 1.44 ( if Y is 1.2 ) Y ** 3 is ( if Y is 1.2)Y ** 3 is ( if Y is 1.2) Y ** 1.5 is not allowed. The power must be an integer.Y ** 1.5 is not allowed. The power must be an integer.

Float Literals b A Float literal is a number that begins with a digit contains a decimal point. Example E E+61.15E-32.3E2 But not E E E3

Expression Precedence Rules ( more in chap 8) b Parentheses, multiplication, division, addition and subtraction A.W := Z * X + Y; B.W := Z * ( X + Y) ; if X=1, Y=2 and Z= 3, then A will get the result “5” and B will be “9”.

Example -4 Coin Collection Program WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; PROCEDURE Coin_Collection IS | Finds the value of a coin collection, --| given pennies and nickels Pennies : Natural; -- input - number of pennies Nickels : Natural; -- input - number of nickels Dollars : Natural; -- output - value in dollars Cents : Natural; -- output - value in cents TotalCents : Natural;

Cont - 1 for Example 4 BEGIN -- Coin_Collection -- prompt user for number of nickels and pennies Ada.Text_IO.Put (Item => "How many nickels do you have? "); Ada.Integer_Text_IO.Get (Item => Nickels); Ada.Text_IO.Put (Item => "How many pennies do you have? "); Ada.Integer_Text_IO.Get (Item => Pennies); Ada.Text_IO.New_Line; -- compute total value in cents TotalCents := 5 * Nickels + Pennies; -- find the value in dollars and change Dollars := TotalCents / 100; Cents := TotalCents REM 100;

Cont - 2 for Example 4 -- display the value in dollars and change Ada.Text_IO.Put (Item => "Your collection is worth "); Ada.Integer_Text_IO.Put (Item => Dollars, Width => 1); Ada.Text_IO.Put (Item => " dollars and "); Ada.Integer_Text_IO.Put (Item => Cents, Width => 1); Ada.Text_IO.Put (" cents."); Ada.Text_IO.New_Line; END Coin_Collection;