DOC PREVIEW
MIT 16 01 - Introduction to Computers and Programming

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to Computers and Programming Lecture 4 Prof. I. K. Lundqvist Reading: B pp. 20-46 ; FK pp. 157-165, 245-255 Sept 10 2003 Recap (1/3) Context Clause Indicates that package with Ada.Text_Io; Ada.Text_Io is used by the program Program Heading Identifies Unified as the procedure Unified is name of the program Constant declaration Associates the constant, Tax : constant Float := 17.00; Star : constant Character := ‘*’; Tax, with the Float value 17.00 Variable declaration Declares a variable X : Float; object named X for Y : Integer := 42; storage of Integer valuesRecap (2/3) Assignment statement Computes the product of Distance := Speed * Time; Speed and Time and assigns it to Distance Input Statements Enters data into the Ada.Text_Io.Get character variable Initial (Item =>Initial); Input Statements … into the integer Ada.Integer_Text_Io.Get variable Age (Item => Age); Input Statements … into the float variable Ada.Float_Text_Io.Get PayRate (Item => PayRate); Recap (3/3) Output Statements Displays the value of Ada.Text_Io.Put (Item =>Initial); the character variable Initial Output Statements Ada.Integer_Text_Io.Put (Item =>HowMany, Width=>3); … integer variable HowMany, using five columns on the display Output Statements Ada.Float_Text_Io.Put (Item => GrossPay, Fore => 4, Aft => 2, Exp => 0); … float variable GrossPay using four columns before the decimal point and two columns after the decimal pointData types String type characters as a single unit of data Max_Str_Length : constant := 26; Alphabet, Response:String(1..Max_Str_Length); String Operations Alphabet := “abcdefghijklmnopqrstuvwxyz” Response := Alphabet; Alphabet(1..3) & Alphabet(26..26) Alphabet & “.”); • Assignment Put(Item => “The alphabet is “ & • Used when representing a sequence of – How many characters? – String (1 .. Maxlen); –Example: • Concatenation (&)Sub-strings – alphabet(10) 'j' alphabet(17) 'q' – alphabet(20..23) "tuvw" alphabet(4..9) "defghi" – response(1..4) := "FRED"; response "FREDefghijklmnopqrstuvwxyz" String I/O • Get(Item => A_String); • Get_Line(Item => A_String, Last => N); • Exact length needed – Get_Line • Variable length accepted • Returns string and length • Individual character: specify position • Slice: specify range of positions • Assign to compatible slice •Text_Io – Output: Put, Put_Line –GetControl Structures Selection statements statements • if-then, when a single action might be done • if-then-else, to decide between two possible actions • if-then-elsif, to decide between multiple actions , also for deciding between multiple actions if-then Statements – statement_before; if test then statement(s)_1; ; statement_after; statement_before test statement(s)_1 statement_after true false end if • Ada provides two types of selection – IF statements –Case statements • Statement form • Statement semanticsif-then-else Statements – statement_before; if test then statement(s)_1; else statement(s)_2; ; statement_after; statement_before test statement(s)_1 statement_after truefalse statement(s)_2 Multiple Selections – statement_before; if test_1 then statement(s)_1; elsif test_2 then statement(s)_2; else statement(s)_3; ; statement_after; end if end if • Statement form • Statement semantics • Statement formif_then_elsif Example (0/5) example was distributed in class today: bank.adb if_then_elsif Example (1/5) • – amount of money (positive integer only) in a bank account. It will then ask for the amount of money (integers greater than zero) to be withdrawn. – amount in the account, by more than $50, the program is to display a message that the transaction is refused, and the unchanged balance is displayed. – or equal to the amount in the account, the transaction is accepted and the new balance in the account is displayed. – amount in the account, by up to $50, the program is to accept the transaction and display the new balance, with a warning that the account is overdrawn. Problem specification A program is required which will ask the user for the If the amount to be withdrawn is greater than the If the amount of money to be withdrawn is less than If the amount to be withdrawn is greater than the • Resulting program of the followingif_then_elsif Example (2/5) • Decision table –A multiple alternative if may often be summarized by a decision table listing the alternatives Balance after Action withdrawal >= 0 Accept withdrawal >= -50 and < 0 Overdraft < -50 Refuse withdrawal if_then_elsif Example (3/5) 100 Enter the withdrawal 50 Accepted. Balance is 50 76 Enter the withdrawal 150 Refused! Balance is 76 50 Enter the withdrawal 75 Overdraft! Balance is -25 Enter balance of the account Enter balance of the account • Alternative user interfaces – Enter balance of the accountif_then_elsif Example (4/5) • 1. Get balance and withdrawal 2. Calculate resulting balance 3. then else if new balance between zero and overdraft limit else Algorithm If new balance is >= zero 1. Get balance 2. Get withdrawal 1. New balance = old balance – withdrawal 1. Indicate transaction accepted 2. Indicate overdraft is used 3. Indicate transaction rejected if_then_elsif Example (5/5) • Data design NAME TYPE Notes Overdraft_Limit Integer -50 (for ease of change) Zero Integer 0 (for readability only) Balance Integer Balance in the account Withdrawal Integer Amount requested by user Resulting_Balance Integer Balance after withdrawalConditions TRUENOT(FALSE) FALSENOT(TRUE) Conditions Examples or (sex = 'F') • not ( (age >= 18) and (sex = 'M')) and (sex = 'F')) or ((age >= 65) and (sex = 'M')) • :ULWLQJ FRQGLWLRQV DQG WKHLU DVVRFLDWHG DFWLRQV FRUUHFWO\ FDQ EH WULFN\ O FDQ KHOS \RX PDNH VXUH WKH FRQGLWLRQV DQG DVVRFLDWHG DFWLRQV DUH FRUUHFW 7UXWK WDE HV •NOT •AND •OR •XOR • (age < 18) • ((age >= 60) F or F F F or T T T or F T T or T T F and F F F and T F T and F F T and T T F xor F F F xor T T T xor F T T xor T FTruth Tables • Nested if statements – if test_1 then if test_2 then statement(s)_1; else statement(s)_2; end if; else if test_3 then statement(s)_3; else statement(s)_4; end if; end if; test_1 test_2 test_3 s_1 s_2 s_3 s_4 F F F * F F T * F T F * F T T * T F F * T F T * T T F * T T T * Control Structures Loop Statements • Definite iteration is where the set of actions is performed a known


View Full Document

MIT 16 01 - Introduction to Computers and Programming

Documents in this Course
Fluids

Fluids

3 pages

Fluids

Fluids

4 pages

Fluids

Fluids

4 pages

Fluids

Fluids

5 pages

Load more
Download Introduction to Computers and Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Computers and Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Computers and Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?