Language: EN

programacion-sentencias-y-bloques

What are statements and blocks

We have seen that a conventional program is made up of a series of “lines”, which form our source code, or “the guts” of the program.

Speaking properly, in reality these lines are 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 make a program that reads a file and displays the content on the screen.

string filePath = "ruta_del_archivo.txt";   // file name
string fileContent = File.ReadAllText(filePath);  // read file
Console.WriteLine("The file content is:\n" + fileContent);  // display file
file_path = "ruta_del_archivo.txt"   # file name
with open(file_path, 'r') as file:   # read file
	file_content = file.read()
	print("The file content is:\n" + file_content) # display file

For now, don’t worry too much about the syntax of each of them, or what each thing does. The important thing is to understand that both are a sequence of statements where:

  • Define the name of the file
  • Read the file
  • Display the file content in the console

Our program (almost any program!) is simply that. A sequence of statements that are executed from beginning to end, performing the actions we have defined.

The types of available sequences 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 its execution.

A block is not only a matter of organizing code or making it look prettier. They are absolutely necessary in most languages.

The reason is that, as we will see in the section on flow control, there are many statements that allow us to say “do this” or “do this other thing”. And in that “do this” they accept a single statement.

Thanks to blocks, we can avoid that imposition and execute several lines of code, which for the compiler or interpreter form a single joint unit.

In most programming languages, blocks are delimited by some type of marker. Many inherit the syntax of 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 like that. 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 major (and almost only) representative is the language Python.

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 one, in the eyes of the compiler or interpreter.