1 of 24

Programming with MATLAB – Control Structures

COM 103 ICT Applications in Food Industry (1+2)

Practical Schedule 7

Dr. S. Sridevy, Ph.D.

Assistant Professor (Computer Science)

Dept. of Physical Sciences & IT

AEC&RI, TNAU, Coimbatore – 3.

24 June 2020

2 of 24

if Structure

if expression

statements

elseif expression

statements

else

statements

end

S. Sridevy

24 June 2020

3 of 24

Example 1

yourNumber = input('Enter a number: ');

if yourNumber < 0

disp('Negative')

elseif yourNumber > 0

disp('Positive')

else

disp('Zero')

end

S. Sridevy

24 June 2020

4 of 24

S. Sridevy

24 June 2020

5 of 24

S. Sridevy

24 June 2020

6 of 24

S. Sridevy

24 June 2020

7 of 24

S. Sridevy

24 June 2020

8 of 24

What will the two disp statements produce?

a = 5; b = 6; c = 3;

if a > 3 && c < 7

result = a + b*c;

elseif a > 1 && b == 3

result = a*c;

elseif b == 5 || c < 5

result = b-15;

end

disp(‘result = ‘);disp(result)

S. Sridevy

24 June 2020

9 of 24

  • What will the two disp statements produce?

a = 5; b = 6; c = 3;

if a > 3 && c < 7

result = a + b*c;

end

if a > 1 && b == 3

result = a*c;

end

if b == 5 || c < 5

result = b-15;

end

disp(‘result = ‘);disp(result)

S. Sridevy

24 June 2020

10 of 24

Switch statement

switch <switch_expression>

case <case_expression>

<statements>

case <case_expression>

<statements>

...

...

otherwise

<statements>

end

S. Sridevy

24 June 2020

11 of 24

switch angle

case 45

disp(‘Northeast’)

case 135

disp(‘Southeast’)

case 225

disp(‘Southwest’)

case 315

disp(‘Northwest’)

otherwise

disp(‘Direction Unknown’)

end

S. Sridevy

24 June 2020

12 of 24

S. Sridevy

24 June 2020

13 of 24

S. Sridevy

24 June 2020

14 of 24

grade = 'B';

switch(grade)

case 'A'

fprintf('Excellent!\n' );

case 'B'

fprintf('Well done\n' );

case 'C'

fprintf('Well done\n' );

case 'D'

fprintf('You passed\n' );

case 'F'

fprintf('Better try again\n' );

otherwise

fprintf('Invalid grade\n' );

end

When you run the file, it displays:

Well done

Your grade is B

S. Sridevy

24 June 2020

15 of 24

S. Sridevy

24 June 2020

16 of 24

Loop Structure

  • A loop is a structure for repeating a calculation a number of times.
  • Each repetition of the loop is a pass.
  • MATLAB uses two types of explicit loops:
    • for loop, when the number of passes is known ahead of time, and
    • while loop, when the looping process must terminate when a specified condition is satisfied and thus the number of passes is not known in advance.

S. Sridevy

24 June 2020

17 of 24

for Loop

  • General syntax of for loop:

for loop variable = m:s:n

statements

end

  • The expression m:s:n assigns an initial value of m to the loop variable, which is incremented by the value s, called the step value or incremental value.
  • The statements are executed once during each pass, using the current value of the loop variable.
  • The looping continues until the loop variable exceeds the terminating value n.

S. Sridevy

24 June 2020

18 of 24

Develop the script

  • Write a script le to compute the sum of the first 15 terms in the series 5k2 - 2k, k =1, 2, 3, . . . , 15.

S. Sridevy

24 June 2020

19 of 24

Script

total = 0;

for k = 1:15

total = 5*k^2 - 2*k + total;

end

disp (‘The sum for 15 terms is:’)

disp (total)

S. Sridevy

24 June 2020

20 of 24

while Loop

  • General Syntax

while logical expression

statements

end

S. Sridevy

24 June 2020

21 of 24

while Loop (Contd.)

  • MATLAB rst tests the truth of the logical expression. Aloop variable must be included
  • in the logical expression. For example, x is the loop variable in the statement
  • while x < 25. If the logical expression is true, the statements are
  • executed.

S. Sridevy

24 June 2020

22 of 24

while Loop (Contd.)

  • For the while loop to function properly, the following two conditions must occur:

1. The loop variable must have a value before the while statement is executed.

2. The loop variable must be changed somehow by the statements.

  • The statements are executed once during each pass, using the current value of the loop variable. The looping continues until the logical expression is false.

S. Sridevy

24 June 2020

23 of 24

while Loop (Contd.)

  • Write a script file to determine the number of terms required for the sum of the series 5k2 - 2k, k =1, 2, 3, . . . , to exceed 10000. What is the sum for this many terms?

S. Sridevy

24 June 2020

24 of 24

Script

total = 0;

k = 0;

while total < 5

k = k + 1;

total = 1+k + total;

end

disp(‘The number of terms is:’)

disp(k)

disp(‘The sum is:’)

disp(total)

S. Sridevy

24 June 2020

Thank You!