Topic5-Introduction To Loop and Using Loop For
Topic5-Introduction To Loop and Using Loop For
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
▪ 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
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 …
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
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.
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