280 likes | 438 Views
Chapter 4. Control Statements. Revisit Operators. ** -- used for raising to a power Negative exponent means use reciprocal, i.e. 3 -2 = 1/3 2 / -- used with integer produces an integer 5/3 = 2 Rem – used to find out remainder in integer division 5 Rem 3 = 2. General Formatting.
E N D
Chapter 4 Control Statements
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
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
Formatting Integers • Put(Num2); • No width given so gives enough space to print the largest integer possible!! • Put(Num2, width=>2); • Put(Num2, 2); • If width is too small will use as much space as needed • Alligns number on the right of the spaces
Formatting Floats • Put(Salary); • Defaults to exponential form, e.g. 3.45E2 • Put(Salary, fore=>3,aft=>2, exp=>0) • 345.00 • Put(Salary,3,2,0)
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)
Three Fundamental Control Structures • Sequence • Selection • Iteration (Repetition)
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)
Sequence Put_line(“Enter two integers”); Get(Num1); Get(Num2); Sum := Num1+Num2 Put(“The sum is ”); Put(sum);
Selection(Simple If then) If Num1 < 10 and Num2 < 10 Then Put(“The sum is less than 20”); End If; -- simple If statement If Age1 <= 12 or Age2 <= 12 Then Put(“At least one is a child”); End If;
Selection(If then else) If Age < 18 then Put(“Person cannot vote”); Else Put(“Person can vote”); End if;
Selection(use of Elsif – categorizing ages) If Age < 3 Then Put(“toddler”); Tod_count := Tod_count + 1; Elsif Age < 12 then Put(“child”); Child_count := Child_count + 1; Elsif Age < 20 then Put(“teen”); Teen_count := Teen_count + 1; Else Put(“Adult”); Adult_count := Adult_count + 1; End If;
Selection(Find smallest) Put_line(“Enter 3 different integers”); Get(A); Get(B); Get(C); If A<B and B<C then put(A); put(“ is the smallest”); Elsif B<A and B < C then put(B); put(“ is the smallest”); Else put(C); put(“ is the smallest”); End if;
Example Nested If • Let’s 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)
Code If A /= 0 And B /= 0 Then -- quadratic If B2 – 4 * A * C > 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 < 0 Then Put(“There are 2 complex number roots”); End If; Else Put(“This is not a quadratic equation”); End If;
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
Case statement Must be discrete expression (not float!) Case selector is When choice1 => Statements to execute When choice2 => Statements to execute When others => Statements to execute End case; Use when there are other possibilities!
Example: Case month_num is When 1..2 | 12 => Put(“Winter”); When 3..5 => Put(“Spring”); When 9|10|11 Put(“Autumn”); When others => Put(“Error in month number”); End case;
Another Example: Get(ch); Case ch is When ‘a’..’z’ | ‘A’..’Z’ => Alpha_count := alpha_count + 1; Put(“alphabet character”); When ‘0’..’9’ => Digit_count := digit_count + 1; Put(“digit character”); When others => Other_count := other_count + 1; Put(“other character”); End case;
Iteration Repetition in Ada
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;
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!
Looping • Executed until a certain condition is met Whileboolean expressionloop statements; End loop; While x /= -1 loop Put(x);New_line; Put(“Enter an integer – use –1 to stop ”); Get(x); End loop;
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;
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;
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;
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!)
Example: finding the average age of the class I could initialize these in declaration! 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!!!