You are on page 1of 23

Computer Programming

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

Dr.Anwar Hassan Ibrahim


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
Example (3): THE While LOOP

x = 0;
y = 10;
while x < y
x=x+1
disp(x)
Pause(2)
end

8
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:

10
Example (1): THE for LOOP
>> Untdditled4
for x=0:10 0
disp(x)
1.00
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.00
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(b)=input('in put a= a(b)=input('in put a=
'); ');

if (a(b) >= 0) if (a(b) >= 0)


y(b)=a(b)+1; y(b)=a(b)+1;
else else
y(b)=a(b)^2+2; y(b)=a(b)^2+2;
end end
disp(y(b)) End
end disp(y(b))

What happened if you run the program in each case?


Example (7): THE for LOOP
x=0;
y=0;
for b=0:1:10
clc
x=x+b
Y=y+1
pause(2)
end

18
Practice

19
Practice: THE for LOOP
1. Write a Matlab program that finds the some of first 25
natural number.
2. Write a Matlab program that finds the some of first 25
natural number
3. (Only events).
4. Write a Matlab program that finds the some of first 25
natural number
5. (Only odds).

20
THE while LOOP
Practice:
Write a program that calculates the average,

and the standard deviation,

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


21
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)))
END

23

You might also like