1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)

Slides:



Advertisements
Similar presentations
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
Advertisements

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Reference :Understanding Computers
 Control structures  Algorithm & flowchart  If statements  While statements.
Flowcharts.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
ITEC113 Algorithms and Programming Techniques
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Chapter 4: Control Structures: Selection
The Program Design Phases
Fundamentals of C programming
ALGORITHMS AND FLOWCHARTS
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Chapter 2 - Algorithms and Design
Computer Science Selection Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
ITEC113 Algorithms and Programming Techniques
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Algorithm Design.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
CPS120: Introduction to Computer Science Decision Making in Programs.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
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.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Program Design & Development EE 201 C7-1 Spring
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
Pseudocode (pronounced SOO-doh-kohd)  is a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled.
Program design Program Design Process has 2 phases:
ALGORITHMS AND FLOWCHARTS
Chapter 4 – C Program Control
GC101 Introduction to computers and programs
Flowcharting Guidelines
Chapter 4: Making Decisions.
Programming Fundamentals
Microsoft Visual Basic 2005 BASICS
ALGORITHMS AND FLOWCHARTS
Lecture 2: Logical Problems with Choices
MSIS 655 Advanced Business Applications Programming
Chapter 4 - Program Control
ALGORITHMS AND FLOWCHARTS
` Structured Programming & Flowchart
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Using Decision Structures
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 4 - Program Control
REPETITION Why Repetition?
Presentation transcript:

1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)

2 Objectives ( CHAPTER (2):Control Flow) At the end of this lesson, you will be able to understand:  The meaning and the basic parts of flowchart.  Study the different methods for control flow in MATLAB  Introduce the three forms of IF statement.  Use the SWITCH-CASE statement

3 MEANING OF A FLOWCHART  A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem.  Flowcharts are generally drawn in the early stages of formulating computer solutions.  Flowcharts facilitate communication between programmers and business people.

4 MEANING OF A FLOWCHART  These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems.  Once the flowchart is drawn, it becomes easy to write the program in any high level language. Often we see how flowcharts are helpful in explaining the program to others. Hence, it is correct to say that a flowchart is a must for the better documentation of a complex program.

5 Flowchart representation  A sequence step is represented by a rectangle.  The logic flow is indicated by lines and arrows.  This kind of representation is called a flowchart or schematic diagram.

6 GUIDELINES FOR DRAWING A FLOWCHART Start or end of the program Computational steps or processing function of a program Input or output operation Decision making and branching Connector or joining of two parts of program Flowcharts are usually drawn using some standard symbols, Some standard symbols are shown : Magnetic Tape Magnetic Disk Off-page connector Flow line Annotation Display

7 EXAMPLES ON FLOWCHARTING Example1: Draw a flowchart to find the sum of first 50 natural numbers.

8 EXAMPLES ON FLOWCHARTING Example2:Draw a flowchart to find the largest of three numbers A,B, and C

9 Flow Control To enable the implementation of computer algorithms, a computer language needs control structures for: Repetition: looping or iteration Conditional execution: branching Comparison We will consider these in reverse order.

10 Comparison Comparison Comparison is achieved with relational operators. Relational operators are used to test whether two values are equal, or whether one value is greater than or less than another. The result of a comparison may also be modified by logical operators.

11 Relational Operators Relational Operators: Relational operators are used in comparing two values. The result of applying a relational operator is a logical value, i.e. the result is either true or false.

12 Relational Operators Examples: Relational operations can also be performed on matrices of the same shape, e.g.,

13 Relational Operators Examples:

14 Logical Operators: Logical operators are used to combine logical expressions (with “and” or “or”), or to change a logical value with “not”

15 Logical Operators Examples:

16 Logical and Relational Operators Summary: Relational operators involve comparison of two values. The result of a relational operation is a logical (True/False) value. Logical operators combine (or negate) logical values to produce another logical value. There is always more than one way to express the same comparison Free Advice: To get started, focus on simple comparison. Do not be afraid to spread the logic over multiple lines (multiple comparisons) if necessary.

17 Conditional Execution Conditional Execution or Branching: As the result of a comparison, or another logical (true/false) test, selected blocks of program code are executed or skipped. Conditional execution is implemented with if, if...else, and if...elseif constructs, or with a switch construct. There are three types of if constructs 1. Plain if …..end 2. if...else …..end 3. if...elseif …end

18 if Constructs The block of statements is executed only if the expression is true.

19 if Constructs Example: Guessing program

20 if... else...end Multiple choices are allowed with if... else and if... elseif constructs Syntax if expression statements (evaluated if expression is true) else statements ( evaluated if expression is false) end

21 if... else...end Example1:

22 if... else...end Example2:

23 if... else...end Example2-cont:

24 if... elseif... end It’s a good idea to include a default else to catch cases that don’t match preceding if and elseif blocks Syntax if expression_1 statements elseif expression_2 statements elseif expression_ else statements end

25 if... elseif... end if exp_1 statements1 elseif exp_2 statements2 elseif exp_3 statements3 end Evaluate exp_1 Evaluate exp_2 Evaluate exp_3 true Continue after end true false Execute statements1 Execute statements2 Execute statements3 Could also have an else before the end

26 if... elseif... end Example1 if (grade >= 90) % 90 and above fprintf(‘A’); elseif ( grade >= 80 ) % fprintf(‘B’); elseif ( grade >= 70 ) % fprintf(‘C’); elseif ( grade >= 60 ) % fprintf(‘D’); else % less than 60 fprintf(‘F’); end

27 if... elseif... end Example2

28 if... elseif... end Example3 An if statement defines command flow based on a test that can have two or more outcomes. % Odd or even? x = input('What is your favorite integer?') if rem(x,2) == 0 % 'rem' divides x by 2 and returns the remainder disp('That is an even number. You are sooo square!') elseif rem(x,2) == 1 disp('That is an odd number. You are sooo weird!') else disp('That is not an integer, lame-brain!!') end

29 if... elseif... end Example4

30 if... elseif... end Example4-cont

31 if... elseif... end Example 5 : Solve for x: Ax 2 + Bx + C = 0 where A,B,C are arbitrary constants

32 if... elseif... end Example 5-cont : START A,B,C are inputs, known to be real, fixed constants and x is the output, expected to be real but unknown at the start

33 if... elseif... end Example 5-cont :

34 The switch Construct If you need to execute different commands for different results based on a single condition then the switch case is the control you need [ can be a scalar or a string ]

35 The switch Construct Example 1:

36 The switch Construct Example 2:

37 The switch Construct Example 3: % Geometric objection? favorite = input('What is your favorite isometric polygon?') switch(lower(favorite)) case('circle') disp('A circle has no sides (except inside and outside...)') case('triangle') disp('A triangle has 3 sides!') case('square') disp('A square has 4 sides!') otherwise disp('That is way too complicated... I have a headache.') end

38 The switch Construct Example 4: x = 3.0; units = 'mm'; switch units case {'in','inch'} y = 2.54*x; %converts to centimeters case {'m','meter'} y = x*100; % converts to centimters case { 'millimeter','mm'} y = x/10; disp([num2str(x)' in ' units 'converted to cm is :' num2str(y)]) case {'cm','centimeter'} y = x; otherwise disp (['unknown units:' units]) y = nan; end

39 The switch Construct Example 5:

40 The switch Construct Example 6: switch A*B case 0 disp(‘A or B is zero’) case 1 disp(‘A*B equals 1’) case C*D disp(‘A*B equals C*D’) otherwise disp(‘no cases match’) end

41