☰ Year plan · Communication Technology

The AND simulator in VB.NET

Week 9 · Unit 2 Topic 2: AND gate simulation project with VB.NET: purpose, stages, controls, code in three parts · 3 lessons
3

Tracing the AND code click by click

ICT Secondary 3 Term 1, Unit 2, Topic 2: pp. 19–31
GoalTrace the click code of the AND simulator on a table, and find the bug in a version that ignores the other switch.
New words
trace · to follow code step by step and write down the state after each stepnested If · an If written inside another Ifclick handler · the code that runs when a control is clickedbug · a mistake in a program
Explanation

Here is the click code of switch A. If its text is Off, it becomes On, and then an inner If checks the other switch: the lamp is lit only if B is On too. Otherwise, when A was On, it becomes Off and the lamp is unlit, because one open switch breaks AND. The code of switch B is its mirror image, with A and B swapped. Notice that when A turns On while B is Off the lamp does not need changing: it was unlit already, because A was Off. The nested If is the AND: the outer test says A is being switched On, and the inner test says B is On. To trace the code, write a table with one row per click, showing the texts of A and B and the lamp after the click. The first row is the start state.

StepClickABLamp
0(start)OffOffunlit
1AOnOffunlit
2BOnOnlit
Worked examples
Vb
1Private Sub SwitchA_Click(sender As Object, e As EventArgs) Handles SwitchA.Click
2    If SwitchA.Text = "Off" Then
3        SwitchA.Text = "On"
4        If SwitchB.Text = "On" Then
5            ShowLamp(True)
6        End If
7    Else
8        SwitchA.Text = "Off"
9        ShowLamp(False)
10    End If
11End Sub
The click code of switch A. The code of switch B is the same with the roles of A and B exchanged.
StepClickABLamp
0(start)OffOffunlit
1AOnOffunlit
2BOnOnlit
3AOffOnunlit
4BOffOffunlit
5BOffOnunlit
6AOnOnlit
A trace of six clicks, starting from the start state. The lamp is lit after step 2 and again after step 6.
Vb
1Private Sub SwitchA_Click(sender As Object, e As EventArgs) Handles SwitchA.Click
2    If SwitchA.Text = "Off" Then
3        SwitchA.Text = "On"
4        ShowLamp(True)
5    Else
6        SwitchA.Text = "Off"
7        ShowLamp(False)
8    End If
9End Sub
Output
On Off -> lit
A bug hunt. In this version of SwitchA_Click the inner If was forgotten. After one click on A, with B still Off, the lamp is lit, which breaks the AND rule. The output line is the state after that click.
Class activity

Trace the correct code on paper from the start state for the clicks B, A, A, B. Write a table with one row per click and the texts of A and B and the lamp after it.

Practice
1
Start state, then a click on B and a click on A. What is the state after each click?
2
The state is On On and the lamp is lit. What is the state after a click on B?
3
Why does the code not change the lamp when A turns On while B is Off?
4
In the buggy SwitchA_Click, what happens after one click on A from the start state?