System Design S.CS301 (Autumn 2015/16) Page 1
Өмнөх хичээлээр юу үзсэн бэ? o m файлууд o Скрипт файл, түүнийг үүсгэх o Функц файл o Оролт гаралтын аргументүүд o Скрипт ба функц м файлуудын ялгаа o ҮС-н командууд o Каталогтой ажиллах команд o Файлтай ажиллах команд (Autumn 2015/16) Page 2
Today we will learn Relational and logical operators Switch statement Nested switch Statements While loop For loop Control of loop (Autumn 2015/16) Page 3
Relational and logical operators A relational operator compares two numbers by determining whether a comparison is true or false. (Autumn 2015/16) Page 4
switch command A switch block conditionally executes one set of statements from several choices. Each choice is covered by a case statement. (Autumn 2015/16) Page 5
Example - 1 (Autumn 2015/16) Page 6
Introduction The switch block tests each case until one of the case expressions is true. A case is true when (Autumn 2015/16) Page 7
Example 2 (Autumn 2015/16) Page 8
Example 3 (Autumn 2015/16) Page 9
The Nested switch Statements It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise. (Autumn 2015/16) Page 10
Example 4 (Autumn 2015/16) Page 11
Loop Types A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages (Autumn 2015/16) Page 12
While loop The while loop repeatedly executes statements while condition is true. The while loop repeatedly executes program statement(s) as long as the expression remains true. An expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false (Autumn 2015/16) Page 13
Example 5 (Autumn 2015/16) Page 14
The for Loop A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. (Autumn 2015/16) Page 15
Values of for loop (Autumn 2015/16) Page 16
Example 6 (Autumn 2015/16) Page 17
Loop Control Statements Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. The break statement terminates execution of for or while loop. Statements in the loop that appear after the break statement are not executed. In nested loops, break exits only from the loop in which it occurs. Control passes to the statement following the end of that loop. (Autumn 2015/16) Page 18
Flow diagram (Autumn 2015/16) Page 19
Continue Statement The continue statement is used for passing control to next iteration of for or while loop. The continue statement in MATLAB works somewhat like the break statement. Instead of forcing termination, however, 'continue' forces the next iteration of the loop to take place, skipping any code in between. (Autumn 2015/16) Page 20
Conditional Expressions 1 x=10; y=20; 1. disp(x < y); 2. disp(x <= 10); 3. disp(x == y); 4. disp((0 < x) & (y < 30)); 5. disp((x > 10) (y > 100)); 6. disp(~(x > 10)); (Autumn 2015/16) Page 21
Conditional Expressions 2 area=[ 1 4 9 16 25 36 ]; perimeter=[ 4 8 12 16 20 24 ]; disp(area < perimeter);? 1 1 1 0 0 0 disp(area(area < perimeter));? 1 4 9 (Autumn 2015/16) Page 22
Exercise Дурын А тоо өгөгджээ. Өгөгдсөн тоо эерэг бол тоог 10-аар хорогдуулж, сөрөг бол 10-р нэмэгдүүлж гарга. Дээрх нөхцөлүүдийг биелүүлэхгүй бол өгөгдсөн тоон дээр 81-г нэмээд гарсан тооноос квадрат язгуур гарга. (Autumn 2015/16) Page 23
What we learned today? Logical operation for conditional commands Multi selection command Repeat several commands by while and for statements Usage of the break and continue keywords To program linear equations using by built-in functions Solve linear equations by script files (Autumn 2015/16) Page 24
End of Any questions? (Autumn 2015/16) Page 25