Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS120: Introduction to Computer Science

Similar presentations


Presentation on theme: "CPS120: Introduction to Computer Science"— Presentation transcript:

1 CPS120: Introduction to Computer Science
File Processing: Basic Structure

2 Control Structures Control structure: an instruction that determines the order in which other instructions in a program are executed Structured programming each logical unit of a program should have just one entry and one exit These constructs are selection statements, looping statements, and subprogram statements Statements are executed in sequence until an instruction is encountered that changes this sequencing

3 Sequence Statements that are simply statements that naturally follow one another from the top of the program to the bottom

4 Sequence Control Structures
Sequence control structures direct the order of program instructions. The fact that one instruction follows another—in sequence—establishes the control and order of operations.

5 Calculate Add 1 to Counter
A program can instruct a computer to perform mathematical operations. Add 1 to Counter

6 Store A program will often instruct a computer to store intermediate results. Place 1 in Counter

7 Compare and Branch A program can instruct a computer to compare two items and do something based on a match or mismatch which, in turn, redirect the sequence of programming instructions. There are two forms: IF-THEN IF-THEN-ELSE

8 Selection Statements that include if statements and switch statements
Selection statements are sometimes called conditional or decision statements. if (score >= 60) { cout << "I passed" << endl; } else { cout << "I'll do better next time!" << endl; }

9 IF-THEN false true Entry Exit True statement a Test condition p

10 IF-THEN-ELSE Entry Exit Test condition p “true” statement a
false true Entry Exit Test condition p “true” statement a “false” statement a

11 Iteration Statements that allow the compiler to return to a point higher in the program in order to continuously repeat one or more statements All loops including while, do/while, and for loops are prime examples of iteration statements while (num < 10) { cout << "hello world" << endl; num = num + 1; }

12 Iterate A program loop is a form of iteration. A computer can be instructed to repeat instructions under certain conditions.

13 Iteration Control Structures
Iteration control structures are looping mechanisms. Loops repeat an activity until stopped. The location of the stopping mechanism determines how the loop will work: Leading decisions Trailing decisions

14 Leading Decisions If the stop is at the beginning of the iteration, then the control is called a leading decision. The command DO WHILE performs the iteration and places the stop at the beginning.

15 DO WHILE Loop No Yes Entry Exit Test condition p Loop statement a

16 Trailing Decisions If the stop is at the end of the iteration, the control mechanism is called a trailing decision. The command DO UNTIL performs the iteration and puts the stop at the end of the loop.

17 DO UNTIL Loop No Yes Entry Test condition p Exit Loop statement a

18 Invocation Statements that allow you to place a block of statements that you wish to use several times during a program in one section You then have the ability to "call" those statements whenever you wish to execute them without having to retype the block of statements over and over again within the program In C++, we use "function calls" as invocation statements displayHelloWorld( ); will call (i.e. invoke) the function below void displayHelloWorld() { cout << "hello world" << endl; }

19 Structural Review: Types of Statements
sequence selection (if and switch structures) iteration (for and while loops) invocation (functions)


Download ppt "CPS120: Introduction to Computer Science"

Similar presentations


Ads by Google