-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture11If.java
More file actions
139 lines (136 loc) · 3.18 KB
/
Lecture11If.java
File metadata and controls
139 lines (136 loc) · 3.18 KB
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
139
import java.util.Scanner;
/**
* Code examples from the Lecture slides
* Lecture 11 powerpoint
*
* conditional execution using if
*
* @author P.M.Campbell
* @version 2020-fall
*
*/
public class Lecture11If {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number;
System.out.println("Using if, check for even, odd, number:");
number = in.nextInt();
usingIfEvenOdd(number);
usingIfPosNeg(number);
usingIfelseIf(number);
unreachableCode(-5); // case 1: <= 0
unreachableCode(2); // case 2: 0, 1, 2, 3
unreachableCode(25); // case 3: > 3
noBraces(number);
weirdBug(number);
}
/**
* use if statements, check for even or odd
*
* @param x number to be checked
*
* @author P.M.Campbell
* @version 2020-fall
*/
public static void usingIfEvenOdd(int x) {
if (x % 2 == 1) {
System.out.println(x+ " is odd ");
}
if (x % 2 == 0) {
System.out.println(x+ " is even ");
}
if (x % 2 == 1) {
System.out.println(x+ " is odd ");
} else {
System.out.println(x+ " is even ");
}
}
/**
* use nested if statements, check for positive/
* negative numbers
*
* @param x number to be checked
*
* @author P.M.Campbell
* @version 2020-fall
*/
public static void usingIfPosNeg(int x) {
if (x >0) {
System.out.println(x+ " is positive ");
} else {
System.out.println(x+ " is negative ");
}
// what about zero ??
// messy nested if
if (x >0) {
System.out.println(x+ " is positive ");
} else {
if (x == 0 ) {
System.out.println(x+ " is zero ");
} else {
System.out.println(x+ " is negative ");
}
}
}
/**
* use else if instead of a nested if statement,
* check for positive / negative numbers
*
* @param x number to be checked
*
* @author P.M.Campbell
* @version 2020-fall
*/
public static void usingIfelseIf(int x) {
// zero covered, better else / if structure
if (x >0) {
System.out.println(x+ " is positive ");
} else if (x == 0 ) {
System.out.println(x+ " is zero ");
} else {
System.out.println(x+ " is negative ");
}
}
/**
* use if else/if statements
* be careful of your logic
*
* @param x number to be checked
*
* @author P.M.Campbell
* @version 2020-fall
*/
public static void unreachableCode(int x) {
if (x > 0 ) {
System.out.println(x+ " is > 0 ");
} else if (x > 3 ) {
System.out.println("Unreachable !!");
}
}
/**
* use if statements, potential problem with missing braces {}
*
* @param x number to be checked
*
* @author P.M.Campbell
* @version 2020-fall
*/
public static void noBraces(int x) {
if (x > 0 )
System.out.println(x+ " is positive ");
System.out.println("Always executed .... +ve or -ve or 0");
}
/**
* use if statements, weird potential problem
*
* @param x number to be checked
*
* @author P.M.Campbell
* @version 2020-fall
*/
public static void weirdBug(int x) {
if (x > 0 ) ; {
System.out.println(x+ " is positive ");
}
}
}