The while loop in C++ is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The code inside the loop is executed as long as the condition is true.
The basic syntax of a while loop in C++ is as follows:
while (condition) {
// code block to be executed
}
This example demonstrates a simple counter using a while loop that prints numbers from 1 to 5.
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << endl;
i++;
}
return 0;
}
Console Output:
1 2 3 4 5
This example shows how a while loop can be used to repeatedly prompt the user for input until a valid number is entered.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a positive number: ";
cin >> number;
while (number <= 0) {
cout << "Invalid! Enter a positive number: ";
cin >> number;
}
cout << "You entered: " << number << endl;
return 0;
}
Console Output:
Enter a positive number: -3 Invalid! Enter a positive number: -1 Invalid! Enter a positive number: 5 You entered: 5
This example calculates the sum of numbers from 1 to 10 using a while loop.
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int i = 1;
while (i <= 10) {
sum += i;
i++;
}
cout << "Sum from 1 to 10 is: " << sum << endl;
return 0;
}
Console Output:
Sum from 1 to 10 is: 55
This example demonstrates using a while loop to calculate the factorial of a number.
#include <iostream>
using namespace std;
int main() {
int n = 5;
int factorial = 1;
while (n > 0) {
factorial *= n;
n--;
}
cout << "Factorial is: " << factorial << endl;
return 0;
}
Console Output:
Factorial is: 120
An infinite loop occurs when the loop's termination condition never becomes false, causing the loop to run indefinitely. This can be useful in certain cases like waiting for user input or server requests.
#include <iostream>
using namespace std;
int main() {
while (true) {
cout << "This will run forever." << endl;
}
return 0;
}
Note:
Use Ctrl+C to stop the execution in a terminal.
Nested loops are loops within loops. Each time the outer loop runs, the inner loop is re-executed from the beginning.
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 3) {
int j = 1;
while (j <= 2) {
cout << "i = " << i << ", j = " << j << endl;
j++;
}
i++;
}
return 0;
}
Console Output:
i = 1, j = 1 i = 1, j = 2 i = 2, j = 1 i = 2, j = 2 i = 3, j = 1 i = 3, j = 2
The break statement allows you to exit a loop when a certain condition is met, even if the loop's condition is still true.
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 10) {
if (i == 6) {
break;
}
cout << i << endl;
i++;
}
return 0;
}
Console Output:
1 2 3 4 5
The continue statement skips the current iteration of the loop and proceeds to the next iteration.
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue;
}
cout << i << endl;
}
return 0;
}
Console Output:
1 3 5 7 9
The do-while loop is similar to the while loop, but it checks the condition after executing the loop's body, ensuring that the loop is executed at least once.
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << endl;
i++;
} while (i <= 5);
return 0;
}
Console Output:
1 2 3 4 5
This example demonstrates using a while loop to iterate over an array and print its elements.
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int i = 0;
while (i < 5) {
cout << arr[i] << endl;
i++;
}
return 0;
}
Console Output:
10 20 30 40 50
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies