W3Basic Logo

Go if else

The if-else is a conditional statement that runs a different set of code blocks depending on whether the expression evaluates to true or false.

package main

import "fmt"

func main() {
	var number int
	fmt.Print("Enter a number: ")
	fmt.Scanf("%d", &number)
	if number > 0 {
		fmt.Printf("%d is a positive number\n", number)
	} else {
		fmt.Printf("%d is a negative number\n", number)
	}
}

In the above example:

If the user enters a number greater than 0 (positive number), the if condition evaluates to true, and the code block inside the if statement gets executed.

Output

Enter a number: 5
5 is a positive number

If the user enters a number less than 0 (negative number) the if condition evaluates to false, and the code block inside the else statement gets executed.

Output

Enter a number: -40
-40 is a negative number

Simple Conditional Statements

In the previous chapter, we saw how to convert a temperature given in degrees Celsius by the user to degrees Fahrenheit. But what if the user enters an invalid temperature, such as -300 degrees Celsius?

Note: In thermodynamics, the lowest possible temperature is -273.15 degrees Celsius, which is known as absolute zero.

We need a way to execute a block of code only if a certain condition is true. This is where conditional statements come in.

The if statement

The if statement is the most basic form of a conditional statement.

if condition {
	// do something
}

The condition is a boolean expression, which is either true or false. For example, celcius < -273.15 is a boolean expression that evaluates to true if the temperature is below absolute zero.

The else statement

The else statement is used to execute a block of code if the condition of the if statement is false.

if condition {
	// do something
} else {
	// do something else
}

Improving the conversion program

We can thus improve our program as follows:

package main

import "fmt"

func main() {
	var celcius float64
	fmt.Print("Enter a temperature in Celsius: ")
	fmt.Scan(&celcius)
	if celcius < -273.15 {
		fmt.Println("The temperature cannot be lower than -273.15°C!")
	} else {
		fahrenheit := celcius*9/5 + 32
		fmt.Printf("%.2f°C = %.2f°F\n", celcius, fahrenheit)
	}
}

Output

Enter a temperature in Celsius: -300
The temperature cannot be lower than -273.15°C!
Enter a temperature in Celsius: 36
36.00°C = 96.80°F

The else if statement

There is one more form of conditional statement to see in this tutorial, which is the else if statement. It is used to add more conditions to an if statement.

if condition1 {
	// do something
} else if condition2 {
	// do something else
} else {
	// do something else
}

As an example, consider the situation where we want to print a message depending on the temperature. The core of the program would be the following:

package main

import "fmt"

func main() {
	var celcius float64
	fmt.Print("Enter a temperature in Celsius: ")
	fmt.Scan(&celcius)
	if celcius < -273.15 {
		fmt.Println("The temperature cannot be lower than -273.15°C!")
	} else if celcius < 0 {
		fmt.Println("Water is frozen (ice).")
	} else if celcius < 100 {
		fmt.Println("Water is liquid.")
	} else {
		fmt.Println("Water is gas (steam).")
	}

}

Output

Enter a temperature in Celsius: -10
Water is frozen (ice).

The Nested if statement

If you add an if statement inside another if statement, then it becomes a nested if statement.

if condition1 {
		// do something

		if condition2 {
			// do something else
		} else {
			// do something else
		}
	}

Let us look at an example. Here we have an outer if condition which evaluates the given marks as greater than or equal to 0. Once it evaluates to true it goes inside the if block and evaluates the inner if-else conditions.

package main

import "fmt"

func main() {
	var marks int
	fmt.Print("Enter the marks: ")
	fmt.Scan(&marks)
	if marks >= 0 {
		if marks > 35 && marks < 70 {
			fmt.Println("Grade is B ")
		} else if marks > 70 && marks < 100 {
			fmt.Println("Grade is A.")
		} else {
			fmt.Println("Grade is C.")
		}
	} else {
		fmt.Println("Enter a valid marks between 0 to 100 ")
	}
}

Output

Enter the marks: 77
Grade is A.

© 2023 W3Basic. All rights reserved.

Follow Us: