3.7. Data Types: Conditional Structures

Página 10

3.7 Data Types: Conditional Structures

In programming, data types are fundamental and let programmers know what kind of value a variable can hold. However, for a program to be useful and functional, it is necessary that we can perform actions according to certain conditions. This is where conditional structures come into play.

Conditional Structures

Conditional statements, also known as flow control statements, allow programmers to define conditions under which certain blocks of code will be executed. They are a fundamental part of programming logic and are a concept that every beginning programmer needs to fully understand.

IF

The most common type of conditional structure is the IF statement. The IF statement executes a block of code if a specific condition is true. For example, if we want to print a message to the screen only if a variable is equal to 10, we could use the following structure:

if (variable == 10) {
  System.out.println("The variable is equal to 10");
}

If the variable is not equal to 10, the program will simply ignore the code block inside the IF statement and continue executing the rest of the code.

ELSE

The ELSE statement is often used in conjunction with the IF statement. The ELSE statement allows us to define a block of code that will be executed if the condition of the IF statement is false. In the previous example, if we wanted to print a different message when the variable is not equal to 10, we could do the following:

if (variable == 10) {
  System.out.println("The variable is equal to 10");
} else {
  System.out.println("The variable is not equal to 10");
}

ELSE IF

In some cases, we may want to check multiple conditions in sequence. For this, we can use the ELSE IF statement. ELSE IF allows us to define a new condition that will be checked if the condition of the IF statement is false. For example, if we want to check if a variable is equal to 10, equal to 20 or equal to 30, we could do the following:

if (variable == 10) {
  System.out.println("The variable is equal to 10");
} else if (variable == 20) {
  System.out.println("The variable is equal to 20");
} else if (variable == 30) {
  System.out.println("The variable is equal to 30");
} else {
  System.out.println("The variable is not equal to 10, 20 or 30");
}

SWITCH

The SWITCH instruction is an alternative to the IF / ELSE IF instruction sequence. The SWITCH statement allows us to check whether a variable is equal to a series of values. Each value is called a 'case' (or 'case') and the program executes the block of code corresponding to the first case that matches the value of the variable. For example:

switch(variable){
  case 10:
    System.out.println("The variable is equal to 10");
    break;
  case 20:
    System.out.println("The variable is equal to 20");
    break;
  case 30:
    System.out.println("The variable is equal to 30");
    break;
  default:
    System.out.println("The variable is not equal to 10, 20 or 30");
}

Conditional structures are a fundamental part of programming logic and allow us to create programs that can make decisions based on different conditions. Fully understanding how these frameworks work is a crucial step in becoming an effective programmer.

Now answer the exercise about the content:

Which of the following statements correctly describes the IF statement in programming?

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

You missed! Try again.

Next page of the Free Ebook:

113.8. Data Types: Repeating Structures

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or 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