ALICE PROJECT 3 REVIEW LOGIC STRUCTURE OF ALGORITHMS - Every computer is an algorithm - a step by step process - In modern OOP, each method is an algorithm - Algorithms are sequenced by default - steps in algorithm form a kind of sequential logic THREE MAJOR LOGICAL STRUCTURES IN ALGORITHMS: - branching routines - loops - linear sequences (No branch or loops) These 3 elements are the building blocks for algorithms FLOW CHART: a diagram that shows the structure of an algorithm - rectangles represent algorithm - diamond shaped boxes represents branching - Oval shaped boxes represents beginning and ends LINEAR SEQUENCES: - clear starting and ending point - entry and exit conditions have to be clearly stated What conditions need to exist before the sequence starts What can we expect the situation when the sequence is finished - sequence of instructions must be complete - sequence of instructions need to be in the proper order - all instruction in the sequence must be correct and valid, a single mistake could cause the entire algorithm to fail BRANCHING ROUTINE (SELECTION SEQUENCES): - occurs whenever the flow of instructions in a computer program splits into separate paths - there must be a condition to determine which path the computer will follow each time the selection sequence is being executed (Binary branching splits in 2 paths, true or false) - there are 2 types of binary branching sequences: binary bypass: an instruction is either executed or passed Example: if do else do binary choice: one of two instructions, NOT both, is executed. Example: if do else do MULTIPLE BRANCHING: - a series of If/Else instructions - it can always be rewritten as a series of nested binary branchings Example: If do elseif do else do LOOPING SEQUENCES (REPETITION SEQUENCES): - in loops, word WHILE is used instead of IF Example: while do ------[condition]---False---> ^ | | True | V ---[instruction] It will keep doing the instructions until the condition becomes false - loops have conditions that controls whether or not the repetition sequence will be executed - a set of instructions to be repeated in a loop is called a body of the loop PRETEST LOOPS - if test comes BEFORE the body of the loop POSTTEST LOOPS - if test comes AFTER the body of the loop --------[test]---False---> ^ | | True PRETEST LOOP | V ---[instruction] --------[instruction]-->[test]---False---> ^ | | True POSTTEST LOOP |_____________________| COUNT-CONTROLLED AND SENTINEL LOOPS: Major 4 parts: - Initialization: an instruction that sets the first value of the control variable - Test: looks at the control variable to see if the loop should be executed - Processing: instructions or set of instructions to be repeated in the loop - Update: changes the value of the control variable each time it goes through the loop Example: i = 1 [Initialization] while i < 5 do [Test] say i [Processing] i = i + 1 [Update] BOOLEAN LOGIC The six different logical comparison operation in computer programming - A equal B - A does not equal B - A is less than B - A is greater than B - A is less than or equal to B - A is greater than or equal to B 3 basic operations: AND: Need 2 equal to be true true and true = true true and false = false false and true = false false and false = true OR: anyone is true is true true or true = true true or false = true false or true = true false or false = false NOT: opposite not true = false not false = true MORE ABOUT: Using comments comments are a type of software documentation, it can help when programming The ACM Association for Computing Machinery: One of the oldest and most respected organizations for computer science professionals, educators, and students. (www.acm.org) Software diagrams Computer scientists and software engineers uses diagrams to describe a computer software