20
Selection Programming EE 100

Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Embed Size (px)

Citation preview

Page 1: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Selection Programming

EE 100

Page 2: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Outline

introduction Relational and Logical Operators Flow Control Loops Update Processes

Page 3: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Introduction

Programs commands in MATLAB are executed in sequence.

The sequence can be altered using programming structures (control flow -if, switch-, repetition- for, while-).

Decision can be made using relational and logical expressions. (a check if a condition is met or not)

Page 4: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Relational and Logical Operators

For more information: help ops.

Page 5: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Relational and Logical Operators

For relational and logical expressions:Inputs: True is any nonzero number

False is 0 (zero) Outputs: True is 1 (one)

False is 0 (zero) An output array variable assigned to a

relational or logical expression is identified as logical.

Page 6: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Relational and Logical Functions

Page 7: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Flow Control

Simple if Statement The general form of a simple if statement is:

if logical expressioncommands

end

Example:if d < 50

count = count + 1;disp(d);

end

Page 8: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Flow Control

Nested if Statements : general form

if logical expression

commands

if logical expression

Commands

end

end

Page 9: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Flow Control

Example: Nested if Statements

if d < 50

count = count + 1;

if count > 10

disp(‘count > 10’);

end

end

Page 10: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Flow Control- else and elseif Clauses

General form

if logical expression

commands

else

commands

end

General form

if logical expression

commands

elseif logical expression

commands

elseif logical expression

Commands

else

Commands

end

Page 11: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Flow Control- else and elseif Clauses

Example:If average > 86

disp(‘Excellent’)elseif average > 76

disp(‘Very good’)elseif average > 68

disp(‘Good’)else

disp(‘Ya 7aram- accepted’)end

Page 12: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Flow Control- Switch Structure

General form:

switch expression

case test expression 1

commands

case {test expression 2, test expression 3}

commands

···

otherwise

commands

end

Page 13: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Flow Control- Switch Structure

Example:

d = floor(3*rand) + 1

switch d

case 1

disp( ’That’’s a 1!’ );

case 2

disp( ’That’’s a 2!’ );

otherwise

disp( ’Must be 3!’ );

end

Example:

d = floor(10*rand);

switch d

case {2, 4, 6, 8}

disp( ’Even’ );

case {1, 3, 5, 7, 9}

disp( ’Odd’ );

otherwise

disp( ’Zero’ );

end

Page 14: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Loops- for loop

General form:

for index = j:k

statements

end

or

for index = j:m:k

statements

End Floor (last − first)/increment + 1

for i = 1:5

disp(i)

end

for i = 1:2:5

disp(i)

end

Page 15: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Loops- while loop

General form

while condition

statements

end

Example:

count = 0;

While count < 5

disp (count)

count= count + 1;

end

Page 16: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Loops- while loop - break

Example:count = 0;

While count < 5disp (count)count= count + 1;If count == 4

Breakend

end

Page 17: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Avoiding loops

In general, loops should be avoided in Matlab, as they can significantly increase the execution time of a program.

The execution time increases as MATLAB allocate memory each time through the loop.

Usually avoid loops by vectorizing.

Page 18: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Avoiding loops

example: vectorizing

tic

n = 1:10000000;

s = sum( n );

toc elapsed_time = 0.5300

example: loop

tic

s = 0;

for n = 1:10000000

s = s + n;

end

toc elapsed_time = 23.6840

Page 19: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Update Processes

Many problems in science and engineering involve modeling a process where the main variable is updated over a period of time. In many situations, the updated value is a function of the current value.

Page 20: Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes

Thank You