Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming for Mechanical Engineers (ME 319)

Similar presentations


Presentation on theme: "Introduction to Programming for Mechanical Engineers (ME 319)"— Presentation transcript:

1 Introduction to Programming for Mechanical Engineers (ME 319)
Lecture 7.1

2 Quote of the day “All that we are is the result of what we have thought. The mind is everything, what we think we become!” - Buddha “Watch your thoughts, for they become words. Watch your words, for they become actions. Watch your actions, for they become habits. Watch your habits, for they become character. Watch your character, for it becomes your destiny.”

3 Things you should have known so far!
True-False or 0-1 representation. Logic design Conditional statements. If-elseif-else-end switch-case-otherwise-end. With today’s class, you will be able to write a full program that would solve a problem with several conditions and loops implemented to reflect the problem’s characteristics.

4 Loops: importance Loops are designed and written to execute some commands several times. Why do we need to execute some commands repeatedly? Say we have a company with a 300 employees (!) If there will be a onetime increase for April’s salary, and whoever’s salary is less than $3000 will receive a $100, and those who are between $3k and $4k are to receive $80, those with $4k and more wont receive any increase. Can you write a file that will do it without the use of loops? Alternative route: Plug in an easy-to-construct loop that will execute certain commands 300 times.

5 Loops: importance A bank keeps track of its clients credit lines. It notifies them whenever their expenses (anytime during a calendar month) exceeds 85% of their credit limits. Can you write a real-time program that tracks any change in a client account? Alternative route: How about an easy while loop??

6 Is condition still TRUE?
Loops: flow chart Within the body of a loop, some variables are assigned new values based on the conditions and the variables being passed. This re-assignment of variables is responsible for keeping the condition valid (true) or invalid (false). If a condition is kept valid, the loop continues to execute, if it is invalid, the program leaves the loop and continue executing commands that come after the loop. Is condition still TRUE? END Group of commands START FALSE TRUE LOOP

7 Types of Loops: ‘for-end’ loop
Syntax: for i = p:q:r group of commands end for a = 1:10 END Group of commands START else If 1<=a & a<=10 LOOP ‘i’ is called a counter. It starts from a value ‘p’. It is incremented by ‘q’ each pass. It Keeps incrementing till it reaches ‘r’. Once it reaches ‘r’, the program leaves the loop. The importance of the for-loop comes from the fact that we DO know in advance how many times we need to execute certain commands.

8 Types of Loops: ‘while-end’ loop
Syntax: while condition(s) say (x >= y) | (x < 0) group of commands end while a<25 END Group of commands START else If a<25 LOOP Here, there is no counter. There should be some sort or reassignment to variable ‘x’ so the condition becomes false and the program leaves the loop. The importance of the while-loop comes from the fact that we DON’T know in advance how many times we need to execute certain commands.

9 Solution for the Example: salary increment problem
Say we have a company with a 300 employees (!) If there will be a onetime increase for April’s salary, and whoever’s salary is less than 3000 will receive a $100, and those who are between 3k and 4k are to receive $80, those with 4k and more wont receive any increase. salary = [ ]; for i = 1:3 if salary(i) < 3000 salary(i) = salary(i) + 100; elseif (salary(i) >= 3000) & (salary(i) < 4000) salary(i) = salary(i) + 80; else salary(i) = salary(i); end salary =

10 Solution for the Example: credit limit problem
format bank balance = 0; Credit_limit = input('Enter credit limit '); while balance < 0.85*Credit_limit available_credit = Credit_limit - balance; purchase = input('How much the client paid? '); if purchase > available_credit fprintf('Not enough credit for this transaction\n') balance = balance - purchase; end balance = balance + purchase; if balance >= 0.85*Credit_limit percentage = balance*100/Credit_limit; fprintf('\nBe careful, you are about to go over the limit\nYou owe the bank $%7.2f \nwhich is %4.2f%% of your credit line of $%7.2f\n\n',balance,percentage,Credit_limit)

11 Solution for the Example: credit limit problem
Enter credit limit 1000 How much the client paid? 500 How much the client paid? 200 How much the client paid? 400 Not enough credit for this transaction How much the client paid? 175 Be careful, you are about to go over the limit You owe the bank $ which is % of your credit line of $

12 Loops: useful hints Most of the time we write programs for general purpose use. By this we mean that we don’t write special case programs that have fixed number of elements or certain values for variables. A complexity is always associated with such programs, such as: how can we calculate the number of elements in an array? What is the number of columns in the given matrix? How to find the location and value of the max/min element in an array? Command Description Example n = length(y) If y is an array, it returns the number of elements of y. If y is a matrix, it returns the number of columns >> exam = [ ]; >> grades = length(exam) grades = 3 [a,b] = size(A) Returns the number of rows of A in a and the number of columns of A in b >> matrix_A = [1 2 3;4 5 6]; >> [rows,columns] = size(matrix_A) rows = 2 columns = [P,LOC_P] = max(S) [Q,LOC_Q] = min(S) Returns the max/min value of S in P/Q. also, returns the location of the max/min value of S in LOC_P/LOC_Q. >> [P,LOC_P] = max(exam) P = 55 LOC_P = Mohammad Y. Saadeh

13 Is condition still TRUE?
Loops: flow chart Within the body of a loop, some variables are assigned new values based on the conditions and the variables being passed. This re-assignment of variables is responsible for keeping the condition valid (true) or invalid (false). If a condition is kept valid, the loop continues to execute, if it is invalid, the program leaves the loop and continue executing commands that come after the loop. Is condition still TRUE? END Group of commands START FALSE TRUE LOOP

14 Termination criterion: break
for/while loop END Group of commands START break IF TRUE FALSE Rest of program break usually appears in a conditional statement to terminate (interrupt) the execution of the loop. When it reaches a break command, MATLAB jumps to the end line of a loop and continues executing the rest of the program.

15 Termination criterion: continue
for/while loop END Group of commands START continue IF TRUE FALSE Rest of program continue usually appears in a conditional statement to terminate (interrupt) the execution of a pass in the loop. When reaches a continue command, MATLAB doesn’t execute the rest of the pass. Instead, it jumps to the beginning of the loop and starts with a new pass.

16 Programming example: break
format bank flag = 0; balance = 0; Credit_limit = input('Enter credit limit '); for i = 1:4 available_credit = Credit_limit - balance; purchase = input('How much the client paid? '); if purchase > available_credit flag = 1; break end i % display the pass number balance = balance + purchase; if flag == 1 fprintf('The loop was terminated\n\n') Enter credit limit 1000 How much the client paid? 500 i = 1 How much the client paid? 400 2 How much the client paid? 200 The loop was terminated

17 Programming example: continue
format bank balance = 0; Credit_limit = input('Enter credit limit '); for i = 1:4 available_credit = Credit_limit - balance; purchase = input('How much the client paid? '); if purchase > available_credit continue end i % display the pass number balance = balance + purchase; Enter credit limit 1000 How much the client paid? 500 i = 1 How much the client paid? 200 2 How much the client paid? 400 How much the client paid? 100 4

18 Thank you for the attention
Any Questions and Suggestions please…


Download ppt "Introduction to Programming for Mechanical Engineers (ME 319)"

Similar presentations


Ads by Google