Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
An "if" statement is a control structure that causes a block of code to run only under certain conditions.
- An
if
statement is a control structure that causes a block of code to run only under certain conditions.- A condition followed by a block; block code is executed only if condition is true
package main
import (
"fmt"
)
func main() {
if true {
fmt.Println("true") // printed
}
if false {
fmt.Println("false") // not printed
}
if 1 < 2 {
fmt.Println("1 < 2") // printed
}
if 1 > 2 {
fmt.Println("1 > 2") // not printed
}
}
- Boolean operators:
-
!
: "not" -
&&
: "and" -
||
: "or"
-
package main
import "fmt"
func main() {
if !true {
fmt.Println("!true") // not printed
}
if !false {
fmt.Println("!false") // printed
}
if true && false {
fmt.Println("true && false") // not printed
}
if true && true {
fmt.Println("true && true") // printed
}
if true || false {
fmt.Println("true || false") // printed
}
if true || true {
fmt.Println("true || true") // printed
}
}
-
else
- Runs a block of code if no
if
condition is true
- Runs a block of code if no
package main
import "fmt"
func main() {
if false {
fmt.Println("if") // not printed
} else {
fmt.Println("else") // printed
}
}
-
else if
: A condition that's checked only if preceding conditions were false
package main
import "fmt"
func main() {
if false {
fmt.Println("if") // not printed
} else if true {
fmt.Println("else if") // printed
} else {
fmt.Println("else") // not printed
}
}
- Block scope rules apply to
if
block
package main
import "fmt"
func main() {
beforeIf := 888
if true {
withinIf := 999
fmt.Println("if")
}
fmt.Println(beforeIf) // OK
fmt.Println(withinIf) // Error!
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
An if statement is a control
structure that causes a block of
0:00
code to run under certain conditions.
0:03
It consists of a condition
followed by a block of code and
0:06
the code in the block is run
only if the condition is true.
0:09
So here we have an if statement and
0:13
the condition is the literal boolean value
trick, which of course evaluates the true.
0:14
So the code here in the block
does get executed and
0:20
it prints out the string true.
0:24
You can see that down here in the output.
0:27
Here's another if statement, where
the condition is a simple boolean value,
0:29
it's the value false.
0:33
And, of course, false is not true, so
the code in the block does not run.
0:35
You don't see false down
here in the program output.
0:39
Here's an if statement with
an actual comparison operation.
0:42
The result of is 1 less than 2 is true,
therefore the code in the block runs and
0:46
it prints out the string,
1 is less than 2.
0:51
You can see that here in the output.
0:54
And here's another comparison,
is 1 greater than 2?
0:57
Well, the result of that value is
false and so the block does not run.
1:01
The go language has many of the same
boolean operators that other languages do.
1:05
For example, there's the not operator,
the exclamation point,
1:09
which takes whatever boolean
value it's given and negates it.
1:12
So if it receives the value true,
it will turn it into false.
1:16
So, this if statement, the result is false
and therefore, this code won't be run.
1:20
But this one takes the value false and
makes it true.
1:25
With the result of true
the code in the block does run,
1:29
and you see that it prints
not false down here.
1:31
The operator looks to ensure that
the value on both sides of it is true.
1:36
So, here we have a value that's true on
the left but a false value on the right,
1:41
therefore this entire expression is
false because both values are not true.
1:47
However, down here, we have true on
the left as well as true on the right
1:53
therefore true double and true the result
is true, and this code gets run.
1:58
You can see it on the output.
2:03
The operator with the two vertical bars
here is read as or, it will return
2:09
a true value if either the value on its
left or the value on its right is true.
2:14
So, since this first value
here on the left is true,
2:19
there's no need to even evaluate
the value on the right.
2:23
The results of this expression is true.
2:26
And it prints this line
down here in the output.
2:28
The case is the same here.
2:34
The value on the left is true and
the value on the right is true, but
2:36
that doesn't matter.
2:40
If either one of those had been true,
the result would've been true, and
2:41
the code here in the if
block would have run.
2:45
And again, you can see it in the output.
2:47
The else block goes after an if block, and
it runs only if the if block doesn't run.
2:50
So here we have an if block,
and its condition is true.
2:56
So it does run.
2:59
It prints the expression
if down here in the output.
3:01
But what if we changed this to false so
that it doesn't run?
3:04
Let's try saving that, run this.
3:09
Now that we've changed this to
false the if block doesn't run so
3:11
the else block does.
3:15
And you see else printed
out here on the output.
3:16
You can also use if else blocks.
3:20
So in this code, the condition in
the if statement is true, and so
3:22
the format print line
if statement gets run.
3:26
But if we were to change that to false,
and re-run it,
3:28
then the if statement doesn't get run.
3:33
So it checks the if-else clause,
which it sees is true.
3:36
And because that's true, it will go ahead
and run this block, printing else-if.
3:40
If we were to change
that to false as well.
3:45
Save that, re-run it.
3:47
Now the if clause is false.
3:51
The else if clause is also false.
3:53
And, therefore,
it runs the else clause and prints else.
3:55
Now as before, this code within the curly
braces following the condition of an if
4:00
statement is a block.
4:04
And that means all the same rules
regarding the scope for variables applies.
4:06
So down here where we access
the beforeIf variable,
4:11
that's fine because the beforeIf variable
gets declared before the if block here,
4:15
and therefore it's still
within scope afterwards.
4:20
But here where we try to
print within the if variable,
4:22
that's not okay because it gets defined
within the block for the if statement.
4:27
And, therefore,
it's out of scope down here.
4:32
So, if we try to run this,
we'll see a compile error,
4:35
undefined within if, here on line 12.
4:38
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up