-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConditionals+Loops+Switch
96 lines (82 loc) · 1.73 KB
/
Conditionals+Loops+Switch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//Switch Statement
var rank = "Commander";
switch(rank)
{
case "Private":
case "Sergeant":
console.log("You are not authorized.");
break;
case "Commander":
console.log("Hello commander! what can I do for you today?");
break;
case "Captain":
console.log("Hello captain! I will do anything you wish.");
break;
default:
console.log("I don't know what your rank is.");
break;
}
//Not Equal to
var notTrue = false;
if (!notTrue)
{
console.log("not not true is true!");
}
//If Statement
if (confirm("Are you John Smith?"))
{
console.log("Hello John, how are you?");
} else {
console.log("Then what is your name?");
}
//////////////////////////
var foo = 1;
var bar = 2;
//If Example 2
if (foo < bar)
{
console.log("foo is smaller than bar.");
}
//If Example 3
var foo = 1;
var bar = 2;
var moo = 3;
if (foo < bar && moo > bar)
{
console.log("foo is smaller than bar AND moo is larger than bar.");
}
if (foo < bar || moo > bar)
{
console.log("foo is smaller than bar OR moo is larger than bar.");
}
//For Loop example 1
for (var i = 0; i < 3; i++)
{
console.log(i);
}
/////////////////////////
//For Loop example 2(iterate through an entire array)
var myArray = ["A", "B", "C"];
for (var i = 0; i < myArray.length; i++)
{
console.log("The member of myArray in index " + i + " is " + myArray[i]);
}
/////////////////////////////
//While loop(iterate through the code only if the condition is met)
var i = 99;
while (i > 0)
{
console.log(i + " bottles of beer on the wall");
i -= 1;
}
//While loop example 2
var i = 99;
while (true)
{
console.log(i + " bottles of beer on the wall");
i -= 1;
if (i == 0)
{
break;
}
}