Early Code (Fortran)

Spaghetti Code

1 i=0
2 i=i+1
3 PRINT i; "squared=";i*i
4 IF i>=100 THEN GOTO 6
5 GOTO 2
6 PRINT "Program Completed."
7 END

Credit: Wikipedia

GOTO Considered Harmful - Dijkstra

Image Credit: XKCD

Structured Programming Paradigm

Programs only consist of:

  • Sequences (Blocks of Statements)
  • Selection (Conditionals)
  • Iteration (Loop & Recursion)
  • Subroutines (Functions)

Sequences

Selection

Iteration

Subroutines

Structured Code

1 FOR i=1 TO 100
2     PRINT i;"squared=";i*i
3 NEXT i
4 PRINT "Program Completed."
5 END

Credit: Wikipedia

Object-Oriented Paradigm

Organize programs to represent real or theoretical "objects" that use:

  • Encapsulation (Attributes)
  • Message Passing (Calling Methods)
  • Dynamic Dispatch (Object Chooses Code)

Object-Oriented Code

class Squares{
	public void printSquares(int limit){
		for(int i = 0; i < limit; i++){
			System.out.println("squared=" + (i*i));
		}
		System.out.println("Program Completed.");
	}
}

We will use only object-oriented, structured code in this class.

It will help us write programs that are easier for others to understand.