C++ - Loops
Chapter 7: Loops
In this chapter, we will learn about loops in C++. Our learning will be based on the task based approach. Each task will help you to understand the requirements and then you will be able to implement the code.
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.
Solution:
Going through the code:
We have already gone through the basic code already many times, in this chapter we will focus on the while
loop.
- while loop is used to execute a block of code as long as a condition is true.
- The condition is checked before each iteration of 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.
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.
Solution:
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.
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 5 cups of tea. The brewing process should be printed once for each cup using a for loop.
Solution:
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 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 with i = 1
and checking if the loop should continue with i <= teaCups
. 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.
4. Break Keyword
Challenge: Write a program that keeps serving tea until the user says they’ve had enough (input ‘stop’). Use a break statement to exit the loop when the user types ‘stop’.
Solution:
Going through the code:
In this case, we are using the break
keyword to exit the loop when the user types ‘stop’. The break
keyword is used to exit the loop immediately, without executing the remaining code inside the loop.
5. Continue Keyword
Challenge: Write a program that skips brewing green tea if the user dislikes it. Use a continue statement to skip over green tea but brew other types of tea in a list.
Solution:
Going through the code:
In this code solution, we are introducing 2 new concepts:
string teaTypes[5] = {"Oolong tea", "Orange Tea", "Green Tea", "Black Tea", "Lemon Tea"};
This line declares an array of strings with 5 elements.
Array is a collection of elements of the same type. In this case, we are declaring an array of strings. In memory, the array is stored as a contiguous block of memory.
Also we are using the continue
keyword to skip over the green tea. The continue
keyword is used to skip the remaining code inside the loop and move to the next iteration of the loop.
6. Nested Loops
Challenge: Write a program that brews multiple cups of different types of tea. For each type of tea, brew 3 cups using a nested loop.
Solution:
Going through the code:
In this code solution, we have already gone through the concept of Array. Further, we are using nested loops to brew multiple cups of different types of tea. The outer loop is used to iterate over the array of strings, and the inner loop is used to iterate over the number of cups to be brewed.
For every iteration of the outer loop, the inner loop will execute 3 times. The inner loop will print the number of cups being brewed and the type of tea being brewed.
Summary
In this chapter, we have learned about loops in C++. We have also learned about the different types of loops and how to use them. By the end of this chapter, you should have a good understanding of how to use loops in C++.