0% found this document useful (0 votes)
7 views

Topic5-Introduction To Loop and Using Loop For

Uploaded by

Da Vy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Topic5-Introduction To Loop and Using Loop For

Uploaded by

Da Vy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Data structure &

programming i
Topic 4- Introduction to loop and using loop for

1
Test if a given number is odd or even

2
Loop (iterator structure)
1. for
2. while
3. do … while

3
Introduction to loop
Types of problems and solutions
▪ Suppose that we want to display a message “Hi, what is your name” and ask for names of 100 students
▪ Solution 1: Using 100 repeated instructions

var name: Sequence of character

100 repeated instructions


begin
write(“Hi, what is your name?”)
read(name)
write(“Hi, what is your name?”)
read(name)
...
write(“Hi, what is your name?”)
read(name)
end

▪ Solution 2 (Better solution): Use iteration structure (loop)


▪ Loop allows a block of instruction/codes to be executed repeatedly within a condition. Let i=1 and
condition i<=100, we can repeat our code 100 times by each time update i=i+1. 4
FOR … DO
inst1
FOR loop
false
condition
▪ Syntax: instruction to initialize value of the control variable
loopback condition for stopping loop when it turns false true

instruction to modify the value of control variable Block instr.

for(inst1; condition; inst2) do


inst2
block of instructions
end for

▪ Example:
var i: integer
begin
for(i  25; i<=30; i  i+1) do
write(i, “ ”)
end for Output: 25 26 27 28 29 30
end 5
FOR … DO
Examples
var i: integer inst1
begin
for(i  1; i<=7; i  i+2) do false
Output: condition
write(i, “ ”)
end for true
end 1
? 3 5 7
Block instr.

var i: integer
begin Output: inst2
for(i  25; i>0; i  i-5) do
write(i, “ ”)
end for
25
? 20 15 10 5
end

6
FOR … DO
Examples
var i: integer inst1
begin
for(i  7 ; i>7; i  i+2) do false
Output: condition
write(i, “ ”)
end for true
end ?
Block instr.

var i: integer
begin Output: inst2
for(i  25; i>0; i  i+1) do
write(i, “ ”)
end for
25
? 26 27 …
end

7
FOR … DO
More examples
inst1

var i, j : integer false


begin condition
for(i  1; i <= 3; i  i+1) do
true
for(j  1; j <= 4; j  j+1) do
write(“A”) Block instr.
write(“B”)
end for
inst2
write(“ ”)
end for
end

Output: ABABABAB ABABABAB ABABABAB


8
FOR … DO
More examples
inst1
var i, j, k, n : integer
begin false
condition
n  4
for(i  1; i <= n; i  i+1) do true
for(j  1; j <= n-i; j  j+1) do
write(“ ”) Block instr.
end for
for(k  1; k <= 2*i-1; k  k+1) do inst2
write(“*”)
end for
write(“\n”) Output:
end for
end i=1 *
i=2 ***
Break 10mn
i=3 *****
Start: 8:25am i=4 ******* 9
Break Vs. Continue keyword

break statement breaks the loop/switch whereas

continue skip the execution of current iteration and continue to the


next iteration (it does not break the loop/switch)

10
BREAK and CONTINUE statements
BREAK Vs. CONTINUE
▪ Break: allows to break/terminate the running loop
▪ Continue: allows to skip 1 iteration, so any instructions followed by
continue will be skipped then the loop continue the next iteration
▪ Examples
var i : integer Output:
var i : integer
begin begin 124567
for(i  1; i<=9; i  i+1) do for(i  1; i<=10; i  i+1) do
if (i==4) then if (i==3) then
continue continue
end if end if
write(i) if (i==8) then
end for break
end end if
write(i)
end for
Output: 12356789 end 11
C program 3
#include <stdio.h>
int main(){
Loop: for int num;
printf("Enter a number: ");
scanf("%d", &num);
Output:
▪ Syntax for(int i=0; i<num; i++){
printf("%d ", i);
} 0 1 2 … (num-1)
}
for(initStatement; condition; updateStatement){
//codes #include <stdio.h> 4
} int main(){
int num;
printf("Enter a number: ");
scanf("%d", &num);
Syntax in C Output:
for(int i=num; i>0; i--){
printf("%d ", i);
} num (num-1) … 1
#include <stdio.h> 1 #include <stdio.h> 2 }
int main(){ int main(){
int n,i; int n; #include <stdio.h> 5
int main(){
int num; Output (infinite loop):
for(i=0; i<20; i++){ for(int i=0; i<20; i++){ printf("Enter a number: ");
printf("%d ", i); printf("%d ", i); scanf("%d", &num);
} } 0 -1 -2 …
} } for(int i=0; i<num; i--){
printf("%d ", i);
}
12
Examples of using for loop in C }
Example: A program to display and sum a suit of number

▪ Using for loop to display and sum all numbers from 1 to 99.

13
Example: A program to check if a given input number is a prime number

14
Assignment (part 1)
Loop exercises: Write a C program to …

1. Display all numbers from 99 to 1.


2. Display all numbers from 1 to 100 except the number 50.
3. Display odd numbers between 8 to 1000 except the numbers 11, 17 and 21.
4. Show all integer divisible by 3 between 1 to 100 except 30, 60, and 90.
5. Sum all numbers from 1 to 100 then display the result.
6. Multiply all numbers from 1 to 100 then display the result.

15
Assignment (part 1)
Loop exercises: Write a C program to …

7. Display the words “Hi” 20 times and then “bye” 10 time using For loop. One line for
displaying the word “Hi”, and another line for displaying the word “bye”.
8. Display all even numbers between 0 to 30.
9. Calculate factorial of integer number n, where n is a positive number entered by a user.
10. Write an algorithm to sum suite number from 1 to n, where n is a positive number
entered by a user.

16
Assignment (part 2)
Loop exercises: Write a C program to …

5. Compute and display the summation of the suit cube number starting from
1 up to n, where n is the input number entered by a user, n is greater than 1.
5. Ex: Suppose the input is 3, then display 1^3 + 2^3 + 3^3 = 36

6. Check whether an input number is a primary number or not. The program


runs indefinitely so that we can always check another input number.
7. Display all primary numbers in between 2 to 500.
8. Read 10 input numbers from a user and then find the maximum number
and display it on screen.
17
Assignment (part 2)
Loop exercises: Write a C program to …

9. Compute and display the summation of the suit cube number starting from
n up to 1, where n is the input number entered by a user, n is greater than 1.
9. Ex: Suppose the input is 3, then display 3^3 + 2^3 + 1^3 = 36

10. Check whether an input number is a primary number or not. The program
runs indefinitely so that we can always check another input number.
11. Display all primary numbers in between 2 to 500.
12. Read 20 input numbers from a user and then find the maximum number
and display it on screen.
18
Assignment (part 2)
Loop exercises: Write a C program to …

13. Check whether an input number is a perfect number or not. The program
runs indefinitely so that we can always check another input number.

Perfect number is a positive integer that is equal to the sum of its proper divisors,
excluding the number itself.
E.g: 6 is a perfect number. • 6 has divisors 1, 2 and 3
• Since
Since thethe sum
sum ofofitsitsdivisors
divisors1,
1, 2,
2, and
and 33 are
areequal
equaltoto6.6.

19
Assignment (part 2)
Loop exercises: Write a C program to …

14. Read 10 input numbers from a user and then find the minimum number and
display it on screen.
15. Display the first n numbers of suit Fibonacci, where n is a number entered
by a user.
The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it.

• The 2 is found by adding the two numbers before it (1+1)


• The 3 is found by adding the two numbers before it (1+2),
• And the 5 is (2+3),
• and so on!
20
Assignment (part 2)
Loop exercises: Write a C program to …

16. Ask a user to input many numbers as possible. When the user inputs 0 or -1,
stop asking the user for the number and display the total summation of all
input numbers on the screen.

21

You might also like