Please complete the following problems regarding if statements and loops and sub
ID: 3682300 • Letter: P
Question
Please complete the following problems regarding if statements and loops and submit by the end of class.
1. Write conditional expressions to perform the following tests:
a. When amountOwed is greater than 1000.00, display an overdue message.
b. When amountOfRain is greater than 5 inches, add 5 to total. When it is between 3 and 5 inches, add 3 to total. When it is less than 3 inches, add 1 to total.
---------------------------------------------------------------------------------------------------------------
2. What is the result of the following code segment?
int f = 7, s = 15;
f = s % 2;
if (f != 1) {
f = 0; s = 0; }
else if (f == 2) {
f = 10; s = 10; }
else {
f = 1; s = 1; }
Console.WriteLine(" " + f + " " + s);
---------------------------------------------------------------------------------------------------------------
3. Rewrite the following compound expression as a nested if statement:
if ((aValue > bValue) && (bValue == 10))
Console.WriteLine("Test complete");
---------------------------------------------------------------------------------------------------------------
4. Could a switch statement be designed logically to perform the same tests as the following nested if statement? If so, explain how it could be done
. if (aValue == 100) Console.WriteLine("Value is 100");
else
if (aValue < 100) Console.WriteLine("Value is less than 100");
---------------------------------------------------------------------------------------------------------------
Questions 5-8 refer to the following program segment.
int i = 0, g = 0, s = 0, t = 0, z = 0;
string sValue;
while (i < 5)
{ inValue = Console.ReadLine();
t = Convert.ToInt32(inValue);
s = s + t;
if (t > -1) g = g + 1; else z = z + 1; i = i + 1; }
5. How many times is the loop body of the while statement executed?
6. Describe the value stored in variable z at the end of the execution of the loop.
7. Categorize the loop (counter-controlled, sentinel-controlled, state-controlled, flag-controlled, etc.).
8. How many lines of output will be printed by the following fragment? What would they be?
for (int i = 0; i < 5; i++)
for (int j = 0; j < 4; j++)
Console.WriteLine("{0} {1}", i, j);
Explanation / Answer
1).
a.
double amountOwed;
if(amountOwed > 1000)
Console.WriteLine("Overdue: " +(amountOwed-1000));
b.
int total;
if(amountOfRain > 5)
total = total +5;
else if(amountOfRain>=3 && amountOfRain <=5)
total = total +3;
else if(amountOfRain <3)
total = total +1;
2) Ans 1 1
because int f = 7, s = 15;
f = s % 2; after this line value of f = 1
So, last else part will execute.
3).
Using nested if else
if(aValue>bValue){
if(bValue == 10)
Console.WriteLine("Test complete");
}
4. Yes It is possible to write using switch statements.
But you need to define 100 switch cases;
switch(aValue){
case 1:
case 2:
case 3:
............
............
..............
case 99:
Console.WriteLine("Value is less than 100");
break;
case 100:
Console.WriteLine("Value is 100");
}
5. loop will executes 5 times for 'i' value: 0, 1, 2, 3, 4
6. Since value of t is 0 in all loop iteration.
so, else part (z = z + 1), will always execute.
initial value of z = 0 and loop executes 5 times
so final value of z = 5
7. Since we are using 'i' as a counter (executes untill i value becomes 5),
so this loop is counter-controlled
8. for each value of i (from outer loop), inner loop will executes 4 times(j value)
since, i will take 5 values(0,1,2,3,4)
hence, total number of output lines = 5x4 = 20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.