Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Conditional Chaining | Control Structures
course content

Зміст курсу

Introduction to GoLang

Conditional ChainingConditional Chaining

We can use the else if keyword to define an additional condition that will be evaluated in case the previous condition is not met:

go

index.go

In the above program, the expression 3 > 4 is checked first, which is false; hence the program proceeds to the next statement (3 > 2) defined using the else if keyword. The next condition is true, so the output displays the result of the second Println statement.

We can add as many additional conditions as needed using else if statements:

go

index.go

It's important to note that the above program only outputs 3 is greater than 2, even though the next condition is also true. This demonstrates that an if-else if chain stops evaluating conditions as soon as it encounters a true condition.

This process is referred to as conditional chaining because we are essentially defining conditions in a chain using if-else if keywords.

Note

In a conditional chain, only one code block is executed, the one that satisfies the condition, while the rest are skipped.

Alternatively, instead of chaining conditionals using the if-else if combination, we can write each condition using separate if statements. However, this approach produces a different output because it's no longer a single chain; it's a set of separate conditions, and therefore, it won't stop executing if one or more of the conditions are met:

go

index.go

Up to this point, for the sake of simplicity, we have been using values in boolean expressions. However, it is more common to use variables or a combination of variables and values in boolean expressions:

go

index.go

In a conditional chain, we can use the else keyword to indicate a code block that should be executed if none of the conditions in the chain are met:

go

index.go

question-icon

Which of the lines will be included in the output?

Виберіть кілька правильних відповідей

Все було зрозуміло?

Секція 3. Розділ 3
course content

Зміст курсу

Introduction to GoLang

Conditional ChainingConditional Chaining

We can use the else if keyword to define an additional condition that will be evaluated in case the previous condition is not met:

go

index.go

In the above program, the expression 3 > 4 is checked first, which is false; hence the program proceeds to the next statement (3 > 2) defined using the else if keyword. The next condition is true, so the output displays the result of the second Println statement.

We can add as many additional conditions as needed using else if statements:

go

index.go

It's important to note that the above program only outputs 3 is greater than 2, even though the next condition is also true. This demonstrates that an if-else if chain stops evaluating conditions as soon as it encounters a true condition.

This process is referred to as conditional chaining because we are essentially defining conditions in a chain using if-else if keywords.

Note

In a conditional chain, only one code block is executed, the one that satisfies the condition, while the rest are skipped.

Alternatively, instead of chaining conditionals using the if-else if combination, we can write each condition using separate if statements. However, this approach produces a different output because it's no longer a single chain; it's a set of separate conditions, and therefore, it won't stop executing if one or more of the conditions are met:

go

index.go

Up to this point, for the sake of simplicity, we have been using values in boolean expressions. However, it is more common to use variables or a combination of variables and values in boolean expressions:

go

index.go

In a conditional chain, we can use the else keyword to indicate a code block that should be executed if none of the conditions in the chain are met:

go

index.go

question-icon

Which of the lines will be included in the output?

Виберіть кілька правильних відповідей

Все було зрозуміло?

Секція 3. Розділ 3
some-alt