Flow control structures are fundamental aspects in any logic programming course, as they allow the creation of programs that can make decisions, that is, perform different actions depending on the circumstances. They are divided into three main types: sequential, decision, and repeat. In this chapter, we will focus on decision structures.

Decision structures, also known as conditional structures, are used to decide which block of code should be executed based on a condition. This condition is an expression that can evaluate to true or false. If the condition is true, a block of code is executed. If false, another block of code is executed.

The simplest example of a decision structure is the 'if' statement. The 'if' statement evaluates a condition and, if the condition is true, executes a block of code. Here is an example of what this might look like in pseudocode:

  if (condition) {
    // block of code to be executed if condition is true
  }

However, we often want something different to happen if the condition is false. For that, we can use the 'else' statement. The 'else' statement is used in conjunction with 'if' to execute a block of code if the condition is false. Here is an example:

  if (condition) {
    // block of code to be executed if condition is true
  } else {
    // block of code to be executed if the condition is false
  }

Sometimes we have multiple conditions to check. For that, we can use the 'else if' statement. 'Else if' is used after 'if' to check a new condition if the first condition is false. We can have as many 'else if' statements as we want. Here is an example:

  if (condition1) {
    // block of code to be executed if condition1 is true
  } else if (condition2) {
    // block of code to be executed if condition1 is false and condition2 is true
  } else {
    // block of code to be executed if both conditions are false
  }

Another important decision structure is the 'switch' statement. The 'switch' statement is used to select one of many blocks of code to be executed. It is a more readable and efficient alternative to a long string of 'if'-'else if'-'else' when we have many conditions to check. Here is an example:

  switch(expression){
    case value1:
      // block of code to be executed if expression equals value1
      break;
    case value2:
      // block of code to be executed if expression equals value2
      break;
    default:
      // block of code to be executed if the expression does not equal any of the values
  }

Decision flow control structures are fundamental in programming logic, as they allow our programs to make decisions and perform different actions depending on circumstances. In the next chapter, we'll explore looping flow control structures.

Now answer the exercise about the content:

What are the three main types of flow control structures in programming logic?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Flow control structures: repetition

Next page of the Free Ebook:

12Flow control structures: repetition

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text