#### Transcript Getting Started - Al Akhawayn University

```Lab 3
 Alternation

Multiple Alternative Decision

Nested If Statements
 Iteration

While Loop

For Loop

Do Loop
Note:
 Look at math.h (Appendix B AP13), and limits.h (Appendix B
AP19).
Alternation
Definition : Alternative Steps to take according to a particular
condition.
Exp1: You need to develop an algorithm to control the warning signs
at the exits of major tunnels.

If road_status is slick , you will have to check the temperature.
If It is higher than 0, you will display the message “Wet road”,
otherwise, you will display “Icy road”.

If the road is not slick, you want to display the message “Drive
carefully”.
Exp1 Solution
 Specification of the problem:
The problem’s objective is to display a warning message given the
 Analysis


Output: warning message.
Algorithm:
Start
{ Get temp
if (temp>0)
else
Endif }
Else
{display «Drive Carefully»}
Endif
End

Exp2: Write a condition that identifies single males between the ages
16 and 26 inclusive, and display the message “All Criteria Are Met”.
 Exp3: Implement the following decision table using nested if
statement. Assume that the wind speed is given as an integer.
Wind Speed (mph)
Category
below 25
not a strong wind
25-38
strong wind
39-54
gale
55-72
whole gale
above 72
hurricane
Exp2 Solution
If ((marital_status == ‘S’)|| (marital_status == ‘s’))
{ if ((gender == ‘M’)|| (gender == ‘m’))
{ if ((age>=18)&&(age<=26))
display « All Criteria are Met»
endif}
endif
} endif
Exp3 Solution
 Specification of the problem:
The problem’s objective is to display the Wind’s Category according to
the Wind’s speed.
 Analysis

Input: Wind_speed.

Output: Category message.
Algorithm
Start
 Get Wind_speed

End
If (Wind_speed <=24))
{display « not a strong wind»}
Else if(Wind_speed <=38)
{display « strong wind»}
Else if(Wind_speed <=54)
{display « gale»}
Else if(Wind_speed <=72)
{display «whole gale»}
Else
{display «hurricane»}
Endif
Iteration
Definition : Certain steps that got repeated while a given condition is
true.
 While loop (variable number of times)
 For loop (fixed number of times)
 Do loop (variable number of times)
While Loop
While Loop Example
Write an algorithm that calculates the sum of a collection of exam
scores. (if the class is very large, the instructor may not know the
number of students who took the exam). The program should work
regardless of the class size (use the SENTINEL).
Exp4 Solution
Start
 SENTINEL=-99
 Sum=0
 Get score
 While (score != SENTINEL)
{ sum += score
diplay « Enter next score ( »Sentinel « to quit )»
Get score
} EndWhile
 Display « The sum of scores is » sum
End
For Loop
For Loop Example
 Compute n!
 Precondition: n is greater or equal to 0.
Exp5 Solution
 Specification of the problem:
The problem’s objective is to display the factorial of n.
 Analysis

Input : n.

Output: factorial.

Relevant formula: factorial = factorial * i
Algorithm
Start
 Get n
 factorial =1
 For(i=n;i>1;i--)
{factorial = factorial * i}
EndFor
 Display « The n factorial is » factorial
End
Do Loop (Post Loop)
Do Loop Example
 Compute n! as long as the user wants.
 Precondition: n is greater or equal to 0.
Exp5 Solution
 Specification of the problem:
The problem’s objective is to display the factorial of different n as long
as the user wishes.
 Analysis

Input : n, again.

Output: factorial.

Relevant formula: factorial = factorial * i
Solution
Start
do
{Get n
factorial =1
For(i=n;i>1;i--)
{factorial = factorial * i}
EndFor
Display « The n factorial is » factorial
Display « One more time ? (1 to continue – 0 to quit) »
Get again
} while (again==1)
End
Common Use of Do Loop
 Testing the input
 Do {

Display « enter n>0 »

Get n
} while (n<=0)
```