Lec 12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Computer Programming

(CSC209)
Lecture (12)
Loops (While and for)

Dr. Omar Almutairi


Outline
1. Understanding of Loops
2. Flow Control and Loops in MATLAB
3. The while loop
Example
4. The for loop
Example
Understanding of Loops
Loops: Construct permits user to
execute a block of instructions more than
once.
Flow Control and Loops in MATLAB
Flow Control:
 if-elseif-else statement
 switch-case-otherwise statement
Loops:
 The for Loop
 The while Loop
THE while LOOP

Structure:
The Code Block is repeated
until the expression is false.
If it is false, the program
executes the first statement
after the end.

5
THE while LOOP
Example 1:
x = input('Enter first value: ');
while x >= 0
disp('x')
end

The value of x will be continuously displayed even with one input

6
THE while LOOP
Example 2:
x = input('Enter first value: ');
while x >= 0
disp('x')
x = input('Enter second value: ');
end
k=2*x

The value of x depend on while condition


7
THE while LOOP
Example 3:
Write a program that calculates the average,

and the standard deviation,

for a set of values (X), entered by the user


8
Example 3 solution
% Initialize sums.
n = 0; sum_x = 0; sum_x2 = 0;
% Read in first value
x = input('Enter first value: ');
while x >= 0
% Accumulate sums.
n = n + 1
sum_x = sum_x + x
sum_x2 = sum_x2 + x^2
% Read in next value
x = input('Enter next value: ');
end
% Calculate the mean and standard deviation
x_bar = sum_x/n
std_dev=sqrt((n*sum_x2-sum_x^2)/(n*(n-1)))
The For Loop
THE for LOOP
Structure:
Repeats a block of statements (Body) specified
number of times.
Matlab generates an array by
evaluating this expression
column by column.

Legal Examples:

11
Example (1): THE for LOOP
>> Untdditled4
for x=0:10 0
n=i;
1.00
disp(x)
end 2.00

3.00

4.00

5.00

6.00

7.00

8.00

9.00

10.00
Example (2): THE for LOOP
>> Untdditled4
for x=0:2:10 0
n=i;
2.00
disp(x)
end 4.00

6.00

8.00

10.00
Example (3): THE for LOOP
Write a MATLAB program to define the sum of the
first 25 number
sum=0;
for k=1:25
sum=sum+k;
end
disp('The total number is:')
disp(sum)

The total number is:


325.00
Example (4): THE for LOOP
Write a MATLAB program to define the sum of the
given number
sum=0
for k=[1 3 6 7]
sum=sum+k;
end
disp('The total number is:')
disp(sum)

The total number is:


17
Example (5): THE for LOOP
a=input('in put a= '); % a=input('in put a=
');
if (a >= 0) for b=1:6
y=a+1; a=input('in put a= ');
else
y=a^2+2; if (a >= 0)
end y=a+1;
else
y=a^2+2;
end
disp(y)
end
Example (6): THE for LOOP
a=input('in put a= '); % a=input('in put a=
');
for b=1:6 for b=1:6
% a=input('in put a= a=input('in put a= ');
');
if (a >= 0) if (a >= 0)
y=a+1; y=a+1;
else else
y=a^2+2; y=a^2+2;
end end
disp(y) disp(y)
end end

What happened if you run the program in each case?


Example (7): THE for LOOP
for b=1:6 for b=1:6
a=input('in put a= '); a=input('in put a= ');

if (a >= 0) if (a >= 0)
y=a+1; y=a+1;
else else
y=a^2+2; y=a^2+2;
end end
disp(y) End
end disp(y)

What happened if you run the program in each case?


Practice

19
END

20

You might also like