Back

Activity 1.4: Output and Program Anatomy

divider

Activity 1.4

Output and Program Anatomy

Key Concepts

Statements

Strings

Comments

Nineteen Lines to Say Hello

Last session took nineteen lines of somebody else's code to put text on a screen.

You could change it. You could not have written it.

Today: one line, in an empty file.

One Line Is a Whole Program

Console.WriteLine("Wear gold.");
Terminal window
Wear gold.

No window, no scaffolding, nothing you did not type.

Read It Left to Right

Console.WriteLine("Wear gold.");

Console is the place. WriteLine is the action.

The semicolon ends the statement, the way a period ends a sentence.

The Quotes Are a Wall

Console.WriteLine("Wear gold.");

Inside the quotes is a string. The compiler does not read it.

Outside the quotes is code. The compiler reads every character.

Misspell the Code and It Stops

Console.WritLine("Wear gold.");
Terminal window
1-4-announcement.cs(1,9): error CS0117: 'Console' does not contain a definition for 'WritLine'
The build failed. Fix the build errors and run again.

The compiler names the exact word it does not recognize.

Misspell the String and It Ships

Console.WriteLine("Wer gold.");
Terminal window
Wer gold.

No error. No warning. The program is wrong and looks fine.

Only you can catch this one.

A Blank Line Is a Statement Too

Console.WriteLine();

Empty parentheses. Nothing to print, so it just ends the line.

Spacing is something you write on purpose, not something you hope for.

Write Stays. WriteLine Moves On.

Console.Write("Countdown: ");
Console.Write("3... ");
Console.Write("2... ");
Console.WriteLine("1... GO");
Terminal window
Countdown: 3... 2... 1... GO

Four statements, one line of output.

Comments Are for the Reader

// Banner
Console.WriteLine("========================================");

Two slashes, and the compiler ignores the rest of the line.

Ignored like the inside of a string — but for the opposite reason. This part is for people.

Comments Are for the Reader

/*
Friday night announcement
Three sections, in the order they print.
*/

/* opens a note and */ closes it.

Everything in between is ignored, however many lines that is.

Also an Off Switch

Console.WriteLine("Doors open at 6:00.");
// Console.WriteLine("Tip-off at 7:00.");
/*
Console.WriteLine("Student section: bring your ID.");
Console.WriteLine("Parking is free after 6:30.");
*/
Console.WriteLine("Wear gold.");
Terminal window
Doors open at 6:00.
Wear gold.

Five statements are in that file. Two of them ran.

Turning a line off is not the same as deleting it. You can get this one back.

Write the Comments First

// Banner
// Times and rules
// The countdown shares one line
// Sign-off

Four lines. Nothing there prints anything.

It is not a program. It is a plan, in order.

Write the Comments First

// Banner
// Times and rules
// The countdown shares one line
// Sign-off
Terminal window
error CS5001: Program does not contain a static 'Main' method suitable for an entry point
The build failed. Fix the build errors and run again.

The compiler agrees. It found nothing at all to run.

Then Fill In Underneath

// Banner
Console.WriteLine("=== FRIDAY NIGHT ===");
// Times and rules
Console.WriteLine("Doors open at 6:00.");
// The countdown shares one line
Console.Write("Countdown: ");
Console.WriteLine("1... GO");
// Sign-off
Console.WriteLine("Wear gold.");
Terminal window
=== FRIDAY NIGHT ===
Doors open at 6:00.
Countdown: 1... GO
Wear gold.

The plan stayed in the file. It is the comments.

Order Is the Only Plan

Statements run top to bottom, once each.

Move two lines and the announcement says something different.

Nothing else decides the order. Not today.

Today's Objectives

  • Writing Console.WriteLine statements from an empty file
  • Telling code apart from strings, comments, and punctuation
  • Controlling spacing with blank lines and Console.Write
  • Sequencing statements deliberately

Key Terms

Statement
One complete instruction, ended by a semicolon.
String
Text inside quotation marks. The compiler does not read what is in it.
Comment
A note for people, starting with two slashes. The compiler skips it.
Sequence
The order statements run in — top to bottom, once each.

'F' → Fullscreen

divider

Build

Create a new C# program named 1-4-announcement. Start it empty — delete anything the template gives you.

You are writing an announcement for an event: a game, a meet, a concert, a club night, a tournament. Pick your own. Every line of it is text you type.


Task 1: One Line From Nothing

  • Write a single statement that prints the name of your event.
  • Run it.
Output and Program Anatomy
Console.WriteLine("FRIDAY NIGHT - HOME GAME");
Output
FRIDAY NIGHT - HOME GAME

That is a complete program. One statement, no framework, and nothing on the screen you did not put there.


Task 2: Build the Announcement

  • Add a border line above and below your event name, made of the same character repeated.
  • Center your event name inside the border using spaces at the front of the string.
  • Add at least three lines of detail — times, place, what to bring.
  • Separate the banner from the details with a blank line, and use another before your last line.
  • Run it after every statement you add.

Count, do not guess, when you center the title. Both borders below are 40 characters and the title is 24, which leaves 16 to split — so 8 spaces go in front of it. Your numbers will be different, and the checkpoint asks how you worked yours out.

Output and Program Anatomy
Console.WriteLine("========================================");
Console.WriteLine(" FRIDAY NIGHT - HOME GAME");
Console.WriteLine("========================================");
Console.WriteLine();
Console.WriteLine("Doors open at 6:00.");
Console.WriteLine("Tip-off at 7:00.");
Console.WriteLine("Student section: bring your ID.");
Console.WriteLine();
Console.WriteLine("Wear gold.");
Output
========================================
FRIDAY NIGHT - HOME GAME
========================================
Doors open at 6:00.
Tip-off at 7:00.
Student section: bring your ID.
Wear gold.

Task 3: One Line, Four Statements

  • Add a countdown that appears on a single line of output.
  • Use Console.Write for every piece except the last one.
  • Make the last piece a Console.WriteLine, and work out why it has to be.

Try it with all four as WriteLine first. Look at what you get, then change them. Being shown the difference is worth less than seeing it once.

Output and Program Anatomy
Console.WriteLine("========================================");
Console.WriteLine(" FRIDAY NIGHT - HOME GAME");
Console.WriteLine("========================================");
Console.WriteLine();
Console.WriteLine("Doors open at 6:00.");
Console.WriteLine("Tip-off at 7:00.");
Console.WriteLine("Student section: bring your ID.");
Console.WriteLine();
Console.Write("Countdown: ");
Console.Write("3... ");
Console.Write("2... ");
Console.WriteLine("1... GO");
Console.WriteLine();
Console.WriteLine("Wear gold.");
Output
========================================
FRIDAY NIGHT - HOME GAME
========================================
Doors open at 6:00.
Tip-off at 7:00.
Student section: bring your ID.
Countdown: 3... 2... 1... GO
Wear gold.

Task 4: Two Kinds of Mistake

  • Misspell WriteLine on one statement. Run it.
  • Copy the error message down exactly, including the numbers in the parentheses.
  • Fix it, and confirm the program runs.
  • Now misspell a word inside the quotation marks. Run it.
  • Copy down exactly what the program printed.
  • Fix that too.

One of these two mistakes stopped the program. The other one did not. The difference is the most useful thing in this session, and the reflection asks you to explain it.


Task 5: Comments, and What Order Buys You

  • Add a comment above each section of your program saying what that section prints.
  • Put a /* */ note at the very top of the file with the name of your event and the sections in the order they print. Use more than one line — that is what the form is for.
  • Comment out one statement with //. Run it, and confirm that line of output is gone and there is no error. Then put it back.
  • Swap two of your detail lines. Run it. Read the announcement out loud and decide which order you actually want.
Output and Program Anatomy
/*
Friday night announcement
Banner, details, countdown, sign-off
*/
// Banner
Console.WriteLine("========================================");
Console.WriteLine(" FRIDAY NIGHT - HOME GAME");
Console.WriteLine("========================================");
Console.WriteLine();
// Times and rules
Console.WriteLine("Doors open at 6:00.");
Console.WriteLine("Tip-off at 7:00.");
Console.WriteLine("Student section: bring your ID.");
Console.WriteLine();
// The countdown shares one line
Console.Write("Countdown: ");
Console.Write("3... ");
Console.Write("2... ");
Console.WriteLine("1... GO");
Console.WriteLine();
// Sign-off
Console.WriteLine("Wear gold.");

A commented-out statement and a deleted statement behave identically. The difference is that you can get one of them back.


Challenge (Optional)

  • Make your border twice as long and re-center the title. Work out the new number of spaces before you run it.
  • Right-align your last line against the end of the border instead of the start.
  • Add a second event underneath the first, separated by a blank line, with its own banner.
  • Print a line that contains a quotation mark. This is genuinely tricky — find out why, and what fixes it.
divider

Checkpoint

Before you submit, check all six:

  • Your program started from an empty file.
  • Your output has a banner, at least three detail lines, and at least two blank lines.
  • Your title is centered inside your border, and you can say how you worked out the spaces.
  • Your countdown appears on one line of output.
  • Every section has a comment above it, and the file opens with a multi-line /* */ note.
  • You have the exact error message from the code misspelling, and the exact printed output from the string misspelling.

The last one is the one that cannot be reconstructed afterward. Both of those come from running the program, and neither can be worked out by reading it.

divider

Reflection

Answer the following questions before submitting your work.

  1. You misspelled WriteLine and the compiler stopped you. You misspelled a word inside quotation marks and it did not. Why can the compiler catch one and not the other?
  2. A comment and Console.WriteLine(); both look like they do nothing. Which one actually changes your output, and how can you tell?
  3. Explain how you worked out the number of spaces that centered your title. What would you change if your border were twice as long?
divider

Submit

Submit four things to the dropbox:

  1. Your 1-4-announcement program file.
  2. A copy of your program's output.
  3. The exact error message and the exact wrong output from Task 4.
  4. Your three reflection answers.

Activity Complete