File tree Expand file tree Collapse file tree 4 files changed +639
-0
lines changed
docs/1-trial-session/08-if-statement Expand file tree Collapse file tree 4 files changed +639
-0
lines changed Original file line number Diff line number Diff line change 22title : 条件分岐
33---
44
5+ import Sandpack from " @site/src/components/Sandpack" ;
6+
57## if文
68
79** if文** は、<Term >JavaScript</Term >の<Term >** 制御構造** </Term >で、特定の条件下のみで実行されるプログラムを記述することができます。
@@ -17,6 +19,32 @@ if (age < 18) {
1719
1820このプログラムは、` 未成年者の場合は法定代理人の同意が必要です。 ` と表示しますが、1行目を` const age = 20; ` に変更すると何も表示されなくなります。
1921
22+ <Sandpack
23+ files = { {
24+ " /index.html" : {
25+ code: ` <!DOCTYPE html>
26+ <html>
27+ <head>
28+ <meta charset="UTF-8">
29+ <title>条件分岐</title>
30+ </head>
31+ <body>
32+ <script src="script.js"></script>
33+ </body>
34+ </html> ` ,
35+ hidden: true ,
36+ },
37+ " /script.js" : ` const age = 16;
38+ if (age < 18) {
39+ document.write("未成年者の場合は法定代理人の同意が必要です。");
40+ } ` ,
41+ }}
42+ options = { {
43+ editorHeight: 250 ,
44+ activeFile: " /script.js" ,
45+ }}
46+ />
47+
20482行目の` if (age < 18) { ` 部分がポイントです。ここに差し掛かると、括弧内の<Term >式</Term >` age < 18 ` が<Term >評価</Term >され、` true ` になります。このため、直後の波括弧内の処理が実行されます。
2149
2250if文の基本構造は
@@ -55,6 +83,34 @@ if (age >= 18) {
5583
5684この例では、` age >= 18 ` の<Term >評価</Term >が` false ` となるので、` 子供です ` が表示されます。
5785
86+ <Sandpack
87+ files = { {
88+ " /index.html" : {
89+ code: ` <!DOCTYPE html>
90+ <html>
91+ <head>
92+ <meta charset="UTF-8">
93+ <title>if-else</title>
94+ </head>
95+ <body>
96+ <script src="script.js"></script>
97+ </body>
98+ </html> ` ,
99+ hidden: true ,
100+ },
101+ " /script.js" : ` const age = 16;
102+ if (age >= 18) {
103+ document.write("大人です");
104+ } else {
105+ document.write("子供です");
106+ } ` ,
107+ }}
108+ options = { {
109+ editorHeight: 250 ,
110+ activeFile: " /script.js" ,
111+ }}
112+ />
113+
58114## if~else if~else
59115
60116if~else if~else構文を使うと、複数の条件を重ねることができます。
You can’t perform that action at this time.
0 commit comments