14. Using Goto

Goto is one of the most powerful elements included with BrainShop.

The Problem

In the previous session, we created four cells as below.

Example 1.

  • Cell A
    • <nerve>itemInClassroom</nerve>
    • <strong/>what s in the classroom
    • Good! Next?
              <think><set-context>what s in the classroom</set-context></think>
           
  • Cell B
    • <nerve>itemInClassroom</nerve> *
    • <strong/>what s in the classroom
    • Good! Next?
              <think><set-context>what s in the classroom</set-context></think>
           
  • Cell C
    • * <nerve>itemInClassroom</nerve> *
    • <strong/>what s in the classroom
    • Good! Next?
               <think><set-context>what s in the classroom</set-context></think>
           
  • Cell D
    • * <nerve>itemInClassroom</nerve>
    • <strong/>what s in the classroom
    • Good! Next?
              <think><set-context>what s in the classroom</set-context></think>
           

They looks beautiful but have a problem: They repeat the same Output for four times. Can we simplify them?

The Solution

We may use goto element for the purpose. A note: goto is a powerful element that enables multiple cells work together to accomplish a complex task. Herein we just give one of many of its use cases.

One more note: If you are a programmer familiar with advanced programming languages such as Java, C, or PHP, you might be educated "goto is a devil" and one should never use goto, blah blah. Forget it. This is BrainXML!

The followings are cells rewritten with goto.

Example 2. Cells with goto.

  • Cell A
    • <nerve>itemInClassroom</nerve>
    • <strong/>what s in the classroom
    • Good! Next?
              <think><set-context>what s in the classroom</set-context></think>
           
  • Cell B
    • <nerve>itemInClassroom</nerve> *
    • <strong/>what s in the classroom
    • <goto><s>1</s></goto>
  • Cell C
    • * <nerve>itemInClassroom</nerve> *
    • <strong/>what s in the classroom
    • <goto><s>2</s></goto>
  • Cell D
    • * <nerve>itemInClassroom</nerve>
    • <strong/>what s in the classroom
    • <goto><s>2</s></goto>

When Cell B, C or D is executed, it doesn't return any result. Instead it continues the process as if user input is the text within goto. For the cells above, that's word matching the nerve - picture, blackboard, door, and so on.

In comparison to Example 1, Example 2 has only one Output and you can change it easily. While the true purpose of goto is control the process, hopefully the above examples may help you understand how it works.

If you haven't learnt s, it returns the text matching the corresponding wildcard in Input. The examples of wildcards include * (star), <word/>, or <nerve/>.

Things to Keep in Mind

  • You may use goto element to change process from one cell to another.
  • The element can be used for implementing complex logic or simplifying cells.