CIS 103 - Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College Property of Jack Wilson, Cerritos College 1 Topics 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 Programs Modules Algorithms Statements Syntax & Semantics Logic Planning Tools Control Structures Memory Concepts Data Types Programming Concepts Overview Property of Jack Wilson, Cerritos College 2 1.1 Programs A program contains one or more modules. Program module module Many languages require a module with a special name, such as "main" for a program to be created. Programs are often also referred to as applications or more generally as software. Programming Concepts Overview Property of Jack Wilson, Cerritos College 3 1.2 Modules A module is a collection of statements that perform a specific task within a program. Module is a generic term. The most commonly used terms are listed below. function method Programming Concepts Overview C, C++, Python Java Property of Jack Wilson, Cerritos College 4 1.3 Algorithms An algorithm is the name given to the logic that is developed and used to code the statements in a module. Algorithm to square a number: 1. get a number 2. multiply the number by itself 3. return this value Programming Concepts Overview Implementation of the algorithm in a module (function): double square ( double number ) { return number * number; } Property of Jack Wilson, Cerritos College 5 1.4 Statements A statement is the name given to a high level language instruction. Programs are written using a variety of different types of statements. Examples include: Declaration Assignment Input Output Module Invocation Programming Concepts Overview int count count = 0 get(number) put(“Grand Total is “ + total) printHeadings() Property of Jack Wilson, Cerritos College 6 1.5 Syntax & Semantics Syntax refers to the rules of a language that must be followed to construct a valid statement. Statements are constructed using the following components: reserved words ( aka keywords ) identifiers operators literals punctuation symbols Semantics refers to the meaning of a statement. It addresses the question "What does the statement do?“. Programming Concepts Overview Property of Jack Wilson, Cerritos College 7 1.6 Control Structures A control Structure determines the flow of execution for statements executing in a module. There are 3 control structures that are used in all programming languages: 1. Sequence 2. Selection 3. Repetition (aka Iteration) Programming Concepts Overview Property of Jack Wilson, Cerritos College 8 1.6 Control Structures Sequence Structure A sequence structure performs a single task without asking any questions or repeating anything. • • • • • • • declare a variable assign a value to a variable input a value output a value call a function return a value from a function stop program execution Programming Concepts Overview Property of Jack Wilson, Cerritos College 9 1.6 Control Structures Selection Structure Asks a question (called a condition) and based on the answer ( true or false ) follows a path of execution. In example 2, only one of the paths of execution would be followed. false true Example 1 – One path of execution false The rectangle represents a structure. true The diamond symbol represents a condition that evaluates to true or false. Example 2 – Two paths of execution Programming Concepts Overview Property of Jack Wilson, Cerritos College 10 1.6 Control Structures Repetition Structure true Asks a question (called the condition) and based on the answer ( true or false ) executes a statement or exits the structure. false Example 1: Pre-test loop The rectangle represents a statement. Continues to execute the statement as long as the condition evaluates to yes. true A repetition structure is often called a "loop". The diamond represents a condition. false Example 2: Post-test loop Programming Concepts Overview Property of Jack Wilson, Cerritos College 11 1.7 Logic Planning Tools Pseudocode Flowchart Hierarchy Chart Printer/Screen Spacing Chart File Description (record layout chart) Programming Concepts Overview Property of Jack Wilson, Cerritos College 12 1.8 Memory Concepts Memory is measured in bytes ( KB / MB / GB / TB ) A byte is 8 bits ( binary digits ) Every byte in RAM memory is assigned a unique address Data types use one or more bytes to store information integer – 4 bytes Programming Concepts Overview real number – 8 bytes Property of Jack Wilson, Cerritos College 13 1.8 Memory Concepts To avoid having to reference a specific location in memory to access a piece of data, variable names are used instead of addresses. To use a variable in a program, you must declare the variable by giving it a name and a data type. [ RAPTOR does not require us to do this ] Examples: int count boolean finished string name When a program is executed, a symbol table ( think of it as a data dictionary in memory ) is created that maps the name of a variable to the location in memory where the data is being stored. Programming Concepts Overview Property of Jack Wilson, Cerritos College 14 1.9 Data Types Programs work with data ( information stored in locations in memory ). There are many different categories of data. Numeric integer numbers real numbers Here are some examples: Language Numeric Text Boolean Java int long float double char String boolean C++ int long float double char string bool Text Every language has specific keywords which are used to specify a data type. Not all data types are supported in all languages. a single character value a "string" of characters Boolean True False Programming Concepts Overview Property of Jack Wilson, Cerritos College 15 x.Xx Program Translation Translators Assembler Compiler Interpreter JIT compilation Programming Concepts Overview Property of Jack Wilson, Cerritos College 16 Identifiers Programming Concepts Overview Property of Jack Wilson, Cerritos College 17 Expressions and Statements Programming Concepts Overview Property of Jack Wilson, Cerritos College 18 Object-Oriented Programming Programming Concepts Overview Property of Jack Wilson, Cerritos College 19 Procedural Programming Programming Concepts Overview Property of Jack Wilson, Cerritos College 20 Event-Driven Programming Programming Concepts Overview Property of Jack Wilson, Cerritos College 21 Reserved Words Programming Concepts Overview Property of Jack Wilson, Cerritos College 22 Operators Mathematical (arithmetic) Relational (boolean) Logical Programming Concepts Overview Property of Jack Wilson, Cerritos College 23 Operator Precedence and Associativity Programming Concepts Overview Property of Jack Wilson, Cerritos College 24 File Input Programming Concepts Overview Property of Jack Wilson, Cerritos College 25 File Output Programming Concepts Overview Property of Jack Wilson, Cerritos College 26 Program Development Programming Concepts Overview Property of Jack Wilson, Cerritos College 27 Making Decisions Programming Concepts Overview Property of Jack Wilson, Cerritos College 28 Looping Programming Concepts Overview Property of Jack Wilson, Cerritos College 29 Working with Strings Programming Concepts Overview Property of Jack Wilson, Cerritos College 30 Arrays Programming Concepts Overview Property of Jack Wilson, Cerritos College 31 Classes Programming Concepts Overview Property of Jack Wilson, Cerritos College 32 Class Diagrams Programming Concepts Overview Property of Jack Wilson, Cerritos College 33 Formatted Output Programming Concepts Overview Property of Jack Wilson, Cerritos College 34