While_loop
While_loop
Fundamentals
Outline
● Control Structures (Repetition/Loops)
● Types of Repetition Structures
○ Pre-test (while, for)
○ Post-test (do-while)
● While Loop
○ Counter controlled While loop
○ Sentinel controlled While loop
○ Flag controlled While loop
○ EOF (end of file) controlled While loop
● For Loop
● Do While Loop
● Break & Continue Statements
2
3
Why Is Repetition Needed?
● Repetition allows you to efficiently use variables
● Can input, add, and take average of multiple numbers using a limited number
of variables
● For example, to add five numbers:
○ Declare a variable for each number, input the numbers and add the variables together
○ Create a loop that reads a number into a variable and adds it to a variable that contains
the sum of the numbers
4
while Looping (Repetition) Structure
● The general form of the while statement is:
true
statement(s)
6
Understanding while
1
3
7
while Looping (Repetition) Structure (cont'd.)
Initialize
before
using
Loop Control
Variable
Omit
this
25 is not printed
Why?
8
Examples
i = 0; i = 0;
while (i <= 20) while (i <= 20)
{ {
cout << i << " " ; i=i+5;
i=i+5;
cout << i << " " ;
}
}
cout << endl ;
cout << endl ;
● Write a C++ program to print the sum of even numbers between 5-1000?
● Write a C++ program to print the those even numbers which are divisible by
4 between 5-1000?
● Write a C++ program to print the factorial of any number input by user?
15
Case 2: Sentinel-Controlled while Loops
● The number of iterations is not known before the loop starts executing
● Sentinel is a special value of variable which changes the loop condition true to false
● Loop ends when sentinel value is encountered
○ Reading from a file till end-of-file eof()
○ Get input from user until a –ve number is input
When to Use: Sentinel controlled loop is useful when we don’t know in advance how many times
the loop will be executed.
std::cin >> variable ; //initialize the loop variable
while(variable != sentinel) //test the loop control variable
{
.
.
std::cin >> variable ; //update the loop control variable
.
.
}
16
Sentinel Example
int total = 0;
int points;
std::cout << "Enter points earned (or -1 to quit): ";
std::cin >> points;
while (points != -1) // -1 is the sentinel
{
total += points;
std::cout << "Enter points earned (or -1 to quit): ";
std::cin >> points;
}
std::cout << "\nTotal = " << total << std::endl;
17
Comparison of Sentinel and Counter Loops
S.NO
BASIS OF COMPARISON SENTINEL CONTROLLED LOOP COUNTER CONTROLLED LOOP
.
A sentinel controlled loop is the infinite repetition loop as A counter controlled loop is the finite repetition loop
1. Definition the number of repetitions is not known before the loop as the number of repetitions is known before the loop
begins executing begins executing
2. Controlling variable Controlled variable used is know as sentinel variable. Controlled variable used is know as counter.
4. Value of variable The value of the variable is not strict i.e. any value The value of the variable is strict i.e. num < N
5. Limitation of variable The Limitation of the variable is strict. The Limitation of the variable is strict also.
For example:
while ((noOfGuesses < 5) && (!isGuessed))
{
…
}
21
Example
1. Get the first two Fibonacci numbers.
2. Get the desired Fibonacci number.
That is, get the position, n, of the
Fibonacci number in the sequence.
3. Calculate the next Fibonacci number
by adding the previous two elements
of the Fibonacci sequence.
4. Repeat Step 3 until the nth Fibonacci
number is found.
5. Output the nth Fibonacci number.
#include <iostream> else
using namespace std; {
22
int main() counter = 3;
{
while (counter <= nthFibonacci)
//Declare variables
int previous1, previous2, current; {
int counter, nthFibonacci; current = previous2 + previous1;
cout << "Enter the first two Fibonacci" previous1 = previous2;
<< "numbers: "; previous2 = current;
cin >> previous1 >> previous2; counter++;
cout << endl; }//end while
cout << "The first two Fibonacci "
}//end else
<< "numbers are "
<< previous1 << " and " cout << "The Fibonacci number at "
<< previous2 << endl; << "position " << nthFibonacci
cout << "Enter the position of the “ << " is " << current << endl;
<< "desired Fibonacci number: "; return 0;
cin >> nthFibonacci; } //end main
cout << endl;
if (nthFibonacci == 1)
current = previous1;
else if (nthFibonacci == 2)
current = previous2;
23
Fibonacci Number Sample Runs
Sample Run 1:
Enter the first two Fibonacci numbers: 12 16
The first two Fibonacci numbers are 12 and 16
Enter the position of the desired Fibonacci number: 10
The Fibonacci number at position 10 is 796
Sample Run 2:
Enter the first two Fibonacci numbers: 1 1
The first two Fibonacci numbers are 1 and 1
Enter the position of the desired Fibonacci number: 15
The Fibonacci number at position 15 is 610
24
Increment and Decrement Operators
● Increment operator(++): increment variable by 1
○ Pre-increment: ++variable
○ Post-increment: variable++
○ int j = i++; // j will contain i, i will be incremented.
○ int j = ++i; // i will be incremented, and j will contain i+1.
End!