0
0
Goprogramming~15 mins

If statement in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
If Statement Basics in Go
๐Ÿ“– Scenario: You are creating a simple program to check if a number is positive or not. This is like checking if the temperature outside is above zero to decide if you need a jacket.
๐ŸŽฏ Goal: Build a Go program that uses an if statement to check if a number is positive and print a message accordingly.
๐Ÿ“‹ What You'll Learn
Create an integer variable named number with the value 10
Create a boolean variable named isPositive to store if the number is positive
Use an if statement to set isPositive to true if number is greater than 0
Print "The number is positive" if isPositive is true
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Checking conditions like this is common in programs that need to make decisions, such as validating user input or controlling game logic.
๐Ÿ’ผ Career
Understanding if statements is essential for any programming job because it helps you control the flow of your program based on different situations.
Progress0 / 4 steps
1
Create the number variable
Create an integer variable called number and set it to 10.
Go
Need a hint?

Use number := 10 to create the variable.

2
Create the isPositive variable
Create a boolean variable called isPositive and set it to false.
Go
Need a hint?

Use isPositive := false to create the variable.

3
Use if statement to check positivity
Use an if statement to check if number is greater than 0. Inside the if block, set isPositive to true.
Go
Need a hint?

Write if number > 0 { isPositive = true }.

4
Print the result
Use fmt.Println to print "The number is positive" only if isPositive is true. Remember to import fmt.
Go
Need a hint?

Use if isPositive { fmt.Println("The number is positive") }.