if文


if文

  • if文は条件によって処理を分けて行うときに使う。


ソースコードJavaScript

a = 5;
if(a % 2 == 0) {
 document.write(a, 'は偶数です。');
} else {
 document.write(a, 'は奇数です。');
}
  • if(a % 2 == 0) :a÷2の余りが0のとき
  • else:a÷2の余りが0ではないとき
    • 5÷2のあまりは1なので、else以下の処理をします。

ブロック


ソースコード【 HTML 】

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>if文</title>
</head>
<body>

<script>


s=65;
    document.write('あなたの点数は',s,'点です。<br>');

if(s<70){
	document.write('平均点まであと',(70-s),'点<br>');
	document.write('がんばりましょう!');

}else{
	document.write('よくできました!');
}


</script>

</body>
</html>


ブラウザで表示