71 Views

Download Presentation
##### Computer Programming (ECGD2102 ) Using MATLAB

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Computer Programming(ECGD2102 ) Using MATLAB**Lecture (4): Control Flow (Chapter 2) Instructor: Eng. Eman Al.Swaity**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**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.**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.**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.**GUIDELINES FOR DRAWING A FLOWCHART**Flowcharts are usually drawn using some standard symbols, Some standard symbols are shown :**EXAMPLES ON FLOWCHARTING**Example1: Draw a flowchart to find the sum of first 50 natural numbers.**EXAMPLES ON FLOWCHARTING**Example2:Draw a flowchart to find the largest of three numbers A,B, and C**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.**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.**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.**Relational Operators**Examples: Relational operations can also be performed on matrices of the same shape, e.g.,**Relational Operators**Examples:**Logical Operators:**Logical Operators: Logical operators are used to combine logical expressions (with “and” or “or”), or to change a logical value with “not”**Logical Operators**Examples:**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.**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**if Constructs**The block of statements is executed only if the expression is true.**if Constructs**Example: Guessing program**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**if. . . else. . .end**Example1:**if. . . else. . .end**Example2:**if. . . else. . .end**Example2-cont:**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_3 ....................... else statements end**if. . . elseif. . . end**Evaluate exp_1 if exp_1 statements1 elseif exp_2 statements2 elseif exp_3 statements3 end Execute statements1 true false Evaluate exp_2 Execute statements2 true false Evaluate exp_3 Execute statements3 true false Continue after end Could also have an else before the end**if. . . elseif. . . end**Example1 if (grade >= 90) % 90 and above fprintf(‘A’); elseif ( grade >= 80 ) % 80-89 fprintf(‘B’); elseif ( grade >= 70 ) % 70-79 fprintf(‘C’); elseif ( grade >= 60 ) % 60-69 fprintf(‘D’); else % less than 60 fprintf(‘F’); end**if. . . elseif. . . end**Example2**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**if. . . elseif. . . end**Example4**if. . . elseif. . . end**Example4-cont**if. . . elseif. . . end**Example 5 : Solve for x: Ax2 + Bx + C = 0 where A,B,C are arbitrary constants**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**if. . . elseif. . . end**Example 5-cont :**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 ]**The switch Construct**Example 1:**The switch Construct**Example 2:**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**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**The switch Construct**Example 5:**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**End of the Lecture**Let Learning Continue