1 / 41

Computer Programming (ECGD2102 ) Using MATLAB

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.

amber
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 Flowcharts are usually drawn using some standard symbols, Some standard symbols are shown :

  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: 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_3            ....................... else            statements end

  25. 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

  26. 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

  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: Ax2 + 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. End of the Lecture Let Learning Continue

More Related