ProgrammingSequence, selection, iteration, procedures, functions, modules, parameters, compilers, interpreters

Syllabus Detail

  • sequence 11 GEN, 11 ATAR, 12 GEN, 12 ATAR
  • selection 11 GEN, one-way, two-way, multi-way (case, nested if) 11 ATAR, 12 GEN, 12 ATAR
  • iteration 11 GEN, test first, test last, fixed for 11 ATAR, 12 GEN, 12 ATAR

Background

  • A control structure is a block of code that analyzes variables and decides where to take the program
  • There are three types of control structure; sequence, selection and iteration
  • Each type has its own benefits and disadvantages
  • The control structure used depends on the type of task being carried out
  • Although multiple control structures may work for an algorithm, it's best practice to chose the most efficient and cleanest control structure to use

 

Sequence

  • Sequence is the easiest control structure to use and make sense of
  • This control structure runs through the code sequentially
  • This means that steps are followed in order from line 1 of the program to the end no matter what conditions are met

 

Selection

  • The selection control structure allows a program to test conditions based off certain criterion
  • Selection follows IF [this] THEN [that] logic
  • It can be seen as a boolean conditon as the result is going to be true or false
  • Selection structures also rely on an ELSE statement
  • When the specified or expected criterion isn't met, the program will follow the ELSE instructions

CASE example used in Selection from Visual Basic

Dim grade As String

Private Sub Compute_Click( )
grade=txtgrade.Text
Select Case grade

Case "A"
result.Caption="High Distinction"

Case "A-"
result.Caption="Distinction"

Case "B"
result.Caption="Credit"

Case "C"
result.Caption="Pass"

Case Else
result.Caption="Fail"

End Select

End Sub

 

Iteration

  • Iteration is another term for looping
  • A function or program utilizing looping will run a specific block of code until certain crtierion is met
  • Examples in Visual Basic HERE
  • Three main types of iteration exist; while, do-while and for

    • WHILE Loops (Test-first)
      • A while loop can be seen as a repeating IF statement
      • At the start of the statement, a boolean condition is given
      • While this statement is true, the code within the WHILE loop will execute
      • Once this statement is false, the compiler or interpreter will move on to the next block of code
      • This is known as test-first because the expression is always considered before running the loop, meaning that this block of code has the option to never run if the condition isn't met

      • Example:
        $i = someFunction($value);

        while ($i >= 2){
            // Do something
        }

        # Our variable $i will have an arbitrary value from some undefined function.
        # While that number is greater than or equal to 2, we'll "do something".

       

    • FOR Loops (Fixed)
      • FOR loops run until a particular condition is satisfied
      • A boolean expression is given at the start of the control structure
      • There is an incremental counter in the for loop expression that tells the function when to stop
      • This is known as a fixed control structure as there's a finite number of times the loop will run (as per determined by the expression)

      • Example:
        for ($i = 1; $i <= 200; $i++){

            // Do something
        }

        # In a FOR loop, we state what value our counter ($i) will begin with (in this case 1)
        # We then tell the control to repeat the code while $i is LESS THAN or EQUAL TO 200
        # Finally, we increment our counter ($i) by 1 to continue the loop.
        # This is a fixed loop because we know that the
        boundaries are 1 <= $i <= 200 (thus looping 200 times)

       

    • DO-WHILE Loops (Test-last)
      • A DO-WHILE loop will run a condition regardless of the previous outcomes
      • This is because the boolean expression is evaluated at the end of the control structure
      • This means that a DO-WHILE loop is garuanteed to run at least once

      • Example:
        do {
            // Do something
            $i++;
        } while ($i < 4);

        # In this example, we're telling the code to run some functions and increment $i by 1.
        # Since this is a test-last, we will always have this code executed at least once, since $i isn't evaluated until the end of the block.
        # $i will increment by 1 until it is equal to 4.

 

Further Research

 

 

Found an error or have an enhancement? Please let us know via this contact form