We have seen that a conventional program is made up 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 that 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 create 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 part does. The important thing is to understand that both are a sequence of statements where:
- It defines the file name
- It reads the file
- It displays the content of the file 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 as a single statement for execution purposes.
A block is not just a matter of organizing code or making it look nicer. They are absolutely necessary in most languages.
The reason is that (as we will see in the section on control flow execution) there are many statements that allow us to say “do this” or “do that.” And in that “do this,” it accepts only a single statement.
Thanks to blocks, we can avoid that imposition and execute several lines of code, which for the compiler or interpreter forms a single cohesive unit.
In most programming languages, blocks are delimited by some kind of marker. Many inherit the syntax from C, and use 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 like this. 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 largest (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. It allows a series of statements to be seen as a single unit, from the perspective of the compiler or interpreter.