LectENG1008_T2_StructuredProgramDevelopmentInC.pdf
Document Details
Uploaded by IrreplaceableIndianArt5176
Tags
Full Transcript
SIT Internal Programming Topic 2 Structured Programming “Good programmers write code that humans can understand.” Martin Fowler T2-1 SIT Internal...
SIT Internal Programming Topic 2 Structured Programming “Good programmers write code that humans can understand.” Martin Fowler T2-1 SIT Internal Objectives To use basic problem-solving techniques To develop algorithms based on top-down program flow To use if selection statement and the if…else selection statement for condition/selection To use while repetition statement for repeating the execution of a block of code To use assignment, increment and decrement operators To implement counter-controlled repetition and sentinel- controlled repetition T2-2 SIT Internal Algorithms Solution of computing problem Involves running a set of operations in a specific sequence The procedure to solve the problem involves operations to be executed the order in which these operations are being executed this is the Algorithm This is derived from having a thorough understanding of the problem and coming up with an execution plan The sequence or order of execution is crucial and important T2-3 SIT Internal Algorithms Correctly specifying the order in which the operations should execute is important Consider the “rise-and-shine algorithm” followed by one student for getting out of bed and going to school: - Get out of bed, - take off pajamas, - take a shower, - get dressed, - eat breakfast, and - Take a bus to school. This gets the student to school well prepared for classes. T2-4 SIT Internal Algorithms Suppose the steps are performed in a slightly different order - Get out of bed, - take off pajamas, - get dressed, - take a shower, - eat breakfast, - carpool to work. In this case, our student shows up for school soaking wet Specifying the order in which statements should execute in a computer program is called program control T2-5 SIT Internal Pseudocode Pseudocode Helps the programmer to think through the solution before starting to write the program in a programming language (e.g. C) Helps in the development of the Algorithm English like not a programming language English language constructs modeled to look like statements available in most programming languages not to be executed/run on the computer system Can be easily converted to the actual program No fixed syntax for most operations is required T2-6 SIT Internal Pseudocode Pseudocode Only contains action statements and the program flow Steps presented in a structured manner (numbered, indented, and so on) Emphasis is on process, not notation Well-understood forms allow logical reasoning about algorithm behavior Definitions (e.g. int i) are not executable statements instructs the compiler to reserve memory space does not cause any action T2-7 SIT Internal Pseudocode T2-8 SIT Internal Pseudocode T2-9 SIT Internal Flowcharts Flowcharts Graphical representation of an algorithm or part of it Drawn using special symbols (e.g. diamonds, rectangles, circles) Symbols connected by arrows - flowlines not a programming language not to be executed/run on the computer system Similar to pseudocode Useful for program development or to represent the algorithm Illustrates clearly the program flow T2-10 SIT Internal Flowcharts Extracted from https://www.smartdraw.com/flowchart/flowchart- symbols.htm T2-11 SIT Internal Flowcharts Flowcharts Begin is the first symbol and is represented by the rounded rectangle symbol Actions are represented by rectangle symbols Decisions are represented by diamond symbols Connector symbols – small circular symbols are used to connect different portion of the algorithm T2-12 Source : “C How to Program”, Pearson Education Inc SIT Internal Control Structures Sequential execution Code in the program are normally executed sequentially Unless redirected, the computer will execute the C statements one after another in the order they were written There are specific C statements that are able to transfer the control / change the program flow enables the programmer to specify the next statement to be executed instead of the next one in sequence T2-13 SIT Internal Control Structures Selection statements The if selection statement (Section 3.5) will perform an action if a condition is true or skip that action if the condition is false (single selection) The if…else selection statement (Section 3.6) will perform an action if a condition is true and perform another action if the condition is false (double selection) The switch selection statement (Chapter 4) will perform different sets of actions depending on the value of an expression (multiple selection) T2-14 SIT Internal Control Structures Repetition statements (Chapter 4) The while statement The do…while statement The for statement The different types of control statements are used together in a C program to implement the required algorithm There is a single entry and single exit for these control structures – this help to build clear programs T2-15 SIT Internal If statement Selection statement are used to choose among alternative course of action The flowchart of Fig. 3.2 illustrates the single-selection if statement for if (grade >= 60) { printf(“Passed\n”); } The diamond symbol – the decision symbol, means that a decision must be made Pseudocode if the grade obtained is more than or equal to 60 print “Passed” T2-16 Source : “C How to Program”, Pearson Education Inc SIT Internal If statement The decision symbol contains an expression, such as a condition, that can be either true or false Decisions can be based on conditions containing relational or equality operators A decision can be based on any expression If the expression evaluates to zero, it is treated as false; If it evaluates to nonzero, it is treated as true Single entry/single exit statement T2-17 SIT Internal If…else statement The if…else selection statement allows you to specify that different actions are to be performed when the condition is true and when it is false The flowchart of Fig. 3.3 illustrates the double-selection if…else if (grade >= 60) { printf(“Passed\n”); } else { printf(“Failed\n”); } if the grade obtained is more than or equal to 60 Pseudocode print “Passed” else T2-18 print “Failed” Source : “C How to Program”, Pearson Education Inc SIT Internal If…else statement Nested if…else statement for multiple cases Placing if…else statement inside if…else statements Pseudocode C Program printf(“A”) printf(“B”) printf(“C”) printf(“D”) printf(“F”) T2-19 SIT Internal If…else statement Nested if…else statement for multiple cases re-writing if (grade >= 90) printf(“A”); else if (grade >= 80) printf(“B”); else if (grade >= 70) printf(“C”); else if (grade >= 60) printf(“D”); else printf(“F”); T2-20 SIT Internal If…else statement The if selection statement expects only one statement in its body—if you have only one statement in the if’s body, you do not need to enclose it in braces if (grade >= 60) To include several statements in the body printf(“Passed.”); else { of an if, you must enclose the set of printf(“Failed.”); statements in braces { and } printf(“Try again.”); } Conditional operator (?:) It takes 3 operands – 1st operand is a condition, 2nd operand is the value if the condition is true and 3rd operand is for the value if the condition is false printf(grade >= 60 ? “Passed” : “Failed”); The 2nd and 3rd operands can also be actions to be executed grade >= 60 ? printf(“Passed”) : printf(“Failed”); T2-21 SIT Internal while statement A repetition statement (also called an iteration statement) allows you to specify that an action is to be repeated while some condition remains true This action will be performed repeatedly while the condition remains true The flowchart of Fig. 3.4 illustrates the flow of control in the while repetition statement while (product