The OR simulator: what changes from AND?
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 was | AND simulator | OR simulator |
|---|---|---|
| Off (becomes On) | lit only if B is On (inner If) | lit at once |
| On (becomes Off) | unlit at once | unlit only if B is Off too (inner If) |
| Click on B | mirror image of A | mirror image of A |
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
On Off -> lit
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
| From (A, B) | Click A gives | Click B gives |
|---|---|---|
| Off, Off | On, Off (lit) | Off, On (lit) |
| On, Off | Off, Off (unlit) | On, On (lit) |
| Off, On | On, On (lit) | Off, Off (unlit) |
| On, On | Off, On (lit) | On, Off (lit) |
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.