Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)
Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

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

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

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.




CODE
#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;
}






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.




CODE
while (condition){
// Code to be executed
}






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.




CODE
#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;
}






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.




CODE
do {
// code to be executed
} while (condition);






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.




CODE
#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;
}






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.




CODE
for (initialization; condition; increment/decrement) {
// code to be executed
}






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.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
Use custom web fonts in Google Sheets charts
2 Quellen
Introducing the new 1Password App for Google Chat
1 Quelle
Context-aware access controls are available for Gemini Enterprise in the Admin console
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Day 7: Unlocking the Power of Loops in C++

Thematisch verwandte Begriffe: Unlocking, Power, Loops · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...