1
00:00:04,900 --> 00:00:06,280
<v Tim>In this video we'll just take a look</v>

2
00:00:06,280 --> 00:00:07,600
at some decision constructs

3
00:00:07,600 --> 00:00:09,850
that you may not necessarily ever use

4
00:00:09,850 --> 00:00:12,160
but that should point out items to look for

5
00:00:12,160 --> 00:00:13,770
on the certification exam.

6
00:00:13,770 --> 00:00:15,465
So let's start by looking at the switch statement

7
00:00:15,465 --> 00:00:16,570
and we're gonna create a new class

8
00:00:16,570 --> 00:00:20,270
in our decisions project 'SwitchStatementOddities.'

9
00:00:20,270 --> 00:00:21,503
I'll paste in some code.

10
00:00:26,200 --> 00:00:27,350
Okay, there's the code.

11
00:00:28,750 --> 00:00:30,200
So terms of this code,

12
00:00:30,200 --> 00:00:33,000
it contains an array of byte assigned values,

13
00:00:33,000 --> 00:00:35,610
a for loop that loops through the array of byte values,

14
00:00:35,610 --> 00:00:38,400
also contains a switch statement with an expression in it,

15
00:00:38,400 --> 00:00:41,030
which simply multiplies the pass value by 10

16
00:00:41,030 --> 00:00:42,530
and the switch statement labels

17
00:00:42,530 --> 00:00:45,440
should now match the value in the switch expression.

18
00:00:45,440 --> 00:00:46,790
So let's actually run this.

19
00:00:49,520 --> 00:00:52,387
You can see we've got the output in the bottom down there.

20
00:00:52,387 --> 00:00:53,600
So far so good,

21
00:00:53,600 --> 00:00:56,100
but let's actually alter this slightly as follows.

22
00:00:56,100 --> 00:00:58,580
I'm going to change the switch statement on line 13.

23
00:00:58,580 --> 00:01:00,630
Instead of switch i multiplied by 10,

24
00:01:00,630 --> 00:01:02,730
just to make that switch i.

25
00:01:02,730 --> 00:01:04,910
Suddenly we've got five compiled errors,

26
00:01:04,910 --> 00:01:06,640
basically one on each of the five case labels,

27
00:01:06,640 --> 00:01:08,840
here as you can see on screen.

28
00:01:08,840 --> 00:01:10,073
Why is that the case?

29
00:01:11,070 --> 00:01:13,360
Required byte found int.

30
00:01:13,360 --> 00:01:16,590
When our switch expression was i multiplied by 10,

31
00:01:16,590 --> 00:01:19,020
the switch variable data type was an int.

32
00:01:19,020 --> 00:01:22,430
And so our case labels were also int, and all was well.

33
00:01:22,430 --> 00:01:25,640
Now what we've done is change the switch variable data type

34
00:01:25,640 --> 00:01:28,560
to a byte where after will lead me through an array of byte

35
00:01:28,560 --> 00:01:30,610
but our case labels are still an int,

36
00:01:30,610 --> 00:01:33,890
because their values exceed the byte valid value range.

37
00:01:33,890 --> 00:01:37,040
So can fix this if I actually change this code slightly now.

38
00:01:37,040 --> 00:01:38,240
Basically adjusting the labels

39
00:01:38,240 --> 00:01:39,690
to be values that are valid bytes.

40
00:01:39,690 --> 00:01:42,210
So I'm just going to copy and paste the code.

41
00:01:42,210 --> 00:01:43,820
Okay so you can see what I've done there now.

42
00:01:43,820 --> 00:01:44,710
Changing those values,

43
00:01:44,710 --> 00:01:47,360
and because the values are now byte valid

44
00:01:47,360 --> 00:01:49,890
so they're actually in the range of a byte

45
00:01:49,890 --> 00:01:52,300
they've no longer got any compilation errors.

46
00:01:52,300 --> 00:01:53,850
Just to confirm we'll run this.

47
00:01:55,670 --> 00:01:56,673
And the code works.

48
00:01:57,840 --> 00:01:59,090
Right so let's go back to the code.

49
00:01:59,090 --> 00:02:00,100
Specifically what I'm going to do

50
00:02:00,100 --> 00:02:02,850
is make a change to the case 104.

51
00:02:02,850 --> 00:02:05,123
If I change that and add a continue.

52
00:02:07,030 --> 00:02:09,820
What does continue mean in a switch statement?

53
00:02:09,820 --> 00:02:11,430
Well actually continue doesn't mean anything

54
00:02:11,430 --> 00:02:12,290
in a switch statement.

55
00:02:12,290 --> 00:02:15,210
It's associated with the encasing for loop,

56
00:02:15,210 --> 00:02:16,090
that we're executing in.

57
00:02:16,090 --> 00:02:17,840
The one starting on line 12.

58
00:02:17,840 --> 00:02:19,563
So let's run the code again.

59
00:02:22,590 --> 00:02:24,140
And you can see we've got a different output

60
00:02:24,140 --> 00:02:25,630
down the bottom now.

61
00:02:25,630 --> 00:02:30,580
So looking at the output, the values 100, 103, 104

62
00:02:30,580 --> 00:02:34,570
they all actually reach the case statement 104

63
00:02:34,570 --> 00:02:36,330
and because of the continue statement

64
00:02:36,330 --> 00:02:39,150
initiated a new iteration of the surrounding loop,

65
00:02:39,150 --> 00:02:41,380
skipping the code after the switch statement.

66
00:02:41,380 --> 00:02:46,363
In other words, down here the system.out.println on line 25.

67
00:02:47,650 --> 00:02:50,170
How would you break out of a loop from a switch statement?

68
00:02:50,170 --> 00:02:52,280
You can't do it with a simple break, can you?

69
00:02:52,280 --> 00:02:53,770
Because a break in a switch statement

70
00:02:53,770 --> 00:02:55,620
is just a break in the switch label.

71
00:02:55,620 --> 00:02:58,880
To break out of a loop from a switch conditional block

72
00:02:58,880 --> 00:03:01,360
you'll need to create a label for your for loop,

73
00:03:01,360 --> 00:03:04,140
which we'll cover in much more detail in the video on loops.

74
00:03:04,140 --> 00:03:05,960
And that's coming up later in the course.

75
00:03:05,960 --> 00:03:08,880
But you're probably already familiar with labels here.

76
00:03:08,880 --> 00:03:10,390
What we'll do is we'll add a label here

77
00:03:10,390 --> 00:03:13,810
and we'll answer the question we asked by changing our code

78
00:03:13,810 --> 00:03:16,793
for the case label 104 and 107.

79
00:03:17,960 --> 00:03:19,630
So what we're gonna do is add a label as I mentioned.

80
00:03:19,630 --> 00:03:21,620
So we'll go back to the code.

81
00:03:21,620 --> 00:03:24,260
We're going to add the label above for loop.

82
00:03:24,260 --> 00:03:25,093
So up here.

83
00:03:26,040 --> 00:03:28,830
Start, I'll just call for.

84
00:03:28,830 --> 00:03:29,663
There's our label.

85
00:03:29,663 --> 00:03:32,360
Now we're gonna change the condition

86
00:03:32,360 --> 00:03:35,010
from continue for our case 104 to a break.

87
00:03:38,010 --> 00:03:40,670
Moving on down for the condition for 107,

88
00:03:40,670 --> 00:03:41,980
we've got a break there.

89
00:03:41,980 --> 00:03:44,360
We're gonna change that to a break.

90
00:03:44,360 --> 00:03:46,680
This time we're gonna use the start_for label

91
00:03:47,580 --> 00:03:49,842
that we added on line 13.

92
00:03:49,842 --> 00:03:51,342
Right, so we run the code now.

93
00:03:54,530 --> 00:03:57,940
So the break in case label 104 prevented any of values

94
00:03:57,940 --> 00:03:59,690
being assigned a value

95
00:03:59,690 --> 00:04:04,690
because the value only occurs in case 107 and case 126.

96
00:04:05,200 --> 00:04:08,930
And the break in case label 107 is the for loop break

97
00:04:08,930 --> 00:04:10,800
and not just the switch statement break.

98
00:04:10,800 --> 00:04:12,790
So we immediately break out of the for loop

99
00:04:12,790 --> 00:04:14,540
without printing another statement.

100
00:04:14,540 --> 00:04:16,410
Now if we added a case statement break

101
00:04:16,410 --> 00:04:21,060
either before or after the break, to find in label 107

102
00:04:21,060 --> 00:04:22,800
we would actually cause a compiler error

103
00:04:22,800 --> 00:04:25,560
indicating one of the other breaks is unreachable.

104
00:04:25,560 --> 00:04:27,320
So as you can see there's many variations

105
00:04:27,320 --> 00:04:28,400
to a switch statement,

106
00:04:28,400 --> 00:04:31,543
and many variables that can lead to compiler errors.

107
00:04:33,300 --> 00:04:35,470
When you see a switch statement on an exam,

108
00:04:35,470 --> 00:04:39,000
examine its location, is it participating in a loop?

109
00:04:39,000 --> 00:04:42,260
Don't let the breaks and continue statements mislead you.

110
00:04:42,260 --> 00:04:45,470
Examine the switch expression, can you determine the type?

111
00:04:45,470 --> 00:04:47,900
Does the type match the case labels?

112
00:04:47,900 --> 00:04:49,450
Is the type valid?

113
00:04:49,450 --> 00:04:53,050
Doubles, floats, longs, booleans are all not valid.

114
00:04:53,050 --> 00:04:56,530
Objects that are not strings or wrappers are also not valid,

115
00:04:56,530 --> 00:04:58,300
remembering e nums are valid.

116
00:04:58,300 --> 00:05:00,570
If the switch expression is a local variable,

117
00:05:00,570 --> 00:05:01,853
has it been initialised?

118
00:05:02,940 --> 00:05:04,130
If it hasn't,

119
00:05:04,130 --> 00:05:06,400
does the switch statement have a default block?

120
00:05:06,400 --> 00:05:09,010
If not, then the answer is compiler error.

121
00:05:09,010 --> 00:05:12,500
Examine the case labels, are they all compiler constants?

122
00:05:12,500 --> 00:05:14,250
Variables are not permitted.

123
00:05:14,250 --> 00:05:17,450
If case blocks do not have breaks, the code falls through,

124
00:05:17,450 --> 00:05:19,590
so multiple blocks could be executed.

125
00:05:19,590 --> 00:05:22,060
And finally, remember that a default label

126
00:05:22,060 --> 00:05:23,210
doesn't have to be the last one

127
00:05:23,210 --> 00:05:25,023
or need to be included at all.

128
00:05:26,134 --> 00:05:28,170
All right so let's end the video here.

129
00:05:28,170 --> 00:05:30,780
In the next one we'll start looking, or look at a quiz

130
00:05:30,780 --> 00:05:32,250
on decision constructs.

131
00:05:32,250 --> 00:05:34,850
Then after the quiz we'll get into loops.

132
00:05:34,850 --> 00:05:36,150
See you in the next video.


