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.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay