EEE 103 Computer Programming
Lecture 4
Introduction to Repetition : Loops
Instructor
Himel Kundu Utsha
Lecturer
Department of EEE
BRAC University
Loop Statements
❑ The ability to repeat a block of code a number of times.
❑ A loop is a way to execute a piece of code repeatedly until the condition is
met .
While Loop
❑ Syntax:
initialization;
while(conditional_test)
{
statement(s);
increment;
}
❑ Will keep on executing the statements
until the conditional_test is true.
Example of While Loop
#include<stdio.h>
int main()
{
int i=0;
while(i<=9)
{
printf("%d\n", i);
i++;
}
return 0;
}
Do While Loop
❑ Syntax:
initialization;
do
{
statement;
increment;
} while(conditional_test);
❑ Test is at the bottom
❑ Will execute at least once
❑ Common error : forgetting the semicolon (;)
after while
Example of Do While Loop
#include<stdio.h>
int main()
{
int i=0;
do
{
printf("%d\n", i);
i++;
}while(i<=9);
return 0;
}
For Loop
❑ for ( initialization; conditional-test; increment )
statement;
❑ initialization:
▪ Give an initial value to the variable that controls the loop (loop-control variable)
▪ Executed only once
▪ Before the loop begins
❑ conditional-test:
▪ Tests the loop-control variable against a target value
▪ If true the loop repeats
▪ statement is executed
▪ If false the loop stops
▪ Next line of code following the loop will be executed
❑ increment:
▪ Executed at the bottom of the loop
Example of For Loop
Single Statement
for(i=1; i<100; i++)
printf("%d\n", i);
❖ Prints 1 to 99
for(i=100; i<100; i++)
printf("%d\n", i);
❖ This loop will not execute
Example of For Loop
Block of Statements
sum=0;
prod=1;
for(i=1; i<5; i++)
{
sum+=i;
prod*=i;
}
printf("sum, prod is %d, %d\n", sum, prod);
For Loop : Step By Step
for(i=1; i<3; i++)
printf("%d\n", i);
1) i is initialized to 1 (Initialization part is executed only once)
2) Conditional test i<3 is true as i is 1, so the loop executes
3) The value of i will be printed, which is 1
4) Conditional test i<3 is true as i is 2, so the loop executes
5) The value of i will be printed, which is 2
6) The value of i will be incremented, so now i is 3.
7) Conditional test i<3 is false as i is 3, so the loop stops
For Loop
❑ for loop can run negatively
❑ decrement can be used instead of increment
for(i=20; i>0; i--)
❑ Can be incremented or decremented by more than one
for(i=1; i<100; i+=5)
For Loop
▪ All of the following loops will print 1 to 99
▪ for(i=1; i<100; i++)
printf("%d\n", i);
▪ for(i=1; i<=99; i++)
printf("%d\n", i);
▪ for(i=0; i<99; i++)
printf("%d\n", i+1);
▪ for(i=0; i<=98; i++)
printf("%d\n", i+1);
▪ So selection of initial value and loop control condition is important
For Loop : Example
GCD of Two Numbers
#include <stdio.h>
int main(void) {
int a, b, min, i, gcd;
scanf("%d %d", &a, &b);
min=(a<b)?a:b;
for(i=1; i<=min; i++)
if(a%i==0 && b%i==0)
gcd=i;
printf("gcd of %d & %d is %d\n", a, b, gcd);
return 0;
}
Nested For Loop : Example
#include<stdio.h> Output:
int main() 1,1
{ 2,1
for(int i=1; i<=3; i++) 2,2
{
3,1
for(int j=1; j<=i; j++)
3,2
{
3,3
printf("%d, %d\n", i, j);
}
}
What if the condition is j<=3?
return 0;
}
Nested For Loop : Example
Write a C code that determines the sum of first n terms of the following series:
1 + 22 + 33 + 44 + ⋯ + 𝑛 𝑛
#include<stdio.h> for(int j=0; j<i; j++)
{
prod=prod*i;
int main()
}
{ sum=sum+prod;
int n, sum, prod; }
prod=1; printf("sum is %d\n", sum);
sum=0; return 0;
scanf("%d", &n); }
for(int i=1; i<=n; i++)
{
prod=1;
Prime Number Tester (While Loop)
/*prime number tester*/ while(i<=num/2)
#include<stdio.h> {
int main() if(!(num%i)) is_prime=0;
{ i++;
int i, num, is_prime=1; }
printf("Enter the number to if(is_prime)
test: ");
printf("%d is prime\n", num);
scanf("%d", &num);
else
/*test for factors*/
printf("%d is not prime\n", num);
i=2;
return 0;
}
Prime Number Tester (Do While Loop)
/*prime number tester*/ if(is_prime)
#include<stdio.h> printf("%d is prime\n", num);
int main() else
{ printf("%d is not prime\n", num);
int i, num, is_prime=1; return 0;
printf("Enter the number to test: "); }
scanf("%d", &num);
/*test for factors*/
▪ It will show that 2 is
i=2;
not prime!!
do
{
if(!(num%i)) is_prime=0;
i++;
}while(i<=num/2);
Prime Number Tester (For Loop)
#include<stdio.h>
int main()
{
int i, num, is_prime=1;
printf("Enter the number to test: ");
scanf("%d", &num);
for(i=2; i<=num/2; i++)
if((num%i)==0) is_prime=0;
if(is_prime==1)
printf("%d is prime\n", num);
else
printf("%d is not prime\n", num);
return 0;
}
Use of Break
/*prime number tester*/
#include<stdio.h> if(is_prime)
int main()
else
{ printf("%d is not prime\n",
int i, num, is_prime=1; num);
return 0;
printf("Enter the number to test: "); }
scanf("%d", &num);
/*test for factors*/
for(i=2; i<=num/2; i++)
if(!(num%i))
{
is_prime=0;
break;
}
Use of Continue
#include<stdio.h> Output:
int main()
{ 2,1
3,1
for(int i=1; i<=3; i++)
3,2
{
for(int j=1; j<=i; j++)
{
if(i==j) continue;
printf("%d, %d\n", i, j);
}
}
return 0;
}
Input Characters
▪ getche()/getch()/getchar can be used
▪ getchar()
▪ Compiler dependent
▪ waits for carriage return
▪ Read only one char
▪ Other input and carriage return will be in buffer
▪ Subsequent input (e.g, scanf) will consume them.
▪ Defined in stdio.h
▪ getche()/getch()
▪ Return immediately after a key is pressed
▪ Defined in conio.h
Practice on Loops
▪ Given a number as input write to program to calculate the number of digits.
▪ Given a number as input write to program to calculate the sum of it’s digits.
▪ Write a program to find xm where x and m are inputs
▪ Write a program to convert a decimal number to a binary number
Practice on Loops
• Given the length and breadth of a rectangle, write a program to find whether the area of the
rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and
breadth = 4 is greater than its perimeter.
• Given the coordinates (x, y) of a center of a circle and it’s radius, write a program which will
determine whether a point (input through the keyboard) lies inside the circle, on the circle or outside
the circle. [Hint: Use sqrt( ) and pow( ) functions from <math.h>]
• Write a program to determine whether the character entered through the keyboard is a lower
case alphabet or an upper case alphabet or neither.
• Write a program using do-while loop to test whether a number is prime or not.
• Write a program using while loop to find xm where x and m are inputs. [You can't use pow() function]