Module 3.4 - For Loop Structure
Module 3.4 - For Loop Structure
for statement
-This loop executes a block of code if the specified condition is true.
-It is a pre-checked loop because it checks the controlling condition before the body of the loop is
executed.
- There is a possibility of not being executed at all.
- Initialization is only read once.
- Unlike do-while and while, in for loop, the initialization, condition and update are found in a
single code line/command. They are enclosed in parenthesis ( ) and separated by semicolons (;).
Components of a loop:
• Initialization
• Condition
• Body
• Update
Sample Problems
1. Write a C program that would print the phrase “Hello World!” 5 times. (There can be more than 1
way to code this program, I will be showing you two solutions.)
Test run:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
3. Write a C program that would print all the even numbers between 1 to 100, inclusive.
Test run:
#include<stdio.h> Notes:
int main()
{ Inclusive means start with 1 and end
int ctr; with 100.The starting and
printf(“Even number from 1-100: “); ending values in the range is
for(ctr=1; ctr<=100; ctr++)
included.
{
if(ctr%2 == 0)
{
printf("%d ", ctr);
}
}
}
4. Write a C program that would print all the odd numbers between 1 to 100, exclusive.
Test run:
Odd numbers from 22-999: 3 5 7 9 11 13 15 17 19 21 23 25 27
29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63
65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
#include<stdio.h> Notes:
int main()
{ Exclusive means start with 2 and
int ctr; end with 99. The starting and
printf(“Odd numbers from 1-100: “); ending values in the range is not
for (ctr=2; ctr<100; ctr++)
{ included.
if(ctr%2 == 1)
{
printf("%d ", ctr);
}
}
}
}
5. Write a C program that would ask the user for the starting and ending value of a range and print
all the even numbers within it, inclusive. The first number should be lesser than the second
number, and both numbers should be positive, otherwise display “Invalid Range”
Test run:
Start: 2 Start: 0
End: 10 End: 20
Even numbers within 2-10: 2 4 6 8 10 Even numbers within 0-20: 0 2 4 6 8
10 12 14 16 18 20
Start: 200
End: 150 Start: -5
Invalid Range End: 10
Invalid Range
#include<stdio.h>
int main()
{
int min, max, ctr;
printf("Start: ");
scanf("%d", &min);
printf("End: ");
scanf("%d", &max);
//check if min is less than max and are both positive
if((min<max) && (min >=0) && (max>=0))
{
//outside the loop so will be repeated only once
printf("Even numbers within %d-%d: ", min, max);
for (ctr=min; ctr<=max; ctr++)
{
if(ctr%2 == 0)
{
printf("%d ", ctr);
}
}
}
else
{
printf("Invalid Range");
}
}
Indefinite Loop:
⚫ You do not know ahead of time how many times your loop will execute.
⚫ For example, you do not know how many books a person might order.
Definite Loop:
⚫ You know exactly how many times you want the loop to execute.
⚫ not at compile time necessarily
for loops – usually used when you know exactly how many times you need
to repeat the loop.
while loops - usually used when we do not know exactly how many times
we need to execute repeating block of code.
do-while loop - usually used when we do not know exactly how many
times we need to execute repeating block of code, but we know that we
need to execute it at least once.
Special Functions:
Break
- exits from the lowest-level loop in which it occurs.
- used for early exit from the loop, or for exiting a forever loop
Create a C program to continuously ask for an integer. Stop the loop only if the
user inputs 0.
#include<stdio.h>
int main()
{
int num;
while(1) //this is an infinite loop
{
printf("Input an integer: ");
scanf("%d",&num);
if(num==0)
{
break; //this will exit from the current loop
}
//no need for update since this is an infinite loop
}
}