Control Statements - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Control Statements

Description:

Negative exponent means use reciprocal, i.e. 3-2 = 1/32 ... Display the 3 lengths & 'isosceles' else. Display the 3 lengths & 'scalene' end nested if ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 29
Provided by: Kim465
Category:

less

Transcript and Presenter's Notes

Title: Control Statements


1
Chapter 4
  • Control Statements

2
Revisit Operators
  • -- used for raising to a power
  • Negative exponent means use reciprocal, i.e. 3-2
    1/32
  • / -- used with integer produces an integer
  • 5/3 2
  • Rem used to find out remainder in integer
    division
  • 5 Rem 3 2

3
General Formatting
  • New_line
  • Moves cursor to the next line down
  • New_page
  • Moves cursor to the top of the next page
  • Set_col(20)
  • Moves cursor to column 20 on the present line
  • If already past column 20, will go to next line
    and column 20

4
Formatting Integers
  • Put(Num2)
  • No width given so gives enough space to print the
    largest integer possible!!
  • Put(Num2, widthgt2)
  • Put(Num2, 2)
  • If width is too small will use as much space as
    needed
  • Alligns number on the right of the spaces

5
Formatting Floats
  • Put(Salary)
  • Defaults to exponential form, e.g. 3.45E2
  • Put(Salary, foregt3,aftgt2, expgt0)
  • 345.00
  • Put(Salary,3,2,0)

6
Try these Remember order of operations!
  • 6 5 2 / 8 4 rem 3
  • 16 2.0
  • 3.0 16.0 / float(3) 2
  • 15 / integer(3.6)

7
Three Fundamental Control Structures
  • Sequence
  • Selection
  • Iteration (Repetition)

8
The Assignment Statement
  • Only one variable on left side of
  • Right side of is evaluated then assigned to
    the variable on leftExamples
  • My_Salary 3250.0
  • Commission 0.125 Sales
  • Name Kincade, Thomas
  • FirstName(1..6) Name(10..15)

9
Sequence
  • Put_line(Enter two integers)
  • Get(Num1)
  • Get(Num2)
  • Sum Num1Num2
  • Put(The sum is )
  • Put(sum)

10
Selection(Simple If then)
  • If Num1 lt 10 and Num2 lt 10 Then
  • Put(The sum is less than 20)
  • End If -- simple If statement
  • If Age1 lt 12 or Age2 lt 12 Then
  • Put(At least one is a child)
  • End If

11
Selection(If then else)
  • If Age lt 18 then
  • Put(Person cannot vote)
  • Else
  • Put(Person can vote)
  • End if

12
Selection(use of Elsif categorizing ages)
  • If Age lt 3 Then
  • Put(toddler)
  • Tod_count Tod_count 1
  • Elsif Age lt 12 then
  • Put(child)
  • Child_count Child_count 1
  • Elsif Age lt 20 then
  • Put(teen)
  • Teen_count Teen_count 1
  • Else
  • Put(Adult)
  • Adult_count Adult_count 1
  • End If

13
Selection(Find smallest)
  • Put_line(Enter 3 different integers)
  • Get(A) Get(B) Get(C)
  • If AltB and BltC then
  • put(A) put( is the smallest)
  • Elsif BltA and B lt C then
  • put(B) put( is the smallest)
  • Else
  • put(C) put( is the smallest)
  • End if

14
Example Nested If
  • Lets write an algorithm to tell how many roots
    of the equations exist
  • Remember quadratic equation
  • Ax2 Bx C 0
  • Assume A, B, C are given to the program
  • First rule out a constant or linear equation (A
    0 and B 0)
  • Then (if quadratic) begin checking the
    determinant (B2 4AC)

15
Code
  • If A / 0 And B / 0 Then -- quadratic
  • If B2 4 A C gt 0 Then
  • Put(There are 2 real roots)
  • ElsIf B2 4 A C 0 Then
  • Put(There is only one root)
  • Else
  • If B2 4 A C lt 0 Then
  • Put(There are 2 complex number roots)
  • End If
  • Else
  • Put(This is not a quadratic equation)
  • End If

16
Lab Problem
  • If sides make a triangle Then
  • If three sides are equal then
  • Display the 3 lengths equilateral
  • elseif two sides are equal then
  • Display the 3 lengths isosceles
  • else
  • Display the 3 lengths scalene
  • end nested if
  • Else
  • Display not a triangle
  • End the outer if

17
Case statement
Must be discrete expression (not float!)
  • Case selector is
  • When choice1 gt
  • Statements to execute
  • When choice2 gt
  • Statements to execute
  • When others gt
  • Statements to execute
  • End case

Use when there are other possibilities!
18
Example
  • Case month_num is
  • When 1..2 12 gt
  • Put(Winter)
  • When 3..5 gt
  • Put(Spring)
  • When 91011
  • Put(Autumn)
  • When others gt
  • Put(Error in month number)
  • End case

19
Another Example
  • Get(ch)
  • Case ch is
  • When a..z A..Z gt
  • Alpha_count alpha_count 1
  • Put(alphabet character)
  • When 0..9 gt
  • Digit_count digit_count 1
  • Put(digit character)
  • When others gt
  • Other_count other_count 1
  • Put(other character)
  • End case

20
Iteration
  • Repetition in Ada

21
Looping
  • Infinite loop
  • Loop
  • Put(hello there!)
  • End loop
  • Execute a fixed number of times
  • For J in 1..10 loop
  • Put(hello there!)
  • End loop

For J in reverse 1..10 loop Put(J) End loop
22
Looping
Do not declare this variable!
  • Execute a fixed number of times
  • For J in 1..10 loop
  • Put(hello there!)
  • End loop
  • For ch in character range a..z loop
  • Put(ch)
  • End loop

Note book is incorrect!
23
Looping
  • Executed until a certain condition is met
  • While boolean expression loop
  • statements
  • End loop
  • While x / -1 loop
  • Put(x)New_line
  • Put(Enter an integer use 1 to stop )
  • Get(x)
  • End loop

24
Looping
  • Using exit when statement
  • Must be within the loop
  • Loop
  • Put(x)New_line
  • Put(Enter an integer use 1 to stop )
  • Get(x)
  • Exit when x -1
  • End loop

25
Looping
  • Interactive loop
  • Initialize in declaration, e.g.
  • resp character y
  • Begin
  • Loop
  • Put(Enter an integer)
  • Get(N)
  • Put(Do you want to continue -- y or n)
  • Get(resp)
  • Exit When resp n or resp N
  • End Loop

26
Another Interactive loop
  • Initialize in declaration, e.g.
  • resp character y
  • Begin
  • While resp / n and resp / N Loop
  • Put(Enter an integer)
  • Get(N)
  • Put(Do you want to continue -- y or n)
  • Get(resp)
  • End Loop

27
Finding an Average What all do we need??
  • Counter (adds same number each time)
  • Accumulator (summing variable adds a different
    value to an accumulating sum)
  • Average is found by dividing Accumulator by
    Counter ---how accurate do you want?? (probably
    will want float!)

28
Example finding the average age of the class
  • Count 0
  • Sum 0
  • Resp y
  • Loop
  • Get(Age)
  • Count Count 1
  • Sum Sum Age
  • Put(any more ages? Y or N )
  • Get(Resp)
  • Exit when resp / Y or resp / y
  • End loop
  • Now I can find the average by dividing Sum by
    Count!!!

I could initialize these in declaration!
Write a Comment
User Comments (0)
About PowerShow.com