Sunday, October 20, 2019 14:07

Interacting with the Console

This lesson will be based on things that we have not yet learned about, so it is not mandatory to fully comprehend it. However, at least a very basic understanding of the interaction with the Console is required, in order to continue explaining concepts in the following lessons.

The Console is the working area of a program that does not contain a Graphical User Interface (a program without buttons, text fields, images, etc). It is usually composed of a black window that may look like this:

console

With this kind of programs, we can only get or give input and output through text. Consequently, the interraction with the console is done only through text. The two main interractions with a console are input and output.

Input refers to the process of sending information to the program, using a keyboard. There are many ways in which we can perform input operations on a console program, but we are only interested in two, for now:

Console.Read() and Console.ReadLine() are both codes for reading text introduced by the user at the console. So, why two of them, and what is the difference between using one or the other? Console.Read() will only read a single character, a single key, or the first character of text, if we paste a longer text. On the other hand, Console.ReadLine() will read all the characters until the end of line, or a return character is detected (such as pressing the Enter key).

Output is the oposite of input, and basically means sending information from the program to the user, usually by displaying text on the console window. As in the case of input, there are many ways of performing this action, but again, as previously for the input part, we are only interested in two of them:

 

The difference between Console.Write() and Console.WriteLine() is the simple fact that the latter will make the carret jump to the next line after displaying the text we tell it to display. Console.Read() on the other hand, will display text on the same line, unless we specifically write a new line character in the text we send it to display.

As can be seen from the exemple above, we use Console.Write() and Console.WriteLine() by giving them some text arguments that we want them to print on the screen. These texts must be placed between double quotation marks, and the reason for it will be explained when we will learn about the string variable type. For now, we found out how to display text on the screen. However, what if we want to display the value of some variables? What if we want to display text AND the value of some variables?

Let’s take an example:

So, we declared three variables, containing some numeric values, such as age, weight or height. When we tried to display them on the console, all we needed to do was to use the plus sign in order to “add” or “join” them to the additional text that we wanted to display. This method of using the plus sign to merge one or more texts or variables together, in order to display them to the console, is called concatenation. You can notice that we delimited the texts that we wanted to display in front, at the end or between the variables, by using quotation marks. It is often challenging for beginners to comprehend the concept of using quotation marks, or to grasp which starting quotation mark belongs to what ending quotation mark, specially in multiple segments concatenation, like in my example, but in time it will get easier.

On the other hand, there is another way of displaying variable values mixed with some other custom text (like we displayed in the last example), which may seem more complicated at first, but its not; on the contrary. Here it is:

No, my age is not 0, my weight is not 1 kg and my height is definitely not 2 cm 🙂 .

Actually, {0}, {1} and {2} are called parameters, and as you can see, they are included INSIDE the text we display, as they are between the starting and the ending quotation marks. However, the compiler is smart enough not to display them as they are, as curly brackets with some numbers in between. Instead, it knows that for every such parameter, there is some value that we specify at the end of the text, separated by comma. For each parameter specified in the text, we must specify such a parameter at the end, or we will get an error. Please note that the parameters we specify inside the text start with 0, and not 1. In our example, we wanted to display three variable values, age, weight and height, therefor, we specified three parameters inside the text, {0}, {1} and {2}. At the same time, i said that for each parameter specified inside the text, we need to provide another parameter at the end of the text, separated by comma. Therefor, we added age, weight and height at the end, and these are actually the variables we declared in the lines above. When we execute our program, the compiler knows that it must replace each parameter between curly brackets with the parameters at the end of the text, in the order they appear separated by comma. More specifically, if we placed {0} after the text “my age is “, and we also specified variable age as the first parameter at the end of the text, it will replace the value of this variable for the {0} piece, weight for the {1} one and height for the {2} piece. If we were to put the variables in a different order, they would have still be displayed in consecutive order, starting from first.

To conclude this lesson, for those that want to play a bit more with the Console and want to customize the way they display stuff on the screen, there are some interesting ways of accomplishing this. For instance, we can set a background and foreground color for the text we want to display, like so:

The output of our console would now look a bit different:

Console colors

Comments

comments

Tags: ,

Leave a Reply