Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
138 lines (117 loc) · 3.44 KB

File metadata and controls

138 lines (117 loc) · 3.44 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package loopAndFlow;
public class IfElseExamples {
public static void main(String[] args) {
// Code inside If is executed only if the condition is true
if (true) {
System.out.println("Will be printed");
}
// Statement inside this condition is not executed
// if (false) { //Dead Code
// System.out.println("Will NOT be printed");// Not executed
// }
// Lets look at an example
int x = 5;
if (x == 5) {
System.out.println("x is 5");// executed since x==5 is true
}
x = 6;
if (x == 5) {
System.out.println("x is 5");// Not executed since x==5 is false
}
// If Else
// If condition is true code in if is executed, else code in else is
// executed
int y = 10;
if (y == 10) {
System.out.println("Y is 10");// executed-condn y==10 is true
} else {
System.out.println("Y is Not 10");
}
y = 11;
if (y == 10) {
System.out.println("Y is 10");// NOT executed
} else {
System.out.println("Y is Not 10");// executed-condn y==10 is false
}
// Nested else if
// The code in the first if condition that is true is executed.
// If none of the if conditions are true, then code in else is executed.
int z = 15;
if (z == 10) {
System.out.println("Z is 10");// NOT executed
} else if (z == 12) {
System.out.println("Z is 12");// NOT executed
} else if (z == 15) {
System.out.println("Z is 15");// executed. Rest of the if else are
// skipped.
} else {
System.out.println("Z is Something Else.");// NOT executed
}
z = 18;
if (z == 10) {
System.out.println("Z is 10");// NOT executed
} else if (z == 12) {
System.out.println("Z is 12");// NOT executed
} else if (z == 15) {
System.out.println("Z is 15");// NOT executed
} else {
System.out.println("Z is Something Else.");// executed
}
// Be very careful with formatting
int number = 5;
if (number < 0) // condn is false. So the line in if is not executed.
number = number + 10; // Not executed
number++; // This statement is not part of if
System.out.println(number);// prints 6
// Guess the output
int k = 15;
if (k > 20) {
System.out.println(1);
} else if (k > 10) {
System.out.println(2);
} else if (k < 20) {
System.out.println(3);
} else {
System.out.println(4);
}
// Output is 2. Once a condition in nested-if-else is true the rest of
// the code is not executed.
// Guess the output
int l = 15;
if (l < 20)
System.out.println("l<20");
if (l > 20)
System.out.println("l>20");
else
System.out.println("Who am I?");
// Output is "l<20" followed by "Who am I?" on next line.
// else belong to the last if before it unless brackets ({}) are used.
// Guess the output
int m = 15;
if (m > 20)
if (m < 20)
System.out.println("m>20");
else
System.out.println("Who am I?");
// Nothing is printed to output. Above code is similar to code below
if (m > 20) {// Condn is false. So, code in if is not executed
if (m < 20)
System.out.println("m>20");
else
System.out.println("Who am I?");
}
//int x1 = 0;
// Condition in if should always be boolean
// if(x1) {} //COMPILER ERROR
// if(x1=0) {}//COMPILER ERROR. Using = instead of ==
boolean isTrue = false;
if (isTrue == true) {
System.out.println("TRUE TRUE");// Will not be printed
}
if (isTrue = true) {
System.out.println("TRUE");// Will be printed.
}
// Condition is isTrue=true. This is assignment. Returns true. So, code
// in if is executed.
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.