introducing programming -...

44
Copyright Copyright © © 2005 2005 Department of Computer & Information Science Department of Computer & Information Science Introducing Programming Introducing Programming

Upload: truongdien

Post on 18-Apr-2018

233 views

Category:

Documents


4 download

TRANSCRIPT

Copyright Copyright ©©2005 2005 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Introducing ProgrammingIntroducing Programming

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

GoalsGoals

By the end of this lecture, you should …By the end of this lecture, you should …

•• Understand the different types of Understand the different types of programming languages.programming languages.

•• Understand the basic procedures in a Understand the basic procedures in a program as input, processing and output.program as input, processing and output.

•• Understand the importance of variables.Understand the importance of variables.

•• Understand a basic map of the program Understand a basic map of the program development cycle.development cycle.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

SoftwareSoftware

•• Application Software Application Software –– Word ProcessorsWord Processors

–– Database s/wDatabase s/w

–– SpreadsheetsSpreadsheets

–– Painting programsPainting programs

–– Web browsers, email Web browsers, email

programsprograms

•• System SoftwareSystem Software–– Operating SystemsOperating Systems

•• WindowsWindows

•• Macintosh OSMacintosh OS

•• UnixUnix

•• LinuxLinux

–– DriversDrivers

Software is comprised of instructions that get a computer to perform a task.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Programming LanguagesProgramming Languages

•• Programming languages allow Programming languages allow

programmers to code software.programmers to code software.

•• The three major families of languages are:The three major families of languages are:–– Machine languagesMachine languages

–– Assembly languagesAssembly languages

–– HighHigh--Level languagesLevel languages

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Machine LanguagesMachine Languages

•• Comprised of 1s and 0sComprised of 1s and 0s

•• The “native” language of a computerThe “native” language of a computer

•• Difficult to program Difficult to program –– one misplaced 1 or one misplaced 1 or

0 will cause the program to fail.0 will cause the program to fail.

•• Example of code:Example of code:1110100010101 111010101110 1110100010101 111010101110

10111010110100 1010001111011110111010110100 10100011110111

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Assembly LanguagesAssembly Languages

•• Assembly languages are a step towards easier Assembly languages are a step towards easier programming. programming.

•• Assembly languages are comprised of a set of Assembly languages are comprised of a set of elemental commands which are tied to a specific elemental commands which are tied to a specific processor.processor.

•• Assembly language code needs to be translated Assembly language code needs to be translated to machine language before the computer to machine language before the computer processes it.processes it.

•• Example:Example:ADD 1001010, 1011010ADD 1001010, 1011010

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

HighHigh--Level LanguagesLevel Languages

•• HighHigh--level languages represent a giant level languages represent a giant

leap towards easier programming.leap towards easier programming.

•• The syntax of HL languages is similar to The syntax of HL languages is similar to

English. English.

•• Historically, we divide HL languages into Historically, we divide HL languages into

two groups:two groups:–– Procedural languagesProcedural languages

–– ObjectObject--Oriented languages (OOP)Oriented languages (OOP)

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Procedural LanguagesProcedural Languages

•• Early highEarly high--level languages are typically level languages are typically

called procedural languages.called procedural languages.

•• Procedural languages are characterized by Procedural languages are characterized by

sequential sets of linear commands. The sequential sets of linear commands. The

focus of such languages is on focus of such languages is on structurestructure..

•• Examples include C, COBOL, Fortran, Examples include C, COBOL, Fortran,

LISP, Perl, HTML, VBScriptLISP, Perl, HTML, VBScript

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

ObjectObject--Oriented LanguagesOriented Languages

•• Most objectMost object--oriented languages are highoriented languages are high--level languages.level languages.

•• The focus of OOP languages is not on The focus of OOP languages is not on structure, but on structure, but on modeling datamodeling data..

•• Programmers code using “blueprints” of Programmers code using “blueprints” of data models called data models called classesclasses..

•• Examples of OOP languages include C++, Examples of OOP languages include C++, Visual Visual Basic.NETBasic.NET and Java.and Java.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

CompilingCompiling

•• Regardless of the HL Language, all HL programs Regardless of the HL Language, all HL programs

need to be translated to machine code so that a need to be translated to machine code so that a

computer can process the program.computer can process the program.

•• Some programs are translated using a Some programs are translated using a

compilercompiler. When programs are compiled, they . When programs are compiled, they

are translated all at once. Compiled programs are translated all at once. Compiled programs

typically execute more quickly than interpreted typically execute more quickly than interpreted

programs, but have a slower translation speed.programs, but have a slower translation speed.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

InterpretingInterpreting

•• Some programs are translated using an Some programs are translated using an

interpreterinterpreter. Such programs are . Such programs are

translated linetranslated line--byby--line instead of all at line instead of all at

once (like compiled programs). once (like compiled programs).

Interpreted programs generally translate Interpreted programs generally translate

quicker than compiled programs, but have quicker than compiled programs, but have

a slower execution speed.a slower execution speed.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Programming ExampleProgramming Example

•• Simple programming problem: Simple programming problem: Convert a Convert a price from British pounds into Dollars.price from British pounds into Dollars.

•• PseudocodePseudocodeInput the price of the item, Input the price of the item, PoundPricePoundPrice, in pounds, in pounds

Compute the price of the item in dollars:Compute the price of the item in dollars:

Set Set DollarPriceDollarPrice = 1.62 * = 1.62 * PoundPricePoundPrice

Write Write DollarPriceDollarPrice

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Programming ExampleProgramming Example

•• Translating to Basic:Translating to Basic:INPUT INPUT PoundPricePoundPrice

LET LET DollarPriceDollarPrice = 1.62 * = 1.62 * PoundPricePoundPrice

PRINT PRINT DollarPriceDollarPrice

ENDEND

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Input & VariablesInput & Variables

•• Input operations get data into the programsInput operations get data into the programs

•• A user is A user is prompted prompted to enter data:to enter data:Write “Enter the price in pounds”Write “Enter the price in pounds”

Input Input PoundPricePoundPrice

•• Computer programs store data in named Computer programs store data in named

sections of memory called sections of memory called variablesvariables. In the . In the

example above, the variable is named example above, the variable is named PoundPricePoundPrice. The value of a variable can, and . The value of a variable can, and

often does, change throughout a program.often does, change throughout a program.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Types of DataTypes of Data

•• Numeric DataNumeric Data–– Integer data, I.e., whole numbers, 10 25 Integer data, I.e., whole numbers, 10 25 --45 045 0

–– Floating point data Floating point data –– have a decimal point 23.0, have a decimal point 23.0, --5.05.0

•• Character data (alphanumeric)Character data (alphanumeric)–– All the characters you can type at the keyboardAll the characters you can type at the keyboard

–– Letters & numbers not used in calculationsLetters & numbers not used in calculations

•• Boolean data Boolean data –– TRUE/FALSETRUE/FALSE

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Data Processing and OutputData Processing and Output

Set Set DollarPriceDollarPrice = 1.62 * = 1.62 * PoundPricePoundPrice

•• The above statement is a processing statement. The above statement is a processing statement. Take the value in the variable Take the value in the variable PoundPricePoundPrice, , multiply by 1.62, and set the variable multiply by 1.62, and set the variable DollarPriceDollarPrice to the result of the multiplication.to the result of the multiplication.

Write Write DollarPriceDollarPrice

•• Output the value in Output the value in DollarPriceDollarPrice to the to the monitor.monitor.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Assignment OperationsAssignment Operations

Set counter = counter + 1Set counter = counter + 1

•• Assignment statements change the value in a Assignment statements change the value in a variable Take the value of variable Take the value of countercounter, add 1, and , add 1, and

store the result back in the same variable.store the result back in the same variable.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Arithmetic OperationsArithmetic Operations

14141616--22--SubtractionSubtraction

181816+216+2++AdditionAddition

8816/216/2//DivisionDivision

323216*216*2**MultiplicationMultiplication

16164^24^2^̂ExponentiationExponentiation

ResultResultExampleExampleSymbolSymbolNameName

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Data HierarchyData Hierarchy

1.1. ParenthesisParenthesis

2.2. ExponentiationExponentiation

3.3. Multiplication/DivisionMultiplication/Division

4.4. Addition/SubtractionAddition/Subtraction

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Hierarchy of Operations Hierarchy of Operations

ExampleExample

3 * (6 + 2) / 12 – (7 – 5) ^ 2 * 3 ( ) first

= 3 * 8 / 12 – 2 ^ 2 * 3 ^ next

= 3 * 8 / 12 – 4 * 3 Mult/Div (L to R)

= 24 / 12 – 4 * 3 Mult/Div (L to R)

= 2 – 12 Add/Subtr

= -10

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Data OutputData Output

•• Send information from the program to the screen, Send information from the program to the screen,

or printer, or disk file.or printer, or disk file.

Write Write DollarPriceDollarPrice

•• The computer displays the value of the variable The computer displays the value of the variable DollarPriceDollarPrice to the screen and the cursor goes to the screen and the cursor goes

to the next line.to the next line.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Data OutputData Output

Write “The price in Dollars is”, Write “The price in Dollars is”,

DollarPriceDollarPrice

•• The output looks like this:The output looks like this:

The price in Dollars is 162The price in Dollars is 162

•• The text inside the “ ” is output to the user “as is,” The text inside the “ ” is output to the user “as is,”

and it is the and it is the valuevalue in the variable that is output.in the variable that is output.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

2.12.1 The Program Development The Program Development

CycleCycle

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Programming as Problem SolvingProgramming as Problem Solving

•• Problem solving Problem solving

principles:principles:1.1. Completely understand Completely understand

the problemthe problem

2.2. Devise a plan to solve it Devise a plan to solve it

3.3. Carry out the planCarry out the plan

4.4. Review the resultsReview the results

•• Developing a Developing a

Program:Program:1.1. Analyze the problemAnalyze the problem

2.2. Design the programDesign the program

3.3. Code the programCode the program

4.4. Test the programTest the program

An example of programming as problem solving,

Brewster’s Thousands follows …

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

1) Analyze the Problem1) Analyze the Problem

•• Brewster’s ThousandsBrewster’s Thousands–– The problem: Brewster wants to invest money at a The problem: Brewster wants to invest money at a

local bank. There are many options such as interest local bank. There are many options such as interest

rates, terms of deposit, compounding frequencies. rates, terms of deposit, compounding frequencies.

He needs a program to compute, for any given initial He needs a program to compute, for any given initial

investment, the final maturity (value) of the deposit.investment, the final maturity (value) of the deposit.

•• What are the inputs? (given data)What are the inputs? (given data)

•• What are the outputs? (required data)What are the outputs? (required data)

•• How will we calculate the required outputs from the given How will we calculate the required outputs from the given

inputs?inputs?

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

2) Design the Program2) Design the Program

•• Create an outline of the programCreate an outline of the program

•• An An algorithm algorithm –– a step by step procedure a step by step procedure

that will provide the required results from that will provide the required results from

the given inputs.the given inputs.

•• Algorithm Examples: Instructions on how Algorithm Examples: Instructions on how

to make a cake, use the bank’s ATM, etc.to make a cake, use the bank’s ATM, etc.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

3) Code the Program3) Code the Program

•• Once the design is completed, write the Once the design is completed, write the

program program codecode. .

•• Code is written in some programming Code is written in some programming

language such as BASIC, Pascal, C++, language such as BASIC, Pascal, C++,

Java, etc.Java, etc.

In this course we write code in pseudoIn this course we write code in pseudo--

code, developing the skills to be used code, developing the skills to be used

when studying the specific languages.when studying the specific languages.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

4) Testing the program4) Testing the program

•• Locate any errors (bugs)Locate any errors (bugs)

•• Testing is done throughout the development Testing is done throughout the development

cyclecycle

•• DeskDesk--checkingchecking, or code , or code walkthrough walkthrough is is

performed to locate errors in the code.performed to locate errors in the code.–– Pretend you are the computer and execute your own code. Pretend you are the computer and execute your own code.

•• Ultimate test is to run the program to see if the Ultimate test is to run the program to see if the

outputs are correct for the given inputs.outputs are correct for the given inputs.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Modular ProgrammingModular Programming

•• Determine the major tasks that the Determine the major tasks that the

program must accomplish. Each of these program must accomplish. Each of these

tasks will be a tasks will be a modulemodule..

•• Some modules will be complex Some modules will be complex

themselves, and they will be broken into themselves, and they will be broken into

subsub--modules, modules, and those suband those sub--modules may modules may

also be broken into even smaller modules.also be broken into even smaller modules.

•• This is called This is called toptop--down designdown design

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Mapping ModulesMapping Modules

Final Value:Final Value:Set Set FinalValueFinalValue = Principal *= Principal *

(1 + Rate / Frequency) ^ (1 + Rate / Frequency) ^

(Frequency * Term)(Frequency * Term)

Display:Display:Write Write

FinalValueFinalValue

Rate of Interest:Rate of Interest:Set Rate = Set Rate =

PercentageRate/100PercentageRate/100

Input Variables:Input Variables:PrincipalPrincipal

PercentageRatePercentageRate

TermTerm

FrequencyFrequency

OutputsOutputsProcessesProcessesInputsInputs

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Code ModulesCode Modules

•• A module is an independent, selfA module is an independent, self--

contained section of code that performs a contained section of code that performs a

single task.single task.

•• The The main modulemain module is the module that is the module that

drives the application. It “controls” all drives the application. It “controls” all

other modules. Typically, the main module other modules. Typically, the main module

calls other modules in order to have them calls other modules in order to have them

perform certain tasks.perform certain tasks.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Program Control & ModulesProgram Control & Modules

•• When the main module calls another When the main module calls another

module, program control transfers to the module, program control transfers to the

called module. Program control cedes called module. Program control cedes

back to the main module when the called back to the main module when the called

module finishes.module finishes.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Main moduleMain module

Display program title and brief description of programDisplay program title and brief description of program

Call Input Data ModuleCall Input Data Module

Call Perform Calculations moduleCall Perform Calculations module

Call Output Results ModuleCall Output Results Module

End ProgramEnd Program

Input Data moduleInput Data module

Prompt for Principal, PercentageRate, Term, FrequencyPrompt for Principal, PercentageRate, Term, Frequency

Input Principal, PercentageRate, Term, FrequencyInput Principal, PercentageRate, Term, Frequency

End moduleEnd module

Perform Calculations modulePerform Calculations module

Set Rate = PercentageRate / 100Set Rate = PercentageRate / 100

Set Set FinalValueFinalValue = Principal * (1 + Rate / Frequency) ^ (Frequency * Term)= Principal * (1 + Rate / Frequency) ^ (Frequency * Term)

End moduleEnd module

Output Results ModuleOutput Results Module

Write Principal, PercentageRate, Term, FrequencyWrite Principal, PercentageRate, Term, Frequency

Write Write FinalValueFinalValue

End moduleEnd module

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Hierarchy Chart (HIPO Chart)Hierarchy Chart (HIPO Chart)

•• A HIPO Chart (“Hierarchy of Inputs, A HIPO Chart (“Hierarchy of Inputs,

Processes & Outputs”) is similar to an Processes & Outputs”) is similar to an

organization chart organization chart –– it shows what it shows what

modules exist and how they are related.modules exist and how they are related.

•• It’s a good idea to keep modules short It’s a good idea to keep modules short ––

about 1 page per module.about 1 page per module.

•• We will have very small modules while We will have very small modules while

getting comfortable using these tools.getting comfortable using these tools.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

HIPO Chart for HIPO Chart for

Brewster’s Thousands ExampleBrewster’s Thousands Example

Main Module

Input Data

Perform Calculations

Output Results

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

CodingCoding

•• Coding is done in a specific programming Coding is done in a specific programming

language. In this part of the course, we will use language. In this part of the course, we will use

pseudocode. Later, we’ll adapt our pseudocode pseudocode. Later, we’ll adapt our pseudocode

to write in JavaScript.to write in JavaScript.

•• Coding before finishing a solid algorithm is a lot Coding before finishing a solid algorithm is a lot

like putting the cart before the horse and usually like putting the cart before the horse and usually

spells disaster. Time wellspells disaster. Time well--spent in the design spent in the design

phase will head off problems in coding!phase will head off problems in coding!

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

DocumentationDocumentation

•• Internal Documentation Internal Documentation –– Comments explain to the reader the logic and Comments explain to the reader the logic and

decision processes of the programmer. Comments are decision processes of the programmer. Comments are

ignored by an interpreter or compiler.ignored by an interpreter or compiler.

–– Types of comments include code comments, Types of comments include code comments,

documentation comments & module comments.documentation comments & module comments.

•• External DocumentationExternal Documentation–– External documentation includes a External documentation includes a user’s guide user’s guide and, and,

typically, a more technical typically, a more technical system administrator’s system administrator’s guideguide..

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

TestingTesting

•• Most of the work should be done before the Most of the work should be done before the

phase begins phase begins –– creating of a testing document.creating of a testing document.

•• Two types of testing:Two types of testing:–– Testing for errorsTesting for errors

–– Quality/Usability testingQuality/Usability testing

•• Two phases of testing:Two phases of testing:–– Alpha testing (Internal testing)Alpha testing (Internal testing)

–– Beta testing (Testing at the customer site w/ live data)Beta testing (Testing at the customer site w/ live data)

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Types of ErrorsTypes of Errors

•• Syntax Syntax –– wrong grammar, i.e., breaking the wrong grammar, i.e., breaking the

rules of how to write the languagerules of how to write the language

–– Forgetting punctuation, misspelling keywordForgetting punctuation, misspelling keyword

–– The program will not run at all with syntax errorsThe program will not run at all with syntax errors

•• Logic Logic -- the program runs, but does not produce the program runs, but does not produce

the expected results.the expected results.

–– Using an incorrect formula, incorrect sequence of Using an incorrect formula, incorrect sequence of

statements, etc.statements, etc.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Structured ProgrammingStructured Programming

•• A method for designing and coding A method for designing and coding

programs in a systematic, organized programs in a systematic, organized

manner. manner.

•• It combines the principles of topIt combines the principles of top--down down

design, modularity and the use of the design, modularity and the use of the

three accepted control structures of three accepted control structures of

sequencesequence, , repetitionrepetition and and selectionselection..

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Control StructuresControl Structures

•• SequenceSequence ––in sequential order. in sequential order.

–– The simplest of control structures The simplest of control structures –– start at start at

the beginning and continue in sequential the beginning and continue in sequential

order.order.

•• SelectionSelection –– selectively execute selectively execute

statementsstatements

–– Called a Called a branchbranch, it requires a condition to , it requires a condition to

determine when to execute statements.determine when to execute statements.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

Control StructuresControl Structures

•• RepetitionRepetition –– repeat statements more repeat statements more

than oncethan once

–– Called a Called a looploop, it needs a stop condition, I.e, , it needs a stop condition, I.e,

the program will continue to loop until some the program will continue to loop until some

condition is met.condition is met.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

EventEvent--Driven ProgrammingDriven Programming

•• In an In an eventevent--driven driven program, the flow of program, the flow of

control is based on the user’s clicking on menus control is based on the user’s clicking on menus

and buttons, etc. These user actions are called and buttons, etc. These user actions are called

eventsevents..

•• EventEvent--driven programming still uses the basic driven programming still uses the basic

principles of structured programming principles of structured programming –– program program

modules, control structures, good programming modules, control structures, good programming

style, and program testing.style, and program testing.

CSCI N201: Programming ConceptsCSCI N201: Programming Concepts

Copyright Copyright ©©2004 2004 �������� Department of Computer & Information ScienceDepartment of Computer & Information Science

ResourcesResources

•• VenitVenit, Stewart. , Stewart. Extended Prelude to Extended Prelude to Programming: Concepts and Design.Programming: Concepts and Design.Scott/Jones, Inc., 2002.Scott/Jones, Inc., 2002.