MODULE 8 B PROCEDURAL PROG USING STRUCTURES Overview

MODULE 8 B : PROCEDURAL PROG USING STRUCTURES : Overview This involves the implementation of the logic structures involved algorithm in the form of instructions in the computer system. The decision logic will be implemented using repetitive in the programming language Mr. Joseph Ecklu (DCS) Slide 1

Reading List • Read Chapter 1 of C++ Programming in easy steps by Mike Mc. Grath • Chapter 1 of C++ Programming by Malik, D. S. Mr. Joseph Ecklu (DCS) Slide 2

STRUCTURE • Definition A Structure is a basic unit of programming logic Each structure is a SEQUENCE SELECTION or LOOP Mr. Joseph Ecklu (DCS) Slide 3

STRUCTURE - Sequence Mr. Joseph Ecklu (DCS) Slide 4

STRUCTURE - Sequence • Operation you perform an action or task and then you perform the next action in order. • Can contain any number of tasks but there is no chance to branch off or skip any of the tasks Mr. Joseph Ecklu (DCS) Slide 5

STRUCTURE - Sequence • Once you start a series of actions in a sequence, you must continue step-by-step until the sequence ends Mr. Joseph Ecklu (DCS) Slide 6

STRUCTURE - Sequence • Example in a C++ program #include <iostream> using workspace std; main() { cout << “University of Ghanan”; cout << “Computer Sciencen”; system(“pause”); return 0; }. . UsersJECKLUDesktopcprogsnnsequence. cpp. . UsersJECKLUDesktopcprogsnnsequence. exe Mr. Joseph Ecklu (DCS) Slide 7

STRUCTURE - Selection Mr. Joseph Ecklu (DCS) Slide 8

STRUCTURE - Selection • Operation you ask a question and depending on the answer you take one of two courses of action Then no matter which path you follow you continue with the next task Mr. Joseph Ecklu (DCS) Slide 9

STRUCTURE - Selection • Sometimes described in programming languages as if – then – else example if some. Condition is True Then do one. Process else do the. Other. Process Mr. Joseph Ecklu (DCS) Slide 10

STRUCTURE - Selection 1. 2. 3. 4. 5. 6. #include <iostream> using namespace std; main(){ char sex. Of. Animal = 'm'; // I want to print the True condition 7. if (sex. Of. Animal == 'm' ) 9. 10. 11. 12. 13. else { 8. Mr. Joseph Ecklu (DCS) { cout << "University of Ghanan"; } cout << "Department of Computer Sciencen"; } system("pause"); return 0; } Slide 11

STRUCTURE - Loop Mr. Joseph Ecklu (DCS) Slide 12

STRUCTURE - Loop • Operation you continue to repeat actions based on the answer to a question Common type you first ask a question if the answer requires an action you perform the action Mr. Joseph Ecklu (DCS) Slide 13

STRUCTURE - Loop • Operation and ask the original question again If the answer requires that the action be taken again you take the action And the ask the original question again This continues until the answer to the question is such that the action is no longer required, then you exit the structure Mr. Joseph Ecklu (DCS) Slide 14

STRUCTURE - Loop • Also referred to as Repetition or Iteration Mr. Joseph Ecklu (DCS) Slide 15

• An example of looping structure is called the while loop • The general from of the while loop is while (expression) statement

• Consider the following C++ program segment i=0 while (I <= 20) { cout << I << “ ”; i = i + 5; } cout << endl; SAMPLE RUN: 0 5 10 15 20

Mr. Joseph Ecklu (DCS) Slide 18

Session Summary Control structures helps in the flow of a program. Control structures covered in this session are – Sequence – Selection – Loops
- Slides: 19