KaiquanMah commited on
Commit
a55dc25
·
verified ·
1 Parent(s): ec73a3d

if, else if, else

Browse files
Week 1: Types, condition clauses and loops/8a. Condition clauses ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // eg1
2
+ // user input > 100?
3
+ import java.util.Scanner;
4
+
5
+ // 3 blocks
6
+ // 1 class definition / define class
7
+ // 2 define 'main' method
8
+ // 3 within 'condition clause'
9
+ public class Example {
10
+ public static void main(String[] args) {
11
+ Scanner reader = new Scanner(System.in);
12
+
13
+ System.out.print("Give a number: ");
14
+ int num = Integer.valueOf(reader.nextLine());
15
+
16
+ if (num > 100) {
17
+ System.out.println("Number was greater than 100.");
18
+ }
19
+
20
+ }
21
+ }
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+ // eg2
31
+ // else
32
+ import java.util.Scanner;
33
+
34
+ public class Example {
35
+ public static void main(String[] args) {
36
+ Scanner reader = new Scanner(System.in);
37
+
38
+ System.out.print("Give a number: ");
39
+ int num = Integer.valueOf(reader.nextLine());
40
+
41
+ if (num > 100){
42
+ System.out.println("Number was greater than 100.");
43
+ } else {
44
+ System.out.println("Number was lesser than or equal to 100.");
45
+ }
46
+ }
47
+ }
48
+ Example output:
49
+ Give a number: 54
50
+ Number was lesser than or equal to 100.
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+ // eg3
59
+ // else if
60
+ import java.util.Scanner;
61
+
62
+ public class Example {
63
+ public static void main(String[] args) {
64
+ Scanner reader = new Scanner(System.in);
65
+
66
+ System.out.print("Give a number: ");
67
+ int num = Integer.valueOf(reader.nextLine());
68
+
69
+ if (num < 0){
70
+ System.out.println("Number was negative.");
71
+ } else if (num > 0) {
72
+ System.out.println("Number was positive.");
73
+ } else {
74
+ System.out.println("Number was zero.");
75
+ }
76
+ }
77
+ }
78
+ Example output:
79
+
80
+ Give a number: 15
81
+ The number was positive.
82
+
83
+
84
+
85
+
86
+
87
+