We have seen that a conventional program consists of a series of “lines”, which form our source code (“the guts” of the program).
Properly speaking, these lines are actually called statements. Statements are the basic unit of any program. They represent the actions the program must execute to achieve a certain result.
A statement can be as simple as assigning a value to a variable or as complex as reading the contents of a file, or making a request to a web page.
For example, let’s see how we would make a program that reads a file and displays its content on the screen.
string filePath = "file_path.txt"; // file name
string fileContent = File.ReadAllText(filePath); // read file
Console.WriteLine("The content of the file is:\n" + fileContent); // display file
file_path = "file_path.txt" # file name
with open(file_path, 'r') as file: # read file
file_content = file.read()
print("The content of the file is:\n" + file_content) # display file
For now, don’t worry too much about the syntax of each one, or what each thing does. The important thing is to understand that both are a sequence of statements where:
- Define the filename
- Read the file
- Display the file content on the console
Our program (almost any program!) is simply that. A sequence of statements that execute from beginning to end performing the actions we have defined.
The types of sequences available and their syntax vary from one programming language to another (although, in general, we will see that they are more similar than they seem at first).
What is a Block?
A block is a grouping of lines of code that is considered a single statement for execution purposes.
A block is not just a matter of code organization or making it look nicer. They are absolutely necessary in most languages.
The reason is that (as we will see in the section on execution flow control) there are many statements that allow us to say “do this” or “do that”. And in that “do this” they accept a single statement.
Thanks to blocks, we can avoid that imposition and execute multiple lines of code, which for the compiler or interpreter forms a single joint unit.
In most programming languages, blocks are delimited by some kind of marker. Many inherit the syntax from C, and use curly braces {} to delimit blocks.
For example, C, C++, C#, Java or JavaScript use braces as delimiters.
if (condition)
{
// this would be a block
let variable = 2;
console.log(variable);
}
But not all languages work that way. For example, VB uses
If x > 3 Then
' this would be a block
Dim variable As Integer = 2
Console.WriteLine(variable)
End If
On the other hand, some languages use indentation to delimit blocks. This is called significant indentation, and the main (and almost only) representative is the Python language.
if x > 3:
# this would be a block
variable = 2
print(variable)
In any case, the functionality of a block is the same in all languages. To allow a series of statements to be seen as a single one, in the eyes of the compiler or interpreter.
