Learn to Program Step-by-Step

Salt and Pepper Pseudo code

Pseudo code for swapping two values

In the illustration, the value of num1 is 5; the value of num2 is 12. We would like to swap num1 and num2 so that num1 is 12 and num2 is 5. We will use another variable, temp.


This is the algorithm for a classic swap. You will probably use this algorithm many times. Note that in an assignment statement, the variable that gets a new value is always on the left and the new value or expression is always on the right.

  1. temp=num1 step1
  2. num1=num2 step2
  3. num2=temp step2
Like us on Facebook:

OER: Open Educational Resource
Creative Commons License Author: Janet E. Joy; Publisher: Zebra0.com
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Creative Commons Attribution-NonCommercial 4.0 International License
Last modified: January 08 2016 10:54:07.