lecture 8 control structures, pseudocode and flowcharts .pptx

Full Transcript

CONTROL STRUCTURES SM 184 FLOWCHARTS A lot of Our programs so far consist of just a list of commands to be done in order ◦ The program cannot choose whether or not to perform a command ◦ The program cannot perform the same command more than once ◦ Such programs ar...

CONTROL STRUCTURES SM 184 FLOWCHARTS A lot of Our programs so far consist of just a list of commands to be done in order ◦ The program cannot choose whether or not to perform a command ◦ The program cannot perform the same command more than once ◦ Such programs are extremely limited! Control structures allow a program to base its behavior on the values of variables Control structures Some classes of control structures are: 1. Sequence structure Programs executed sequentially by default 2. Selection structures eg. if, if/else, switch 3. Repetition structures eg. while, do/while, for Selection Control Structures Some types of selection statements include: 1. – if selection statement 2. – if … else selection statement 3. – switch selection statements Selection Control Structures explained 1. The if selection statement is called a single-selection structure It selects or ignores a single action (group of actions) 2. The if…else statement is called a double selection structure It selects between two different action (group of actions) 3. The switch statement is called a multiple-selection structure. It selects among different actions (group of actions) Flowchart for the if statement true If selection structure condition? statement performs an indicated action only when the false condition is true otherwise the action is skipped The If Selection Structure Example: Let’s have pseudocode statement » If student’s grade is greater than or equal to 60, Print “Passed” true Print Studentgrade>=60 Passed Start If studentgrade >= 60 false Print “Passed” Stop Exercise on If Selection Write a program using pseudocode and flow chart that would accept the age of a person. If age > 18 Display a message indicating you can vote. Else Display a message indicating you can't vote. The If … else selection structure The if…else selection structure allows the programmer to specify that different action is to be performed when condition is true and when the condition is false Flowchart for the if-else statement true false condition? statement-1 statement-2 The If … else selection structure The if…else selection structure allows the programmer to specify that different action is to be performed when true false Studentgrade>=60 condition is true and when the condition is false Print Print Passed failed For example: If a student’s grade is greater than or equal to 60, print passed else print failed Nested if …else Statement if (grade >= 70) Nested if…else structure test Print A for multiple cases by placing else if (grade >= 60) if…else selection structures Print B else if (grade >= 50) inside if…else selection Print C structure else if (grade >= 40) Print D else Print F Repetition Control Structures Repetition Control Structures repetition structures are created with repetition statement(s) Examples of repetition statements are: ◦– while ◦– do…while ◦– For While loop A WHILE loop is a loop that repeats while some condition is satisfied. The WHILE loop tests its condition at the beginning of every loop. If the condition is false from the start, the sequence of activities contained in the loop never runs at all. While Control Structure (Loop) A while repetition structure allows the programmer to specify that an action is to be repeated while some condition remains true. The while loop This is the form of the while loop: while (condition) statement(s) If the condition is true, the statement is executed, then the whole thing is done again The statement is executed repeatedly until the condition becomes false If the condition starts out false, the statement is never executed at all Flowchart for the while loop true condition? statement false Exercise: Write a program using a while loop that prints “I love computers” five (5) times Pseudocode 1. Start 2. count = 0 Exercise: Write the complete flowchart for this algorithm 3. While count < 5 Display "I love computers!" 4. Increment count 5. Endwhile 6. Stop Exercise 1. Write a computer solution in pseudocode and flowchart print the numbers 1 to 5 using while loop. 2. Write a computer solution in pseudocode and flowchart to calculate the average of “N” students using the while loop Do…While Control Structure (Loop) Like a while loop, a do-while loop is a loop that repeats while some condition is satisfied. Unlike a while loop, a do-while loop tests its condition at the end of the loop. This means that its sequence of activities always runs at least once. The Do While Control Structure A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time. The syntax of a do...while loop in C programming language is do { statement(s); } while( condition ); The Do While Control Structure In case the condition is true, the control goes back to the beginning of the loop. If the condition is false, the control breaks out of the loop. This means that the statements inside the loop are executed before the condition is tested. So the do while loop should be used in all scenarios where the loop body needs to be executed at least once Do ….While Example An algorithm in pseudocode to print Dry run of the program Hello World two times. using do.. while 1. Program starts. 2.. i is initialised to 2. 3. Execution enters the do loop 1. Start a) "Hello World" gets printed 1st time. 2. i = 2 b) Update is done. (i is increased by 1) Now i = 2. 3. Do 4. Condition is checked. 1. Print “Hello World” 2 < 2 yields false. 5. 2. Increase i by 1 4. While i

Use Quizgecko on...
Browser
Browser