DEV Community

Mayank Roy
Mayank Roy

Posted on

Day 7: Unlocking the Power of Loops in C++

Day 7 of my C++ journey introduced me to loops, the tools that bring repetition and efficiency to programming. I explored for, while, and do-while loops, understanding how they help execute code multiple times based on conditions.

1.While Loop

Challenge: Write a program that keeps track of tea orders. Each time a cup of tea is made, decrease the number of cups remaining. The loop should run until all cups are served.

#include <iostream>

using namespace std;

int main(){

    int teacups;

    cout << "Enter the number of teacups to serve: " << endl;
    cin >> teacups;

    //while loop
    while ( teacups > 0 ){
        teacups--;
        cout << "Serving a cup of tea \n" << teacups << " remaining" << endl ;

    }

    cout << "All tea cups are served." << endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Going through the code:

-whileloop is used to execute a block of code as long as a condition is true.

  • The condition is checked before eachiterationof the loop.
  • The loop will continue to execute as long as the condition is true.
  • The loop will execute at least once, even if the condition is initially false.
while (condition){
      // Code to be executed
}
Enter fullscreen mode Exit fullscreen mode

Here while checks if the teacups is greater than 0, if it is true, it will execute the code inside the loop. If the teacups is 0, the loop will not execute and the program will continue to the next line.

2.Do-While Loop

Challenge: Create a program that asks the user if they want more tea. Keep asking them until they type “no” (case-insensitive), using a do-while loop.

#include <iostream>
#include <string>

using namespace std;

int main(){
    string response;
    do {
        cout << "Do you want more tea (yes/no): " << endl;
        getline(cin, response);
        cout << "Here is your cup of tea enjoy!!" << endl;
    } while (response != "no" && response != "No");


    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Going through the code:
Here we have used the do-while loop. The loop will execute at least once, even if the condition is initially false.

do {
    // code to be executed
} while (condition);
Enter fullscreen mode Exit fullscreen mode

Here do prints the message and asks the user for input. Then it checks if the response is equal to “no” or “No”. If it is, it will execute the code inside the loop. If it is not, it will exit the loop and continue to the next line.

3.For Loop

Challenge: Write a program that prints the brewing instructions for making cups of tea. The brewing process should be printed once for each cup using a for loop.

#include <iostream>
#include <string>
using namespace std;

int main(){
    int teacups;

    cout << "Enter the of teacups you want: " ;
    cin >> teacups;

    for(int i = 1; i<= teacups; i++){
        cout << "Brewing cup  " << i << "  of tea.." << endl;
    }


    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Going through the code:

Here we are using the for loop. The loop will execute the code inside the loop for the specified number of times.

for (initialization; condition; increment/decrement) {
    // code to be executed
}
Enter fullscreen mode Exit fullscreen mode

For loop has three parts:

  • Initialization: This is where you initialize the loop variable.
  • Condition: This is where you check if the loop should continue or not.
  • Increment/Decrement: This is where you update the loop variable.

In our case, we are initializing the loop variable withi = 1 and checking if the loop should continue with i <= teaccups. If the condition is true, the code inside the loop will be executed. If the condition is false, the loop will exit and the program will continue to the next line.

Top comments (0)