2. Overview
• Review
• Program control
• if-else statement
– Relational and logical operators
– Logical expressions
• for loop statement
3. How to write a program?
1. Read the problem statement, and identify
– The input and its range
– The output
– The relationship between the input and the output
(how to compute the output) [Comprehend]
2. Write your thoughts as a sequence of steps.
[Algorithm]
3. Convert these steps to Code. [Program]
4. Test your code and compare your program result
against a human result. [Testing]
4. Calculate area of a rectangle
(Algorithm Program)
1. Get height h
h=input(‘enter height: ’);
2. Get width w
w=input(‘enter width: ’);
3. Calculate area = h * w
area = h * w;
4. Display area
disp (area);
5. Program Control
• How to control the flow of the program?
– To executes some statements and not the other
based on the input data.
– To make the program takes decision
• How to make some statements executed
several times.
6. Print the maximum of two numbers
(Algorithm and Program)
1. Get number1 as X X=input(‘enter num1:’);
2. Get number2 as Y Y=input(‘enter num2:’);
3. If X > Y if X>Y
display X disp(X);
Otherwise else
display Y disp(Y);
end
7. Print the maximum of two numbers
(Program and Testing)
% printmax.m
X=input(‘enter num1:’);
Y=input(‘enter num2:’);
if X>Y
disp(X);
else
disp(Y);
end
>>printmax
enter num1: 5
enter num2: 10
10
>>printmax
enter num1: 8
enter num2: 3
8
5
10
X
Y
8
3
8. Print two number in ascending order
(Algorithm Program)
1. Get number1 as X X=input(‘enter num1:’);
2. Get number2 as Y Y=input(‘enter num2:’);
3. If X > Y if X>Y
display Y, X disp(Y); disp(X);
Otherwise else
display X, Y disp(X); disp(Y);
end
9. Print two number in ascending order
(Program and Testing)
% order2.m
X=input(‘enter num1:’);
Y=input(‘enter num2:’);
if X>Y
disp(Y);
disp(X);
else
disp(X);
disp(Y);
end
>>order2
enter num1: 5
enter num2: 10
5
10
>>order2
enter num1: 8
enter num2: 3
3
8
5
10
X
Y
8
3
10. If-else statement
if condition
% do this part if condition is true
else
%do this part if the condition is false
end
Condition (use relational operators):
>, <, >=, <=, ==, ~=
11. Relational Operators
• Relational operators test the Relationship between
two operands (which can be expressions)
• They are a “Truth Statement”, and resolve to either
True (1) or False (0)
Operator Operation
== Equal to
~= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
12. Logical Operators
• Relational operators are binary operators so they can have only two
operands
• To have a more complicated condition, we need to use logical
operators
• Logic Operators can combine “truth statements” together in helpful
ways. (and, or, not)
Inputs and or not
A B A && B A || B ~A
false false false false true
false true false true true
true false false true false
true true true true false
13. Logical Expressions Examples
Write the if condition for the following cases:
1. Check if x > y > z
if x>y && y>z
2. Check if x is greater than y or less than or equal to z
if x>y || x <=z
3. Check if x does not equal y but equal z
if x~=y && x==z
4. Check if x is even
if rem(x,2)==0
15. Try it in MATLAB
for i=1:1:5
X=input(‘enter num1:’);
Y=input(‘enter num2:’);
if X>Y
disp(X);
else
disp(Y);
end
end
X
Y
i
How many times these loops will run:
for i=1:2:10 for z=10:2:20
for a=10:-2:1 for x=1:10
?
16. Calculate sum from 1 to N (Program)
N=input(‘enter a number:’);
sum=0;
for i=1:N
sum=sum+i;
end
disp(sum);
How to sum only even numbers?
N i sum
5 0
1 1
2 3
3 6
4 10
5 15
17. Calculate factorial of a number N
(Program)
N=input(‘enter a number:’);
f=1;
for i=1:N
f=f*i;
end
disp(f);
N i f
5 1
1 1
2 2
3 6
4 24
5 120
18. What this program does?
N=input(‘enter a number:’);
sum=0;
for i=1:N
x=input(‘enter a value:’);
sum=sum+x;
end
disp(sum);
N i x sum
5 0
1 3 3
2 5 8
3 0 8
4 -2 6
5 1 7
How to compute the average of N numbers?
19. Sum only positive numbers?
N=input(‘enter a number:’);
sum=0;
for i=1:N
x=input(‘enter a value:’);
if x>0
sum=sum+x;
end
end
disp(sum);
N i x sum
5 0
1 3 3
2 5 8
3 -3 8
4 -2 8
5 1 9
20. Sum and Average positive numbers?
N=input(‘enter a number:’);
sum=0;
c=0;
for i=1:N
x=input(‘enter a value:’);
if x>0
sum=sum+x;
c=c+1;
end
End
Avg=sum/c;
disp(sum);
Disp(Avg);
N i x sum c
5 0 0
1 3 3 1
2 5 8 2
3 -3 8 2
4 -2 8 2
5 1 9 3
C is used to count positive numbers