This section contains the Golang conditional statements (if/else) find output programs (set 2) with their output and explanations.
Program 1:
package main
import "fmt"
func main() {
var num = 0
if true {
fmt.Println("Hello")
} else {
fmt.Println("Hiii")
}
}
Program 2:
package main
import "fmt"
func main() {
var num = 0
if (num == 0) == true {
fmt.Println("Hello")
} else {
fmt.Println("Hiii")
}
}
Program 3:
package main
import "fmt"
func main() {
var num1 = 10
var num2 = 20
var num3 = 30
var large = 0
if num1 > num2 && num1 > num3 {
large = num1
} else if num2 > num1 && num2 > num3 {
large = num2
} else {
large = num3
}
fmt.Println("Largest value is : ", large)
}
Program 4:
package main
import "fmt"
func main() {
var num1 = 10
var num2 = 20
var num3 = 30
var large = 0
if(num1 > num2){
if(num1 > num3)
large = num1
else
large = num3
}else if(num2 > num1){
if(num2 > num3)
large = num2
else
large = num3
}else{
large = num3
}
fmt.Println("Largest value is : ",large)
}
Program 5:
package main
import "fmt"
func main() {
var num1 = -10
if (num1 < 0) == true {
fmt.Println("Number is negative")
} else {
fmt.Println("Number is positive")
}
}
Answer Program 1:
Output:
Explanation:
The above program will generate a syntax error. Because we created an integer variable num, which was not used.
Answer Program 2:
Output:
Explanation:
In the above program, we created a variable num which was initialized with 0. Then we used the if condition to compare the value of num and also checked the result condition with Boolean value true and printed the "Hello" message.
Answer Program 3:
Output:
Explanation:
In the above program, we created four variables num1, num2, num3, and large which was initialized with 10, 20, 30 respectively. Then we found the largest number and printed the result on the console screen.
Answer Program 4:
Output:
Explanation:
The above program will generate a syntax error. Because we did not use curly braces "{ }" in nested if.
Answer Program 5:
Output:
Explanation:
In the above program, we created a variable num which was initialized with -10. Then we used the if condition to compare the value of num and also checked the result condition with Boolean value true and printed the "Number is negative" message.
need an explanation for this answer? contact us directly to get an explanation for this answer