ITEC113 Algorithms and Programming Techniques

Slides:



Advertisements
Similar presentations
PSEUDOCODE & FLOW CHART
Advertisements

 Control structures  Algorithm & flowchart  If statements  While statements.
ITEC113 Algorithms and Programming Techniques
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Program Design and Development
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
A High-Level Model For Software Development
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
Pseudocode.
Chapter 3 Planning Your Solution
The Program Design Phases
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Fundamentals of C programming
DCT 1123 Problem Solving & Algorithms
Programming Techniques I SCJ1013
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Chapter 2 - Algorithms and Design
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Lecture 5: Developing Procedural Thinking (How to think like a programmer) B Burlingame 30 Sept 2015.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Visual Basic Programming
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Controlling Program Flow with Decision Structures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CPS120: Introduction to Computer Science Decision Making in Programs.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016.
Program Program is a collection of instructions that will perform some task.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
 Problem Analysis  Coding  Debugging  Testing.
Learning outcomes 5 Developing Code – Using Flowcharts
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
GC211Data Structure Lecture2 Sara Alhajjam.
Introduction To Flowcharting
Programming Fundamentals
Lecture 2: Logical Problems with Choices
Unit# 9: Computer Program Development
Structured Program
Chapter 4 - Program Control
3 Control Statements:.
Introduction to Algorithms and Programming
Chapter 3: Selection Structures: Making Decisions
Chapter 4 - Program Control
Basic Concepts of Algorithm
Structural Program Development: If, If-Else
Presentation transcript:

ITEC113 Algorithms and Programming Techniques Lecture 2 : Selection Statements

Recap

Definition of algorithm A procedure for solving a mathematical problem in a finite number of steps that frequently involves repetition of an operation a step-by-step procedure for solving a problem or accomplishing some end especially by a computer Source: http://www.merriam-webster.com/

So what is an algorithm? A recipe or a set of step-by-step instructions that describe a solution to a given problem. In the context of computer programming “well-ordered collection of unambiguous and effectively computable operations, that when executed, produces a result and halts in a finite amount of time.”

Characteristics of an Algorithm Well-ordered: the steps are in a clear order Unambiguous : the operations are understood by the computer module without any further simplification Effectively computable: the computer module can actually complete the execution of the specified operations in a finite amount of time

Method for Developing an Algorithm Define the problem : Describe the problem in clear and brief terms List inputs : Clearly specify information needed for the solution of the problem (can be input from the keyboard, a file etc) List outputs : Describe the result that the algorithm will produce Describe the steps needed to accomplish the desired result : How to manipulate/use the inputs to produce the desired output Test the algorithm : Verify that the algorithm works.

Structured Programming All programs can be written using three control structures one statement is executed after another Sequence : A statement is executed or skipped depending on whether a condition evaluates to TRUE or FALSE. Example: if, switch Selection : Statements are executed repeatedly until a condition evaluates to TRUE or FALSE. Example: while, for Repetition : All programs can be written using three control structures Sequence : one statement is executed after another Selection : A statement is executed or skipped depending on whether a condition evaluates to TRUE or FALSE. Example: if, switch Repetition : Statements are executed repeatedly until a condition evaluates to TRUE or FALSE. Example: while, for

Pseudocode Consists of natural language-like statements that precisely describe the steps of an algorithm or program Statements describe actions Focuses on the logic of the algorithm or program No language-specific elements Written at a level so that the desired programming code can be generated almost automatically from each statement Keywords written using upper case letters (Optional) Steps are numbered. Subordinate numbers and/or indentation are used for dependent statements in selection and repetition structures (Optional) Variables and Constants are declared

PseudoCode Constructs Assignment: Set num1 to 1 Num1 1 Computation Use all arithmetic operators: addition (+), subtraction (-) . Division (/), multiplication (*), modulus (%) … Input Input : to enter from the keyboard Read : to read from a file Output Display : to display on screen Print : to print on the printer Selection IF .. END IF IF .. ELSE …END IF IF .. ELSE IF .. ELSE …END IF SWITCH .. CASE … Repetition (Will be covered later) You can use either one of these assignment statements. We prefer the second one

FLOWCHARTING SHAPES Terminal Flowcharting Symbols                                                                                                                                                             FLOWCHARTING SHAPES   Symbol Name Function Terminal Indicates the starting or ending of the program Input Used for data entry from keyboard. Display Used for displaying on screen Process Indicates any type of internal operation inside the Processor or Memory  (STORE INFORMATION & DO CALCULATIONS -variables) Decision Used to ask a question that can be answered in a binary format (Yes/No, True/False) Connector Allows the flowchart to be drawn without intersecting lines or without a reverse flow. Predefined Process Used to invoke a subroutine or an interrupt program.

Rules for flowcharting All boxes of the flowchart are connected with Arrows. (Not lines) Flowchart symbols have an entry point on the top of the symbol with no other entry points. Exception : connector symbol circle used in loops! The exit point for all flowchart symbols is on the bottom. Exception: The Decision symbol has two exit points; these can be on the sides or the bottom and one side. Generally a flowchart will flow from top to bottom, and left to right. Connectors are used to connect breaks in the flowchart. Examples are: From one page to another page. From the bottom of the page to the top of the same page. Subroutines have their own and independent flowcharts. All flow charts start with a Terminal or Predefined Process symbol. All flowcharts end with a terminal.

Benefits of Flowcharts Make communication on the logic of a system easier. Make analysis of the problem more effective and easier Serve as a good program documentation, which is needed for various purposes. Act as a guide or blueprint during the systems analysis and program development phase. Aid in debugging process. Make maintenance of programs easier

Sequence Statements INPUT num1 Sq  num1*num1 DISPLAY sq Statements are executed one after the other in the same order as they are written Default execution! Example : Read a number from keyboard and print its square on screen START INPUT num1 Sq  num1*num1 DISPLAY sq num1 sq  num1*num1 sq END

Selection Statements Selection statements: decide whether or not to execute a particular statement Also called the conditional statements or decision statements IF Statement is a flexible construct where you can use any condition that evaluates to TRUE or FALSE. Branching is determined by the condition. Switch Statement: Branching depends on the values the parameter may take.

Selection Statements: Simple If Decides whether the statement-block of the if statement will be executed or not. The statement-block may be a single statement or a group of statements. If the test expression is true, the statement-block will be executed otherwise the statement-block will be skipped

Selection Statements: Simple If Example: Prompt the user to enter a number and print “positive” if number is greater than 0. START num1 INPUT num1 IF num1>0 DISPLAY “Positive” ENDIF ? Num1>0 FALSE TRUE “Positive” END

Selection Statement: If ..else This is an extension of simple if where one of two branches is selected by the if condition. If the test expression is true , then the true-block statement(s), immediately following the if statement are executed otherwise the false-block statement(s) are executed. Either true-block or false-block will be executed, not both.

Selection Statements: If .. else Example: Prompt the user to enter a number and print “positive” if number is greater than 0, otherwise print “negative”. START num1 INPUT num1 IF num1>0 DISPLAY “Positive” ELSE DISPLAY “Negative” ENDIF ? num1>0 FALSE TRUE “Positive” “Positive” END

Part 2 : New Material Nested Selection statements Loops

Selection Statements: If .. Elseif ladder Multipath decisions are represented using IF ELSEIF ladder. A multipath decision is a chain of IF statements in which the statement associated with each ELSE is an IF. The conditions are evaluated from the top downwards. As soon as a TRUE condition is found, the statement associated with it is executed and IF statement exits. When all the all conditions enumerated as ELSEIF statements become FALSE, the final ELSE containing the default statement will be executed.

Selection Statements: If .. else Example: Prompt the user to enter a number and print “positive” if number is greater than 0, if the number if less than 0 print “negative”, otherwise print “Zero” START num1 INPUT num1 IF num1>0 DISPLAY “Positive” ELSEIF num1<0 DISPLAY “Negative” ELSE DISPLAY “Zero” ENDIF FALSE ? num1<0 FALSE ? num1>0 TRUE TRUE “Positive” “Negative” “Zero” END

Selection Statements: Nested If’s When a series of decisions are involved, more than one if.....else statement may be used in nested form. Nesting can be done in IF, ELSEIF or ELSE parts if the if statement. It is possible to nest very large number of if statements but readability of the program/algorithm will be reduced!

Selection Statements: If .. else Example: Prompt the user to enter a number and print “positive” if number is greater than 0, if the number if less than 0 print “negative”, otherwise print “Zero” START num1 INPUT num1 IF num1<0 DISPLAY “Negative” ELSE IF num1>0 DISPLAY “Positive” DISPLAY “Zero” ENDIF ? num1>0 FALSE ? num1>=0 FALSE TRUE TRUE “Positive” “Negative” “Zero” END

Selection Statements: If .. else Example: Prompt the user to enter a number and print “positive” if number is greater than 0, if the number if less than 0 print “negative”, otherwise print “Zero” START num1 INPUT num1 IF num1>=0 IF num1>0 DISPLAY “Positive” ELSE DISPLAY “Zero” ENDIF DISPLAY “Negative” FALSE ? num1>=0 “Negative” TRUE FALSE ? num1>0 “Zero” TRUE “Positive” END

Looping Statements Loops are used to perform repetitive tasks such as printing sequential numbers on screen. The statements enclosed inside a loop are usually executed more than once. Each execution is called an iteration.

A live performance of walking to a chair and sitting on it! Flowchart Loops A live performance of walking to a chair and sitting on it!

START TAKE ONE PACE FORWARD IS CHAIR IN FRONT OF YOU? NO YES

START TAKE ONE PACE FORWARD IS CHAIR IN FRONT OF YOU? NO YES

START TAKE ONE PACE FORWARD IS CHAIR IN FRONT OF YOU? NO YES

START TAKE ONE PACE FORWARD IS CHAIR IN FRONT OF YOU? NO YES TURN AROUND AND SIT ON IT STOP

WHILE …… ENDWHILE loop : characteristics Iteration count is greater than and equal to 0 (zero). If the condition is initially FALSE Loop Body will never be executed. Working Mechanism : First Check then Execute The loop is repeated as long as the condition is true!

WHILE …… ENDWHILE loop : structure YES NO ? Condition Instruction 1 Instruction 2 …. WHILE condition Instruction-1 Instruction-2 … ENDWHILE

WHILE …… ENDWHILE loop : example Display all integer numbers between 1 and 10 on screen. The boundaries (1 and 10) should also be displayed. YES NO ? i<=10 START i  1 i END i  i + 1 i 1 WHILE i <= 10 Display i i  i +1 ENDWHILE

DO…..WHILE loop : characteristics Iteration time is greater than and equal to 1 . Even if the condition is initially FALSE the Loop will be executed at least once. Working Mechanism is First Execute and Then Check Iterations are carried out while condition is TRUE, and stopped when the condition is FALSE.

DO…..WHILE loop : structure YES NO ? Condition Instruction 1 Instruction 2 …. DO Instruction-1 Instruction-2 … WHILE condition

DO…..WHILE loop : example Display all integer numbers between 1 and 10 on screen. The boundaries (1 and 10) should also be displayed. YES NO ? i<=10 START i  1 i  i + 1 i END   i 1 DO Display i i  i+1 WHILE i <= 10

REPEAT…..UNTIL loop : characteristics Iteration time is greater than or equal to 1 . Even if the condition is initially TRUE the Loop will be executed at least once. Working Mechanism is First Execute and Then Check Iterations are carried out while condition is FALSE, and stopped when the condition is TRUE.

REPEAT…..UNTIL loop : structure NO YES ? Condition Instruction 1 Instruction 2 …. REPEAT Instruction-1 Instruction-2 … UNTIL condition

REPEAT…..UNTIL loop : example Display all integer numbers between 1 and 10 on screen. The boundaries (1 and 10) should also be displayed. NO YES ? i>10 START i  1 i END i  i + 1  i  1 REPEAT i  i + 1 Display i UNTIL i > 10

FOR … ENDFOR loop : characteristics Repeated a "specific" number of times, determined by the program or the user.  "counts" the number of times the body will be executed.   good choice when the number of repetitions is known, or can be supplied by the user.

FOR … ENDFOR loop : structure NO YES lowerlimit stepsize Instruction 1 Instruction 2 …. Upper Limit FOR loopcounter=lowerlimit to upperlimit with increment = stepsize Instruction-1 Instruction-2 … ENDFOR

FOR … ENDFOR loop : example Display all integer numbers between 1 and 10 on screen. The boundaries (1 and 10) should also be displayed. YES NO START i  1 i  i + 1 i END ? i <=10  i  1 FOR i =1 to 10 with increments = 1 DISPLAY total END FOR

The End