คืนค่าการตั้งค่าทั้งหมด
คุณแน่ใจว่าต้องการคืนค่าการตั้งค่าทั้งหมด ?
ลำดับตอนที่ #9 : Go I - Conditionals & Booleans
Using
if Statements
What
is Control Flow?
For the rest of this lesson, we will learn about
control flow. When programming, it is common to decide which part of the
program to run based on certain conditions. For example, "Changing grades
(command) is based on the test score (condition)" is an example of control
flow.
if
Statements
if statements allow you to create a control flow
that means "if X, do Y". An example of how to write an if statement
is shown below. Write the conditional
statement after if, and run the code
inside {} when the condition is true.
Continue to the next slide for more!
if
Statement Conditions
Symbols used to compare values are called comparison operators. Some,
like < and >,
are written the same way as in math. However, symbols like >= (greater than
or equal to) and <= (less than or equal to) from math are written like >= and <= in
Go. if statements in the form: if
conditionalStatement { }.
Boolean
Values
Let's look more at conditions used in if
statements. When a conditional statement using a comparison operator in the if
statement is printed, "true" is displayed in the console. What does
this "true" mean? Look at the next slide to find out!
What
are Boolean Values?
The "true" printed in the previous slide
is something called a boolean
value. The data type for boolean values is called "boolean type" and it
has two possible values, true or false. Conditional statements with comparisons are
true when the comparison is true and false when they are not.
if
Statements and Booleans
Using what you learned about booleans into
account, let's take another look at if statements. The code inside an if
statement will run when its condition is true and are not called when it's
false.
Comparison
Operators
Comparison operators are not only symbols that
compare the size of values, like > and >=. There are also symbols used to compare the
equality of values. In mathematics, _"%=_%" is used when both sides
are equal, but in Go, since = is for
assignment, "equality" is shown using the ==
operator.
Recap
of Booleans
The figure below recaps the most common comparison operators that you learned in this lesson.
else
Handling
False Conditions
With if statements,
you have already learned how to run specific code when a condition is true.
Next, let's learn how to write a different kind of control flow to deal with
conditions that are false.
else
By using else
with if statements, you can write a control flow meaning "if X, then Y,
but if not then Z". Here, when the if statement condition is false, the
code in the else statement will run.
else
if
If you want to add a branch for when the condition
of an if-statement is false, use else if.
The control flow "if A then B, if X then Y, if neither then Z" can be
created using else if!
else if statements
can be used any number of times, but they will run in order from the top down.
This means that only the code in the else-if with the condition that is true
first will run.
Combining
Conditional Statements
and
(&&)
Let's learn how to combine multiple conditional
statements. The conditional "if condition1 and condition2 are true"
is written like condition1 && condition2,
using &&. Here, && means
"and". When combining multiple conditional statements with &&, the statement will only become true when every condition is true.
or
(||)
The conditional statement "if condition1 or
condition2 is true" can be written like condition1
|| condition2 using the || operator. When
multiple conditions are combined with ||, if
any of them are true, the enitre statement will be true.
Negation
(!)
The ! operator is used to negate a condition, giving the opposite result. Therefore, the code !(conditionalStatement) will become false if the conditional statement is true, and true when it is false.
switch
The
switch Statement
This is the last exercise! In control flow, switch statements can be used
instead of if statements. switch statements
can be written as shown below. When the conditional value matches the case value, the code for that case will run. The
structure of switch statements is a bit complicated. (The "default"
will be explained later on.)
if
Statements and switch Statements
Let's compare examples of a switch statement and
an if statement. On the right, the content will be called when the conditional
value and case value are == (equal). When
the control flow is based on "what the value is (x
% 2)", switch statements can be written more easily.
Designating
Multiple Values
Multiple values can be specified using commas ( , ) after the word case. When multiple values are
used, the code for a case will run if the condition value of the switch matches
either one of them. This is similar to using multiple conditions with the
logical || (or) operator.
default
In switch statements, the content that will be run
when none of the cases fit can be specified with the default
case. This is similar to the way else is used in if statements.
ความคิดเห็น