Go Conditional Statements

Go Conditional Statements

Kishore V


Go Conditional Statements

Conditional statements in Go allow your program to make decisions and execute different blocks of code based on specific conditions. They are a fundamental part of control flow and help create dynamic, responsive applications.

Understanding Conditions in Go

A condition is an expression that evaluates to either true or false. Go uses standard comparison and logical operators to build these expressions.

Comparison Operators

These operators are used to compare two values:

Operator Description Example
< Less than a < b
<= Less than or equal to a <= b
> Greater than a > b
>= Greater than or equal to a >= b
== Equal to a == b
!= Not equal to a != b

Logical Operators

Logical operators combine multiple conditions:

Operator Description Example
&& Logical AND (a > b) && (b < c)
|| Logical OR (a > b) || (b < c)
! Logical NOT !(a == b)

Types of Conditional Statements in Go

Go provides several ways to handle decision-making:

1. The if Statement

Use the if statement to execute a block of code only when a condition is true.

It's a hot day!

2. The if...else Statement

Use else to define an alternative block of code when the condition is false.

You are not eligible to vote yet.

3. The if...else if...else Ladder

Use else if to check multiple conditions sequentially.

Grade: B

4. The switch Statement

The switch statement is useful when you need to select one of many code blocks to execute.

Wednesday

Go if Statement

The if statement in Go is used to execute a block of code only when a specified condition evaluates to true. It is one of the most essential control flow structures and is widely used for decision-making in programs.

Syntax of the if Statement

if condition { // code executed when condition is true }

Key Rules:

  • The keyword must always be written in lowercase. Using If or IF will result in a compilation error.
  • The condition must evaluate to a boolean value (true or false).
  • Curly braces {} are mandatory, even for a single line of code.
  • Unlike some other languages, parentheses () around the condition are not required.

Example 1: Using if with a Direct Condition

In this example, we check whether a number is positive:

The number is positive

Example 2: Using if with Variables

You can also compare variables inside an if statement:

Length is greater than width

Example 3: Using if with Expressions

The condition can be a more complex expression involving calculations:

You passed the evaluation

Go else Statement

The else statement in Go is used alongside the if statement to define an alternative block of code that executes when the specified condition evaluates to false. It ensures your program can handle both outcomes of a decision effectively.

Syntax of if...else

if condition { // code executed when condition is true } else { // code executed when condition is false }

Important Notes:

  • The else block is optional but useful when you need a fallback action.
  • The condition must return a boolean value (true or false).
  • Parentheses () around the condition are not required in Go.
  • Curly braces {} are mandatory for both if and else blocks.

Example 1: Checking Time of Day

Good evening!

Explanation: Since the value of hour is 21, the condition hour < 18 evaluates to false. Therefore, the else block is executed.

Example 2: Comparing Temperature

It's a cool day

Explanation: The condition temperature >= 25 is false, so the program executes the else block.

Example 3: Even or Odd Number Check

The number is odd

Go else if Statement

The else if statement in Go allows you to evaluate multiple conditions sequentially. It is used when you need to test additional conditions after the initial if condition evaluates to false.

Syntax of if...else if...else

if condition1 { // executes if condition1 is true } else if condition2 { // executes if condition1 is false and condition2 is true } else { // executes if all above conditions are false }

How It Works:

  • Conditions are evaluated from top to bottom.
  • As soon as a condition evaluates to true, its corresponding block executes.
  • Once a match is found, the remaining conditions are not evaluated.
  • The else block is optional and acts as a fallback.

Example 1: Greeting Based on Time

Good evening!

Example 2: Comparing Two Numbers

Both numbers are equal

Example 3: Order of Conditions Matters

Value is at least 10

Explanation: Even though value >= 20 is also true, the first condition (value >= 10) is matched first. Therefore, only "Value is at least 10" is printed.


The Nested if Statement

You can have if statements inside other if statements. This is called a nested if.

Example: Nested if

Num is more than 10. Num is also more than 15.


Our website uses cookies to enhance your experience. Learn More
Accept !