Conditionally Executed
- Pages: 5
- Word count: 1063
- Category:
A limited time offer! Get a custom sample essay written according to your requirements urgent 3h delivery guaranteed
Order NowConditionally executed is called a single alternative decision structure because it provides only one alternative path of execution. The action is conditionally executed because it is performed only when a certain condition is true.
2. You need to test a condition and then execute one set of statements if the condition is true. If the condition is false, you need to execute a different set of statements. What structure will you use?
I will use the âIfâ statement because it is a dual alternative decision structure.
3. If you need to a test the value of a variable and use that value to determine which statement or set of statements to execute, which structure would be the most straightforward to use?
The case structure lets the value of a variable or an expression determine which path of execution the program will take.
4. Briefly describe how the AND operator works.
The AND operator takes two Boolean expressions as operands and creates a compound Boolean expression that is True only when both sub-expressions are true.
5. Briefly describe how the OR operator works.
The OR operator takes two Boolean expressions as operands and creates a compound Boolean expression that is true when either of the sub-expressions is true. The following is an example of an If-Then statement that uses the OR operator:
6.When determining whether a number is inside a range, which logical operator is it best to use?
The AND operator would be the best to use to determine whether or not a number is within a given range.
7. What is a flag and how does it work?
A flag is a Boolean variable that signals when some condition exists in the program. When the flag is set to False, it indicates the condition does not exist. When the flag variable is set to True, it means the condition does exist.
Algorithm Workbench
1. Design an If-Then statement (or a flowchart with a single alternative decision structure) that assigns 20 to the variable y and assigns 40 to the variable z if the variable x is greater than 100.
If x > 100
y = 20
z = 40
End If
2. Design an If-Then statement (or a flowchart with a single alternative decision structure) that assigns 0 to the variable b and assigns 1 to the variable c if the variable is a less than 10.
If a < 10 Then
b = 0
c = 1
End If
3. Design an If-Then-Else statement (or a flowchart with a dual alternative decision structure) that assigns 0 to the variable b if the variable a is less than 10. Otherwise, it should assign 99 to the variable b.
If a < 10
b = 0
Else If
b = 99
End If
4. The following pseudo code contains several nested If-Then-Else statements. Unfortunately, it was written without proper alignment and indentation. Rewrite the code and use the proper conventions of alignment and indentation.
If score < 60 Then
Display âYour grade is F.â
Else
If score < 70 Then
Display âYour grade is D.â
Else
If score < 80 Then
Display âYour grade is C.â
Else
If score < 90 Then
Display âYour grade is B.â
Else
Display âYour grade is A.â
End If
End If
End If
End If
5. Design nested decision structures that perform the following: If amount1 is greater than 10 and amount2 is less than 100, display the greater of amount1 and amount 2.
Main Mod
If (amount1 > 10) AND (amount2 < 100) Then
If amount1 > amount2 Then
Display “Amount 1 is greater than Amount 2”
Else
If amount2 > amount1 Then
Display “Amount 2 is greater than Amount 1”
Else
Display “Amount 1 is equal to Amount 2”
End If
End If
End If
End Main module
Programming Exercises
1. Roman Numerals
Design a program that prompts the user to enter a number within the range of 1 through 10. The program should display the Roman numeral version of that number. If the number is outside the range of 1 through 10, the program should display an error message.
Main Mod
Declare Integer
2. Areas of Rectangles
The area of a rectangle is the rectangleâs length times its width. Design a program that asks for the length and width of two rectangles. The program should tell the user which rectangle has the greater area, or if the areas are the same.
Main Module
Declare length1 as integer, length2 as Integer, width1 as integer,width2 as integer
Declare AreaOfFirstRectangle as integer, AreaOfSecondRectangle as integer
Display “Enter length of first rectangle”
Input length1
Display “Enter width of first rectangle”
Input width1
AreaOfFirstRectangle =length1 * width1
Display “Enter length of second rectangle”
Input length2
Display “Enter width of second rectangle”
Input width2
AreaOfSecondRectangle=length2 * width2
If AreaOfFirstRectangle > AreaOfSecondRectangle Then
Display “Areas of first rectangle is greater than area of second rectangle”
Else If AreaOfFirstRectangle < AreaOfSecondRectangle Then
Display “Areas of second rectangle is greater than area of first rectangle”
Else
Display “Areas of both rectangles are same”
End If
End Mod
3. Mass and Weight
Scientists measure an objectâs mass in kilogram and its weight in Newtons. If you know the amount of mass of an object, you can calculate its weight, in Newtons, with the following formula: Weight = Mass x 9.8
Design a program that asks the user to enter an objectâs mass, and then calculates its weight. If the object weighs more than 1000 Newtons, display a message indicating that it is too heavy. If the object weighs less than 10 Newtons, display a message indicating that it is too light.
Main Mod
Declare Integer newtons = 0
If newtons < 10 Then
Display “Too Light!”
Else If newtons > 1000 Then
Display “Too Heavy!â