Four short programs, all about decisions — Activities 1.11 through 1.13.
Three kinds of mistake show up in C#, the same ones from dc-arithmetic and dc-casting. A compiler error stops the program before it ever runs — nothing prints, not even a heading. A runtime error runs until it hits something it cannot recover from, so you get partial output and then a crash. A logic error runs cleanly to the end and gives the wrong answer.
Three of these four are logic errors, and that is the point of this set. Decisions are where C# stops being able to protect you. If you find yourself waiting for an error message on three of these, that is exactly the habit this challenge exists to break.
Two of these programs produce a compiler warning rather than an error — a message that appears, and then the program runs anyway. Say so when you spot one, and give its code. A warning you ignored is still a mistake the compiler told you about.
This should say whether a score passes. The score is 42, and it prints Pass. It prints Pass for every score.
int score = 42;bool isPassing = score >= 60;
if (isPassing = true){ Console.WriteLine("Pass");}else{ Console.WriteLine("Fail");}This should report a letter grade. A score of 95 prints D.
int score = 95;
if (score >= 60){ Console.WriteLine("D");}else if (score >= 70){ Console.WriteLine("C");}else if (score >= 80){ Console.WriteLine("B");}else if (score >= 90){ Console.WriteLine("A");}else{ Console.WriteLine("F");}Also: three of this program's five branches can never run, for any score at all. Name them, and say which single line is responsible.
This should count login attempts. It refuses to build, and nothing prints.
int attempts = 1;
if (attempts = 3){ Console.WriteLine("Locked out.");}else{ Console.WriteLine($"Attempt {attempts} of 3.");}Compare this one against Bug 1 before you write anything. The two programs contain the same mistake. Only one of them was caught. Explain what is different about them — that explanation is worth more than either fix.
This should charge shipping only on orders under $50. An order of exactly $50 is given free shipping — and then told it just missed it.
double total = 50;
if (total < 50){ Console.WriteLine("Shipping: $6.00");}else{ Console.WriteLine("Free shipping");}
if (total <= 50){ Console.WriteLine("You just missed free shipping!");}There are two separate things wrong here, and fixing only one leaves a program that still contradicts itself. Find both.
For each program, give:
Plus the two written answers:
Work them out by reading first. Running the code tells you that something is wrong; it rarely tells you why, and on the exam there is no computer.