160 likes | 423 Views
Selection. If. Flowchart. A diagram that shows step-by-step progression through a procedure or system especially using connecting lines and a set of conventional symbols Used to represent the flow of control of a computer program. Sample Flowchart. Basic Elements in Flowchart. Terminal Box.
E N D
Selection If
Flowchart • A diagram that shows step-by-step progression through a procedure or system especially using connecting lines and a set of conventional symbols • Used to represent the flow of control of a computer program
Basic Elements in Flowchart Terminal Box Process Box Decision Box Flow line
Another Sample Start Press Ctrl-Atl-Delete Turn on the computer Enter your password Wait until login screen appears End Flow of control of turn on and login windows NT
Terminator Box: Start and end of the flow Process Box: Action that will be taken Flow Line: The direction of line indicates the next state Decision Box: A true-or-false decision, the next state depends on the result of the decision. Meaning of Flowchart Elements
Relation Between Pseudo-code and Flowchart • Both pseudo-code and flowchart descript the steps and flow to solve a problem • You can convert a pseudo-code to flowchart and vice versa
Start Press Ctrl-Atl-Delete Turn on the computer Enter your password Wait until login screen appears End Relation Between Pseudo-code and Flowchart • Turn on the computer • Wait until login screen appears • Press Ctrl-Atl-Delete • Enter your password
Decision Box • One of the elements in flowchart • Contains a true or false statement • The next state depends on the value of the statement
Start Stand Up Before 1:00 pm ? Good Morning Mr. Wong Good Afternoon Mr. Wong true false End Example
Start Stand Up Before 1:00 pm ? Good Morning Mr. Wong Good Afternoon Mr. Wong true false End Pseudo-code Version • Stand-up • If the time is before 1:00 pm • Say “Good morning Mr. Wong” • Else, say “Good afternoon Mr. Wong”
If…then…else Statement • Syntax: • if <Boolean expression> • then <statement-1> • else <statement-2> • if, then, else are reserved words • Remember! no comma after statement-1 and before else • else statement can be omitted
Example program positive; var input : integer; begin write ( ‘Please enter a number: ‘ ); readln ( input ); if input >= 0 then writeln ( ‘It is a positive number’ ) else writeln ( ‘It is a negative number’ ) end.
Compound statements • Compound statement means one or more statements bracketed by begin and end • The statement part itself is also a compound statement • The then part and else part of if…then…else statement should contain one statement only • If you need more then one statement in any of these parts, you have to use compound statement
Example if x > 0 then begin y := x + 1; writeln ( x ); end ……
Nested if statements • if statement inside the then part or the else part of another if statement • You are reminded to write program with proper indentation