Skip to content

Conditional Program Flow

Controlling the flow of a program from within the program is a big part of Turing initial General Purpose computer. It allows a program to execute in various orders, based on internal changes and external data

Key Concepts

Conditional FlowA single decision point in the program that will execute or skip a section of the code
Program makes decisions based in outside informationDuring execution a program can change program flow based on data or user inputs
Either/Or DecisionsBeyond executing or skipping instructions, a program make a mutually-exclusive decision to execute one (1) set of instruction or another
Terms
TermMeaning
Code BlockSet of instructions together in a section of a program. Code blocks are instructions that together accomplish a function of the program.
ConditionBoolean value set by data or user input. Can be multiple values that are all logically compared into a single True/False result.
InstructionSingle executable line of code in a program. Includes OpCodes and Operands.

Introduction

Programs can react to data or user inputs during execution, and decide to execute a different set of instruction based on the input. The ability to make these decisions allows the program to be dynamic, reacting to various inputs and producing appropriate outputs.

The standard conditional flow has two (2) types: 1) IF, where instructions are executed or skipped and , 2) IF/ELSE, where one (1) set of instructions are executed or an different set are executed.

Conditional and Iterative constructs use a condition to decide to change program flow. The condition is set prior to the conditional or iterative code. It may be set by data from previous instructions, user inputs, or data form other programs or computers.

The Condition is the result of comparing data with another data items, or to a range of values. The condition typically a Boolean result of a true/false.

Example Comparisons
  • data is the same as a single value x = 17
  • data is larger that a value x > 16
  • data within a range of values x > 16 && x < 87

In each case, the comparison is either true or false

With the Condition evaluated, the program will execute or skip certain code if the condition is true. Otherwise, the program will continue executing in a sequential flow

Conditional Flow

In conditional flow, the program can execute or skip 1 or more lines of code based on a condition. The condition is evaluated while the program is running. In this way, the same program can execute differently, based on data or user inputs, each time it runs

Conditional Flow Diagram

Simple conditional 'if/else' program in Java and LC-3 Assembly

java
  Scanner myObj = new Scanner(System.in);
  System.out.println("Enter age");
  String userAge = myObj.nextInt();
  if(userAge > 0)
  {
    System.out.println("You entered a valid age");
  }
  else
  {
    System.out.println("You entered an invalid age");
  }
asm
.ORIG x3000
  LEA R0, enterAge
  PUTS
  IN
  JSR convertToInt ; subroutine to convert input into a number and save in R1
  BRp Valid ; Based on CC register set by convertToInt
  BRnz Invalid

  Valid LEA R0, goodAge
    PUTS
    BR Done

  Invalid LEA R0, badAge
    PUTS

  Done HALT
.END

enterAge .STRINGZ "Enter age"
goodAge .STRINGZ "You entered a valid age"
badAge .STRINGZ "You entered an invalid age"

If Branch

A program can evaluate a condition, such as an arithmetic or logical comparison between values. Based on that condition, the code may skip a section of code that does not pertain to the condition

Simple conditional 'if' program in Java and LC-3 AssemblySimple conditional 'if' program in Java and LC-3 Assembly

java
  Scanner myObj = new Scanner(System.in);
  System.out.println("Enter age");
  String userAge = myObj.nextInt();
  if(userAge > 0)
  {
    System.out.println("You entered a valid age");
  }
asm
.ORIG x3000
  LEA R0, enterAge
  PUTS
  IN
  JSR convertToInt ; subroutine to convert input into a number and save in R1
  BRp Valid ; Based on CC register set by convertToInt
  BRnz Done

  Valid LEA R0, goodAge
  PUTS

  Done HALT
.END

enterAge .STRINGZ "Enter age"
goodAge .STRINGZ "You entered a valid age"

If/Else Branch

The program may choose between 2 separate code sections based on a condition Simple conditional 'if/else' program in Java and LC-3 Assembly

java
  Scanner myObj = new Scanner(System.in);
  System.out.println("Enter age");
  String userAge = myObj.nextInt();
  if(userAge > 0)
  {
    System.out.println("You entered a valid age");
  }
  else
  {
    System.out.println("You entered an invalid age");
  }
asm
.ORIG x3000
  LEA R0, enterAge
  PUTS
  IN
  JSR convertToInt ; subroutine to convert input into a number and save in R1
  BRp Valid ; Based on CC register set by convertToInt
  BRnz Invalid

  Valid LEA R0, goodAge
    PUTS
    BR Done

  Invalid LEA R0, badAge
    PUTS

  Done HALT
.END

enterAge .STRINGZ "Enter age"
goodAge .STRINGZ "You entered a valid age"
badAge .STRINGZ "You entered an invalid age"

Other conditionals constructs, such as Switch, While, and Do/While are based on these same Condition check and change in flow

Conclusion

Conditional flow, like If/Else are decision points in the program. The decision point makes the react better in dynamic inputs

The contents of this E-Text were developed under an Open Textbooks Pilot grant from the Fund for the Improvement of Postsecondary Education (FIPSE), U.S. Department of Education. However, those contents do not necessarily represent the policy of the Department of Education, and you should not assume endorsement by the Federal Government.
Released under Creative Commons BY NC 4.0 International License