72 lines
1.3 KiB
HTML
72 lines
1.3 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
|
|
<html>
|
|
<head>
|
|
<title>If statement</title>
|
|
<link href="style.css" rel="stylesheet" type="text/css">
|
|
</head>
|
|
<body background="images/back.gif">
|
|
<!--START-->
|
|
|
|
<h3>If statement</h3>
|
|
|
|
An if statement has the form
|
|
|
|
<p>
|
|
<blockquote>
|
|
<pre><b>if</b> (<expression>) <statement></pre>
|
|
</blockquote>
|
|
|
|
<p>
|
|
or
|
|
|
|
<p>
|
|
<blockquote>
|
|
<pre><b>if</b> (<expression>) <statement> <b>else</b> <statement></pre>
|
|
</blockquote>
|
|
|
|
<p>
|
|
The statement can also be a block. The expression will be evaluated. If
|
|
the (rounded) value is <=0 (<b>false</b>) the statement after else is
|
|
executed, otherwise (<b>true</b>) the other statement is executed. It is a
|
|
good habit to always put curly brackets around the statements in the if
|
|
statement. So best use
|
|
|
|
<p>
|
|
<blockquote>
|
|
<pre>
|
|
<b>if</b> (<expression>)
|
|
{
|
|
<statement>
|
|
}
|
|
<b>else</b>
|
|
{
|
|
<statement>
|
|
}
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<p>
|
|
<b>Example</b><br>
|
|
|
|
The following program moves the object toward the middle of the screen.
|
|
|
|
<p>
|
|
<blockquote>
|
|
<pre>
|
|
{
|
|
<b>if</b> (x<200) {x += 4} <b>else</b> {x -= 4};
|
|
}
|
|
</pre>
|
|
</blockquote>
|
|
|
|
|
|
<!--END-->
|
|
</body>
|
|
</html>
|
|
|
|
<!-- KEYWORDS
|
|
else
|
|
if
|
|
conditional statement
|
|
--> |