To compare two numbers, you use the
==
command. This double equals sign is easily
confused with the character
=
, which is used to assign values to variables.
There is another form of
if
that allows you to do one thing if the condition is true and
another if it is false. We will use this in some practical examples later in the book.
for
In addition to executing different commands under different circumstances, you also often
will want to run a series of commands a number of times in a program. You already know
one way of doing this, using the
loop
function. As soon as all the commands in the
loop
function
have been run, it will start again automatically. However, sometimes you need
more control than that.
So, for example, let’s say that you want to write a sketch that blinks 20 times, then
paused for 3 seconds, and then started again. You could do that by just repeating the same
code over and over again in your
loop
function, like this:
The
for
loop looks a bit like a function that takes three arguments, although here those
arguments are separated by semicolons rather than the usual commas. This is just a quirk
of the C language. The compiler will soon tell you when you get it wrong.
The first thing
in the parentheses after
for
is a variable declaration.
This specifies a
variable to be used as a counter variable and gives it an initial value—in this case, 0.
The second part is a condition that must be true for you to stay in the
Dostları ilə paylaş: