Free Ebook cover Basic to Advanced Logic Programming Course

Basic to Advanced Logic Programming Course

New course

50 pages

Flow control structures: decision

Capítulo 11

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

  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.

The text states that flow control structures in programming logic are divided into three main types: sequential, decision, and repeat. This matches Option 2 which lists sequential, decision, and repeat as the main types of flow control structures.

Next chapter

Flow control structures: repetition

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.