How to Check if two of three boolean variables are true in Golang?

Go ProgrammingServer Side ProgrammingProgramming

In this tutorial we are going to see that the two of three boolean variables are true or not in Golang. As there are only three variables then we can use the if else condition statements to check this thing.

Checking with if, elseif and else condition statements.

Algorithm

  • Step 1 − Declaring the boolean variables that we will compare later.

  • Step 2 − Initializing the variables with respective values.

  • Step 3 − Comparing the variables using if elseif and else ladder.

Example 1

In this example we will check with if, elseif and else condition statements.

package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the three variables that // we have see are true or not var numberOne, numberTwo, numberThree bool // initializing the variables numberOne = true numberTwo = false numberThree = true fmt.Println("Number one =",numberOne,"\nNumber Two =",numberTwo,"\nNumber Three=",numberThree) // using the if else if ladder we are checking all three // possibilities where two variables can be true if not // then we are having else condition to handle that if numberOne == true && numberTwo == true && numberThree == false { fmt.Println("Two of three variables are true.") } else if numberOne == true && numberTwo == false && numberThree == true { fmt.Println("Two of three variables are true.") } else if numberOne == false && numberTwo == true && numberThree == true { fmt.Println("Two of three variables are true.") } else { fmt.Println("Two of three variables are not true.") } }

Output

Number one = true
Number Two = false
Number Three = true
Two of three variables are true.

Explanation of Code

  • if numberOne == true && numberTwo == true && numberThree == false {} − In this line of code we are checking that the first and second variables are true or not if yes then printing the result.

  • else if numberOne == true && numberTwo == false && numberThree == true {} − In this line of code we are checking that the first and third variables are true or not if yes then printing the result.

  • else if numberOne == false && numberTwo == true && numberThree == true {} − In this line of code we are checking that the first and second variables are true or not if yes then printing the result.

  • else {} −If we had reached this else then we don’t have two variables with true value.

Checking with only if condition statement.

Algorithm

  • Step 1 − Declaring the boolean variables that we will compare later.

  • Step 2 − Initializing the variables with respective values.

  • Step 3 − Comparing the variables using if conditions.

Example 2

In this example we will check with only if condition statement.

package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the three variables that // we have see are true or not var numberOne, numberTwo, numberThree bool // declaring and initializing the count variable which will // store the count of the variables that are true var count int count = 0 // initializing the variables numberOne = true numberTwo = false numberThree = false fmt.Println("Number one =",numberOne,"\nNumber Two =",numberTwo,"\nNumber Three =",numberThree) // using the if condition to check the // first variable is true or not if numberOne == true { count++ } // using the if condition to check the // second variable is true or not if numberTwo == true { count++ } // using the if condition to check the // third variable is true or not if numberThree == true { count++ } // checking the count is two or not if count == 2 { fmt.Println("Two of three variables are true.") } else { fmt.Println("Two of three variables are not true.") } }

Output

Number one = true
Number Two = false
Number Three = false
Two of three variables are not true.

Explanation of Code

  • if numberOne == true { count++ } − In this line of code we are checking that the first variable is true or not if yes then increasing the count.

  • if numberTwo == true { count++ } − In this line of code we are checking that the second variable is true or not if yes then increasing the count.

  • if numberThree == true { count++ } − In this line of code we are checking that the third variable is true or not if yes then increasing the count.

  • if count == 2 {} - Checking the value of the count variable in the end.

Conclusion

These are the two ways to check if two of three boolean variables are true in Golang. To learn more about go you can explore these tutorials.

raja
Updated on 30-Sep-2022 07:52:32

Advertisements