Tuesday, June 21, 2011

Control structures - Go if else statement

Programs need to be able to take up different courses of action based on different situations - if you want to go to the beach turn left, or if you want to go the movies, turn right. The if else statement is a simple control structure. The general statement in Go is similar to:

if some_boolean_expression {
    // execute this block if some_boolean_expression is true
} else if alternate_boolean_expression {
    // execute this block if alternate_boolean_expression is true
} else {
    // if none of the other blocks are executed, then execute this block
}

A few notes:
* The value of the placeholder names some_boolean_expression and alternate_boolean_expression has to evaluate to one of the bool types: true or false.
* There are no parentheses required around the boolean expression - you can have a parentheses, but you don’t have to have it.
* A special quirk: Go requires the curly braces and the if else keywords to be on the same line with the corresponding braces. If not you will see errors like missing condition in if statement or syntax error: unexpected semicolon or newline before else.
* In case of multiple expressions evaluating to true, the first one encountered in lexical order has its corresponding block executed.

Note that types like an int cannot be used as truth values - this is unlike languages like C, where you can use integers and pointers as truth values.

You can have the explicit values like true or false, or you can also use any expression that evaluates to a true or false. So expressions that use the operators below to generate a truth or false value is permissible.


== equal to
!= not equal to
< less than 

<= less than or equal to 
> greater than
>= greater than or equal to
&& and
|| or


We shall do a few examples to illustrate. First one uses the literal bool values to see the result.

Full program
package main

import "fmt"

func main() {
    if true {
        fmt.Println("the true block is executed")
    } 

    if false { 
        fmt.Println("the false block won't be executed")
    } 
}

the true block is executed

One more simple example that uses some of the comparative operators.

Full program
package main

import "fmt"

func main() {
    a, b := 4, 5
    if a < b {
        fmt.Println("a is less than b")
    } else if a > b {
        fmt.Println("a is greater than b")
    } else {
        fmt.Println("a is equal to b")
    } 
}

a is less than b

The if-else statement is fairly straightforward to warrant a lengthy explanation beyond a couple of examples. What we will do however is list a few samples that show typical errors.

Partial code: error snippets
//error: non-bool 5 (type ideal) used as if condition
if 5 {  //an integer type does not evaluate to a bool by itself
}

//error: non-bool s (type string) used as if condition
var s string
if s { //a string type does not evaluate to a bool by itself
}

//error: non-bool e (type os.Error) used as if condition
var e os.Error
e = nil
if e { //nil value is not a true or false
}

//error: missing condition in if statement
if true  //the open brace needs to be on this line
{ }

//error: unexpected semicolon or newline before else
if true {
} // the else needs to be on this line itself
else {
}

1 comment:

If you think others also will find these tutorials useful, kindly "+1" it above and mention the link in your own blogs, responses, and entries on the net so that others also may reach here. Thank you.

Note: Only a member of this blog may post a comment.