☰ Year plan · Communication Technology

The OR gate in VB.NET and PHP

Week 12 · Unit 2 Topics 5–6: OR gate simulation with VB.NET and with PHP · 3 lessons
1

The OR simulator: what changes from AND?

ICT Secondary 3 Term 1, Unit 2, Topics 5–6: pp. 48–64
GoalSay which parts of the AND simulator stay the same for OR and which part of the click code changes.
New words
window load · the code that runs once when the window opens and sets the start stateclick code · the code that runs when a switch is clickedinner If · an If written inside another Ifstate · what the controls show now: the two texts and the lamp
Explanation

Almost everything in the OR simulator is copied from Week 9. The window has the same controls, two buttons SwitchA and SwitchB and a lamp box LampBox. The start state is the same, both switches Off and the lamp unlit. The Load code and the small helper ShowLamp are the same. Only the click code changes, and only the part that decides the lamp. The AND rule needs both switches On, so when A is switched On the code must look at B first: that is the inner If. The OR rule needs at least one switch On. When A is switched On the lamp can be lit at once, because one On switch is enough, and no look at B is needed. When A is switched Off the lamp must go unlit only if B is Off too, so the inner If moves to the Off branch and its test changes. The code of SwitchB is again the mirror image, with A and B exchanged.

Click on A, and A wasAND simulatorOR simulator
Off (becomes On)lit only if B is On (inner If)lit at once
On (becomes Off)unlit at onceunlit only if B is Off too (inner If)
Click on Bmirror image of Amirror image of A
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        ShowLamp(True)
5    Else
6        SwitchA.Text = "Off"
7        If SwitchB.Text = "Off" Then
8            ShowLamp(False)
9        End If
10    End If
11End Sub
Output
On Off -> lit
The click code of switch A in the OR simulator. The output line is the state after one click on A from the start state.
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
For comparison, the click code of switch A in the AND simulator of Week 9. Find the inner If: it sits in the other branch.
From (A, B)Click A givesClick B gives
Off, OffOn, Off (lit)Off, On (lit)
On, OffOff, Off (unlit)On, On (lit)
Off, OnOn, On (lit)Off, Off (unlit)
On, OnOff, On (lit)On, Off (lit)
Where does one click lead in the OR simulator? Each new state obeys the OR rule: the lamp is lit when at least one text is On.
Class activity

Write the click code of SwitchB for the OR simulator by exchanging the roles of A and B in the code of SwitchA. Then say in one sentence what the inner If of the OR code checks.

Practice
1
Which parts of the simulator are the same for AND and OR?
2
When A is clicked and was Off, what does the OR code do with the lamp?
3
When A is clicked and was On, when does the OR lamp go unlit?
4
Where does the inner If sit in the AND code, and where in the OR code?