Console.WriteLine("This is printed verbatim (exactly as written here)"); int number; number = 10; Console.WriteLine(number);
This is printed verbatim (exactly as written here) 10 Press any key to continue . . .
Which line of the 'Relevant Program Code' produces the following output?
This is printed verbatim (exactly as written here)
Line 1 - Console.WriteLine("This is printed..
What output does the following line of 'Relevant Program Code' produce?
Console.WriteLine(number);
10
In general, if Console.WriteLine is told to print out a single 'thing' (whether that 'thing' is a string (text), a variable,etc), what does Console.WriteLine do?
It prints that thing to the screen, on it's own lineMain()
and then it was run (executed), what output would this section of code produce?Main()
) and then that method was run (executed), what output would this
section of code produce?int number; number = 10; Console.WriteLine("Number is: {0}", number); Console.WriteLine("Number is: {0} again: {0}", number);
Number is: 10 Number is: 10 again: 10 Press any key to continue . . .
What output does the following line of 'Relevant Program Code' produce?
Console.WriteLine("Number is: {0}", number);
Number is: 10
In your answer to your prior question, the {0}
does not appear in the output. What has it been
replaced with?
10, which is the VALUE of the variable 'number' (not the name of the variable - 'number')
What output does the following line of 'Relevant Program Code' produce?
Console.WriteLine("Number is: {0} again: {0}", number);
Number is: 10 again: 10
In the above code snippet, what does the {0}
mean? What does it do / what effect does it have
when used in the Console.WriteLine command?It replaces the {0} placeholder with the value of the variable
int number; number = 10; // Note the single-line declaration AND initialization // for otherNumber: int otherNumber = 20; // "declaration" = create the variable // "initialize" = give the variable it's first value Console.WriteLine("Number: {0} otherNumber: {1}", number, otherNumber); // swap numbers to print backwards Console.WriteLine("Number: {1} otherNumber: {0}", number, otherNumber);
Number: 10 otherNumber: 20 Number: 20 otherNumber: 10 Point is located at: (10, 20, 30) Press any key to continue . . .
What output does the following line of 'Relevant Program Code' produce?
Console.WriteLine("Number: {0} otherNumber: {1}", number, otherNumber);
Number: 10 otherNumber: 20
What output does the following line of 'Relevant Program Code' produce?
Console.WriteLine("Number: {1} otherNumber: {0}", number, otherNumber);
In the following line of 'Relevant Program Code', what does the {0}
refer to? What does the
{1}
refer to?
Console.WriteLine("Number: {1} otherNumber: {0}", number, otherNumber);
{0} refers to the value of number
; {1} refers to otherNumber
Describe how the number inside the curly braces ( { } ) relates to the things that follow that
first parameter to Console.WriteLine()
0 is the first item, 1 is the second, 2 is the third, etc
Predict the output of the following code:
int x = 10; int y = 20; int z = 30; Console.WriteLine("Point is located at: ({0}, {1}, {2})", x, y, z); Console.WriteLine("Point is located at: ({2}, {1}, {0})", x, y, z);Point is located at: (10, 20, 30)
If you saw code like the following, what do you think would happen?:
int x = 10; Console.WriteLine("x is: {0} y is: {20}", x );"It wouldn't work" - either crash, left blank, etc (C.WL will actually throw an exception)
Let's say that you have the following code:
double totalGuesses = 3; double numGuessesUsed = 0;
Furthermore, let's say that you wanted to print out the following message:
You have a total of 3 guesses, and you have tried to guess the secret number 0 times.
Write a line of code (that will be placed after the two lines of code listed above) that will print out a message like the above message (but with the current value of totalGuesses and numGuessesUsed.
Console.WriteLine("You have a total of {0} guesses, and you have tried to guess the secret number {1} times.", totalGuesses, numGuessesUsed);Building on your answer to the previous problem, write a different line of code that will still output the same message. Hint: Do this by rearranging the variables, and then fixing the rest of the line.
Console.WriteLine("You have a total of {1} guesses, and you have tried to guess the secret number {0} times.", numGuessesUsed, totalGuesses);Console.WriteLine("A1"); Console.WriteLine("B1"); Console.WriteLine("C1"); Console.WriteLine(); Console.WriteLine("Separator"); Console.WriteLine(); Console.Write("A2"); Console.Write("B2"); Console.Write("C2"); Console.WriteLine(); Console.WriteLine("End");
A1 B1 C1 Separator A2B2C2 End Press any key to continue . . .
What line of the 'Relevant Program Code' produces the line of output that reads A1
? Which line of code produces B1
?Which line of code produces C1
?
Briefly summarize what the Console.WriteLine method does, making sure to be clear about which lines it produces the output on
It FIRST prints the output THEN moves to the next lineWhy is there a blank line between the output C1
and Separator?
What line of the 'Relevant Program Code' produces the line of output that reads A2
? Which line of code produces B2
?Which line of code produces C2
?
Briefly summarize what the Console.Write method does, making sure to be clear about which lines it produces the output on
It FIRST prints the output and stays on that same lineWhat output would the following code produce?
Console.Write("A1"); Console.WriteLine("B1"); Console.Write("C1"); Console.WriteLine("D1");A1B1 on one line, C1D1 on the next
Write a single line of code that will produce the output Point is at (0, 1, 2)
, given the following code snippet.
Next, write several lines of code that will produce the same output
.
int x = 0, y = 1, z = 2;Console.WriteLine("Point is at ({0}, {1}, {2})", x, y, z); // make sure to watch for parens and commas
int number; number = 10; Console.WriteLine("Number is: + number"); Console.WriteLine("Number is: " + number); int otherNumber = 20; Console.WriteLine("Number is: " + number + " and: " + otherNumber); Console.WriteLine("Number is: " + number + number); Console.WriteLine(number + number); Console.WriteLine("Number is: " + (number + number)); int otherNumber = 20; // note single line Console.WriteLine("Number: " + number + "otherNumber: " + otherNumber); Console.WriteLine("Number: " + otherNumber + "otherNumber: " + number); // POSSIBLE ERROR int x = 10; int y = 20; int z = 30; Console.WriteLine($"Point is located at: (" + x + ", " + y + ", " + z + ")");
Number is: + number Number is: 10 Number is: 10 and: 20 Number is: 1010 20 Number is: 20
What line of the 'Relevant Program Code' produces the line of output that reads
Number is: + number
?
Which line of code produces
Number is: 10
?
How do the two lines (that you identified in the previous question) differ?
For the second, the + is outside the quotesWhat output does the following line of 'Relevant Program Code' produce?
Console.WriteLine("Number is: " + number + " and: " + otherNumber);
In general, what does the '+' symbol do?
It glues together two pieces of
information so that they're all displayed as one string
Look up the word 'concatenation'
Discuss how that concept
is being used in this program snippet.
The + symbol is concatenting the
variable onto the string to produce the output
What line of the 'Relevant Program Code' produces the line of output that reads
Number is: 1010
?
Which line of code produces
20
?
Which line of code produces
Number is: 20
?
Write a single line of code that will produce the output Point is at (0, 1, 2)
, given the following code snippet.
int x = 0, y = 1, z = 2;Console.WriteLine("Point is at: (" + x + ", " + y + ", " + z + ")");
"+ var +"
splif everywhere, or are
they easy to ignore? int number; number = 10; Console.WriteLine("Number is: {number}"); Console.WriteLine($"Number is: {number}"); int otherNumber = 20; Console.WriteLine($"Number is: {number} and: {otherNumber}"); Console.WriteLine($"Number is: {number + number}");
Number is: {number} Number is: 10 Number is: 10 and: 20 Number is: 20
What line of the 'Relevant Program Code' produces the line of output that reads
Number is: {number}
?
Which line of code produces
Number is: 10
?
How do the two lines (that you identified in the previous question) differ?
For the second, there is a $ before the first double-quotesWhat output does the following line of 'Relevant Program Code' produce?
Console.WriteLine($"Number is: {number} and: {otherNumber}");
What line of the 'Relevant Program Code' produces the line of output that reads
Number is: 20
?
In the 'string concatenation' approach, a common mistake is
to accidentally do string concatenation when one means to do
mathematical addition.
This mistake is much less common in
the 'string interpolation' approach.
Discuss within your
group the reason(s) why this is true.
Write a single line of code that will produce the output Point is at (0, 1, 2)
, given the following code snippet.
int x = 0, y = 1, z = 2;Console.WriteLine($"Point is located at: ({x}, {y}, {z})");