150 likes | 188 Views
Basic Control Structures. Sequence Selection Repetition. Sequence. A process executed from one to another in a straightforward manner. Start. Read N. Print N. End. Example of Sequence. Design a flowchart that will accept and display a number. Write its equivalent algorithms.
E N D
Basic Control Structures Sequence Selection Repetition
Sequence • A process executed from one to another in a straightforward manner.
Start Read N Print N End Example of Sequence • Design a flowchart that will accept and display a number. Write its equivalent algorithms. Algorithm: Step 1: Read in the value of N. Step 2: Print the value of N.
Start Sum=0 Product=0 Read A,B Sum=A+B Product=A*B Print Sum, Product End Example of Sequence 2. Draw a flowchart that will compute and display the sum and product of two numbers. Write its equivalent algorithm. Algorithm: Step 1:Initialize Sum and Product into 0. Step 2: Read in the values of A and B. Step 3. Compute Sum by adding A and B then compute Product by multiplying A and B. Step 4: Print the computed value of Sum and Product.
A C B Selection (if – then – else) • A choice is provided between alternatives. T F
Example of Selection • Draw a flowchart that will input values for A and B. Compare two values inputted and print which of the values is higher including the remarks “Higher”. Write its equivalent algorithm.
Start Input A, B A > B Print A, “Higher” Print B, Higher End Algorithm: Step 1: Read in the values of A and B. Step 2: Test if A is greater than B, A is higher. However, If A is less than B, B is higher. Step 3: Print the number and the remark “Higher”. T F
Repetition (Looping) • This structure provides for the repetitive execution of an operation or routine while the condition is true. The condition is evaluated before executing any process statement. As long as the condition is true, the process is executed, otherwise, control flows out of the structure.
C F T Loop A
Example of Repetition (Looping) Construct a flowchart that will count from 1 to 10 and print each number counted using the do-while-repetition structure .Write its equivalent algorithm.
Start C=0 C < 10 F End T C = C + 1 Print C Example of Repetition (Looping) Algorithm: Step 1: Initialize the value of C to 0. Step 2: Test if C is less than 10. Step 3: If C is less than 10, add 1 to the value of C, print the value then go back to Step 2. However, if C is greater than 10, stop processing.
Commonly Used Operators in Flowcharting Arithmetic Operators + addition - Subtraction * Multiplication / Division
Commonly Used Operators in Flowcharting Relational Operators = equal > Greater than < less than <> Not equal > Greater than or equal to < less than or equal to
Commonly Used Operators in Flowcharting Logical operators && AND || OR ! NOT