A while loop repeatedly runs a block of code as long as the condition is true. Once the condition becomes false, the loop stops. This type of loop is useful when the number of iterations is not known beforehand.
int password; std::cout << "Enter your password: "; std::cin >> password; // The loop keeps asking for the password until the user enters 1234 while (password != 1234) { std::cout << "Incorrect. Try again: "; std::cin >> password; } std::cout << "Access granted.";
A for loop is used when the number of times to execute the code block is known. It is useful for iterating over a range of values or running a set of instructions a specific number of times.
// Loop from 0 to 9 and print each number for (int i = 0; i < 10; i++) { std::cout << i << "\n"; } std::cout << "Loop complete.";
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!