In: Computer Science
This is a question about C# programming:
True / False questions:
for (int i=5; i < 55; i+=5)
{ decimal decPrcPerGal = decGalPrc + i;
decPayPrc = iBuyGall * decPrcPerGal;
if (decPrcPerGal > decMAX_GALPRC) { continue; }
if (decPrcPerGal > decMAX_GALPRC) { break; }
Console.WriteLine(" {1} gallon(s) at {0,3} would cost
{2,8:$###,##0.00}", decPrcPerGal, iBuyGall, decPayPrc);
}
In the code above:
1. If decGalPrc is 10.57 decPrcPerGal will always be
some number of dollars and 57 cents True/ False?
2. In some cases, the loop’s code block will run several times
without showing anything on the console True/ False?
3. Removing only the code if (decPrcPerGal > decMAX_GALPRC) {
continue; } would change the output of the program
True/ False?
4. Removing only the code if (decPrcPerGal > decMAX_GALPRC) {
break; } would change the output of the program True/ False?
5. Removing the code if (decPrcPerGal > decMAX_GALPRC) {
continue; } Would allow the program to avoid performing some
unnecessary steps True/ False?
In case of any query do comment. Please rate answer Thanks.
Answer 1: True
Explanation: Because inside the loop, you are adding index i of the loop to decGalPrc to calculate decPrcPerGal where i is integer value only and this is the only place where you are calculating the value of decPrcPerGal. That is why answer is yes.
10.57 + 1 = 11.57
10.57 + 2 =12.57... so on
Answer 2: True
Explanation: When decPrcPerGal is greater than decMAX_GALPRC from first index only, then it will not print on console and will keep on running because of continue keyword.
Answer 3: False
Explanation: If you remove the code if (decPrcPerGal > decMAX_GALPRC) { continue; }, then output will not be changed in this program because next line which is having same condition but it is breaking the loop so you will not have any output again after the above condition met.
Answer 4: False
Explanation: If you remove the code if (decPrcPerGal > decMAX_GALPRC) { break; } , it would not affect the output of the program because this line of code was not reachable because of previous line of code where you are checking the same condition and when condition met you are continuing the loop.
Answer 5: True
Explanation: If you remove the code if (decPrcPerGal > decMAX_GALPRC) { continue; }, It will remove the unnecessary processing of the loop because in next line you are checking the same condition and breaking the loop.
E.g. if decMAX_GALPRC = 18 and decGalPrc =10
So after first iteration i =5, decPrcPerGal = 10 + 5 =11
in second iteration when i =10 decPrcPerGal = 10 + 10 = 20
if (decPrcPerGal > decMAX_GALPRC) { continue; }, code is not there it will break because of next line which is
if (decPrcPerGal > decMAX_GALPRC) { break; }