Skip to content
Related Articles

Related Articles

Combining Conditional Statements in Golang

View Discussion
Improve Article
Save Article
  • Last Updated : 08 Jun, 2020
View Discussion
Improve Article
Save Article

Go is a open-source programming language developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is similar to C syntactically but with CSP style concurrency and many features of other robust programming language. Often refereed to as Golang because of the domain name, this language also has the If/else conditions. Usually the If/else/else if condition when written with one condition makes the program lengthy and increases the complexity thus we can combine two conditions. The conditions can be combined in the following ways :-

Using &&(AND) Operator

The And (&&) is a way of combining conditional statement has the following syntax:

if (condition1) && (condition2) {
......
}

Here, condition1 represents the first condition and condition2 represents the second condition. The && statement combines the conditions and gives the results in the following way:

  1. If condition1 is True and condition2 is also True, the result is True.
  2. If condition1 is True and condition2 is  False, the result is False.
  3. If condition1 is False and condition2 is  True, the result is False.
  4. If condition1 is False and condition2 is also False, the result is False.
     

Go




package main
  
import "fmt"
  
// AND Condition
func main() {
    time := 18
    if time > 10 && time < 18 {
        fmt.Println("Time for work")
    }
}

Using OR operator

The OR way of combining conditional statement has the following syntax:

if (condition1) || (condition2) {
.......
}

Here, condition1 represents the first condition and condition2 represents the second condition. The || statement combines the conditions and gives the results in the following way:

  1. If condition1 is True and condition2 is also True, the result is True.
  2. If condition1 is True and condition2 is  False, the result is True.
  3. If condition1 is False and condition2 is  True, the result is True.
  4. If condition1 is False and condition2 is also False, the result is False.
     

Go




package main
  
import "fmt"
  
// OR Condition
func main() {
    time := 14
    if time > 10 || time < 12 {
        fmt.Println("Hello geeks")
    }
}


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!