can someone give me detailed explanation on the while loop?

Don't explain "For Loop" and "Do While" loop. Just need an explanation on the simple "While" loop and how to use counter
If you know the "Do While" loop, a "While" loop is the same thing, except instead of checking the condition after the loop has executed, it checks it before.

This means that if your condition was false at the start, in a "Do While", it would run once, and in a "While" it would not run at all.
Last edited on
The while loop is especially useful for validating input. If an invalid value is entered, a loop can require that the user reenter it as many times as necessary. For example, the following loop asks for a number in the range of 1 through 100:

1
2
3
4
5
6
7
cout << "Enter a number in the range 1-100: ";
cin >> number;
while (number < 1 || number > 100)
{
cout << "ERROR: Enter a value in the range 1-100: ";
cin >> number;
}


This code first allows the user to enter a number. This takes place just before the loop. If the input is valid, the loop will not execute. If the input is invalid, however, the loop will display an error message and require the user to enter another number. The loop will continue to execute until the user enters a valid number.

Sometimes it’s important for a program to control or keep track of the number of iterations a loop performs. For example, the program below displays a table consisting of the numbers 1 through 10 and their squares, so its loop must iterate 10 times.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// This program displays a list of numbers and
// their squares.
#include <iostream>
using namespace std;

int main()
{
   const int MIN_NUMBER = 1,   // Starting number to square
             MAX_NUMBER = 10;  // Maximum number to square

   int num = MIN_NUMBER;       // Counter

   cout << "Number Number Squared\n";
   cout << "-------------------------\n";
   while (num <= MAX_NUMBER)
   {
      cout << num << "\t\t" << (num * num) << endl;
      num++; //Increment the counter.
   }
   return 0;
}


The variable num , which starts at 1, is incremented each time through the loop. When num reaches 11 the loop stops. num is used as a counter variable, which means it is regularly incremented in each iteration of the loop. In essence, num keeps count of the number of iterations the loop has performed.

It’s important that num be properly initialized. Remember, variables defined
inside a function have no guaranteed starting value.
i made this program. in this program we will input 10 numeric values and the in the end the program will give us the highest number.

#include <iostream>
using namespace std;
int main ()

{
int counter=0,num1,num2;

cout<<"Type a number : ";
cin>>num1;

while(counter<=10)
cout<<"Type a number : ";
cin>>num2;

if(num1>num2)
cout<<num1<<"is greatest";
else
cout<<num2<<"is the greatest";

return 0;
}

the program does not run properly. whats the mistake?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
 using namespace std;
 int main ()
 {
 int counter=0,num1,num2;

 cout<<"Type a number : ";
 cin>>num1;

 while(counter<=10)
 cout<<"Type a number : ";
 cin>>num2;

 if(num1>num2)
 cout<<num1<<"is greatest";
 else
 cout<<num2<<"is the greatest";

 return 0;
 }


Please put your code in code tags to make it more readable and give us line numbers to work with. See above.

Right now you are printing "Type a number : " infinitely.
Your loop body is not contained in brackets {}, so it just loops line 11 repeatedly forever.

Secondly, you have no increment on your counter, so even if you did have the brackets, you would not ever reach the ending condition (counter <= 10).

Third, you never store the greater number. So, if num2 was greater than num1, you would still keep num1, which may result in not having the highest number.


Last edited on
@tallyman can you please rearrange the code
The code doesn't have to be rearranged. It is in the right order. You've just left out a few very important things that I've described above.

Here is a example of a good while loop.
1
2
3
4
5
6
x = 0
while (x < 5) 
{ // open brace, so the loop knows there are multiple lines
cout << "this" << endl;
++x; // increment the counter
} // closing brace, so the loop knows to return to the while statement 
i fixed the first two errors but not the last one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main ()

{
	int counter=0,num1,num2;
	
	cout<<"Type a number : ";
	cin>>num1;
	
	while(counter<=10)
	{
		counter++;
		cout<<"Type a number : ";
		cin>>num2;
	}

	if(num1>num2) 
		cout<<num1<<"is the greatest";
	else
		cout<<num2<<"is the greatest";

	return 0;
}


i've been trying for the past 45 minutes to fix the third problem but i was unable to fix that. and the program asks Type a number 11 times. not 10 times
Last edited on
This is a classic off-by-one error. You're starting at 0, and going until your counter = 11. When it reaches 10, you continue looping (counter <= 10).

Right now, since you're reassigning num2 every loop, you will not be getting the greatest of all entered numbers. You will just get the greater of the first and last.
please rewrite the program
please rewrite the program

It is your homework, you need to rewrite the program not the people that are trying to help. tallyman gave you more than enough information for you to re-write the program. Do you honestly think that people seat behind the keyboard writing other peoples code, for free? Post the new code and we can help. It may take you 5 minutes or 5 hours. That is the beauty of it. Happy coding.
sorry for the late reply, i did figure out how to solve it :D
Topic archived. No new replies allowed.