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

Module 3.3 - While Loop Structure

Uploaded by

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

Module 3.3 - While Loop Structure

Uploaded by

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

Cebu Institute of Technology – University

Computer Engineering Department


Module 3.3 –While Loop Structure

while 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.
- Note: There is NO semicolon (;) after the while statement unlike in do-while.

Components of a loop:
• Initialization
• Condition
• Body
• Update

Syntax and Flowchart:

The body (block of codes you


want to repeat) would be
repeated if the condition is true.
Otherwise, the loop would be
exited.

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!

#include<stdio.h> Initialization: start your counter variable’s


int main() value to 1. (ctr=1)
{ Condition: your loop will continue if ctr is still
int ctr = 1; //ctr is your counter variable less than or equal to 5.
while(ctr<=5) //condition Your loop will repeat five times.
{ Body: the statement to be repeated.
printf("Hello World!\n"); Updating: you can use prefix or postfix
ctr++; //update
}
}

#include<stdio.h> Initialization: start your counter variable’s


int main() value to 0. (ctr=0)
{ Condition: your loop will continue if ctr is still
int ctr = 0; //ctr is your counter variable less than 5. Your condition can change
while(ctr<5) //condition depending on the initial value of your
{ counter.
printf("Hello World!\n"); Your loop will still repeat five times.
ctr++; //update Body: the statement to be repeated.
} Updating: you can use prefix or postfix
}
Engr. Jundith D. Alterado
[email protected]
Cebu Institute of Technology – University
Computer Engineering Department
Module 3.3 –While Loop Structure
2. Write a C program that would print the pattern below:
Test run: #include<stdio.h>
int main()
***** {
***** int ctr = 1; //initialization
***** while(ctr<=5) //controlling condition
***** {
***** printf("*****\n"); //”\n” is to go to the next line
ctr++; //update
}
}

3. Write a C program that would print all the even numbers between 1 to 100, inclusive.
Test run:

Even numbers from 1–100: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36


38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86
88 90 92 94 96 98 100

#include<stdio.h> Notes:
int main()
{ Inclusive means start with 1 and end
int ctr = 1; with 100.The starting and
printf(“Even number from 1-100: “); ending values in the range is
while (ctr<=100)
included.
{
if(ctr%2 == 0)
{
printf("%d ", ctr);
}
ctr++; //or ++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 = 2; end with 99. The starting and
printf(“Odd numbers from 1-100: “); ending values in the range is not
while(ctr<100)
{ included.
if(ctr%2 == 1)
{
printf("%d ", ctr);
}
ctr++; //or ++ctr;
}
}
}

Engr. Jundith D. Alterado


[email protected]
Cebu Institute of Technology – University
Computer Engineering Department
Module 3.3 –While Loop Structure

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);
//initialize counter
ctr = min;
//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);
while(ctr<=max)
{
if(ctr%2 == 0)
{
printf("%d ", ctr);
}
ctr++; //update
}
}
else
{
printf("Invalid Range");
}
}

Engr. Jundith D. Alterado


[email protected]
Cebu Institute of Technology – University
Computer Engineering Department
Module 3.3 –While Loop Structure

do-while VS while

do-while statement (post-checked) while statement (pre-checked)

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int ctr = 10; int ctr = 10;
do while(ctr>10)
{ {
printf("I run the statement first printf("I will check the condition
before checking."); first before running the statements.");
ctr++; ctr++;
}while(ctr>10); //false since ctr is }
not less than 10. So the loop is exited. }
}
Notes:
Notes: In the program above even though ctr’s initial value is 10 and the controlling
In the program above even though ctr’s initial value is 10 and the condition of the loop is that ctr must be less than 10, the compiler would check
controlling condition of the loop is that ctr must be less than 10, the the condition first before executing the statements inside the while. Therefore,
statements inside the do is executed first before the condition is checked. since the result is false (ctr is NOT less than 10) the program above wouldn’t
When the condition is checked, since it is false, the compiler would exit have any output /phrase displayed.
from your loop statements thus ending the iterations.

I run the statement first before Output


checking.

You can use either of the two unless specifically stated. But if not, you have the freedom to choose
depending on what you think can be best used for the problem. Just be careful in noting the difference
between the two.

Engr. Jundith D. Alterado


[email protected]

You might also like