Control Statements

Computer Programming in

MATLAB Assoc. Prof. Dr. M. Akif CEVİZ Atatürk University Engineering Faculty Department of Mechanical Engineering

Atatürk University

Control Statements

Control statements:  Conditional statements: if, else, elseif, switch  Repetition statements: while, for

Atatürk University

‘disp’ command

Control Statements

You can also use disp command to display a message enclosed in apostrophes (called a string), or to display an array.

To display a message and a value on the same line, use the following trick:

Atatürk University

Example 1

Control Statements

Write a Matlab program taking the numerator and denominator from the keyboard, and writing «please enter a number other than zero» message if user enters zero to denominator, and then computing the ratio of them. clear N = input(‘enter numerator: '); D = input(‘enter denominator: '); if D==0 Disp(‘please enter a number other than zero‘) else ratio= N/D end

Note that if user enters zero as denominator for a second time, D will be zero, and Matlab produces an error. Atatürk University

Control Statements

Example 2

Write a Matlab program taking the month number from keyborad and then displaying the day number of that month. clear m= input(‘which month's day number would you like to learn (1-12)= ' ); if m==1 | m==3 | m ==5 | m==7 | m==8 | m==10 | m==12 disp(‘it takes 31 days‘) elseif m==2

disp(' it takes 28 days‘) else disp(' it takes 30 days‘) end

Footnote: if user enter a number other than the numbers from 1 to 12, the program will display «it takes 30 days» message. Please change it to display «please enter a number from 1 to 12» message. Atatürk University

Example 3

Control Statements

Write a Matlab program that asks the user for a mid-term and final grade for a student and then determining the letter grade to the following scheme:

Condition --------mean >= 90 75 > fprintf('The temperature is %g °C \n', T) The temperature is 100 °C

Atatürk University

Control Statements

Example 8 Write a Matlab program that displays the following table.

Number Team --------------------1. Fenerbahçe

2. Fenerbahçe 3. Fenerbahçe

fprintf(NUMBER TEAM\n') fprintf('-------------------\n') for i=1:5 fprintf('%g. Fenerbahçe\n',i) end

4. Fenerbahçe

5. Fenerbahçe

Atatürk University

Example 9

Control Statements

Write a Matlab program that displays the numbers from 1 to 10 and squares of them in two column.

Before writing the program, examine the followings. balance = 12345; rate = 0.09; interest = rate * balance; balance = balance + interest; fprintf( 'Interest rate: %g New balance:%g\n',rate, balance )

Interest rate: 0.09 New balance:13456

Atatürk University

Control Statements

Homework 1:

In a cargo company, a pricing policy according to the package weight is as follows; Base price is 5 TL for packages up to 2 kg. For the packages heavier than 2 kg, 0.5 TL should be added to the base price for every 1 kg. If the packages is heavier than 35 kg, 10 TL should be added to the total price. The packages heavier than 50 kg are not accepted because of worker’s health. Write a Matlab program that computes the price according to the above-mentioned scheme and then prints the following table for 1:50 kg pacages. (Assume that the scale readings are only integer values) Weight (kg)

Price (TL)

1

5

2

5

3

5.5

4

6





50

39

Atatürk University

Control Statements

while-end

A while loop repeats a group of commands as long as a specified condition is true.

Syntax: while expression statements

… end

Atatürk University

Control Statements

Example 10

Write a Matlab program that computes the summation of the number from 5 to 10.

a=4;

T=0 while a