Tag c++ string

C++ Arrays and Loops

Loop Through an Array You can loop through the array elements with the for loop. The following example outputs all elements in the cars array: Example // Create an array of strings string cars[5] = {“Volvo”, “BMW”, “Ford”, “Mazda”, “Tesla”};…

C++ Arrays

C++ Arrays Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type, specify the name of the array followed by square brackets and…

C++ Break and Continue

C++ Break You have already seen the break statement used in an earlier chapter of this tutorial. It was used to “jump out” of a switch statement. The break statement can also be used to jump out of a loop.…

C++ For Loop Examples

Real Life Example To demonstrate a practical example of the for loop, let’s create a program that counts to 100 by tens: Example for (int i = 0; i <= 100; i += 10) {   cout << i <<…

C++ The foreach Loop

The foreach Loop There is also a “for-each loop” (also known as ranged-based for loop), which is used exclusively to loop through elements in an array (or other data structures): Syntax for (type variableName : arrayName) {   // code…

C++ Nested Loops

Nested Loops It is also possible to place a loop inside another loop. This is called a nested loop. The “inner loop” will be executed one time for each iteration of the “outer loop”: Example // Outer loop for (int…

C++ For Loop

C++ For Loop When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop: Syntax for (statement 1; statement 2; statement 3) {   // code…

C++ While Loop Examples

Real Life Example To demonstrate a practical example of the while loop, we have created a simple “countdown” program: Example int countdown = 3; while (countdown > 0) {   cout << countdown << “\n”;   countdown–; } cout <<…

C++ Do/While Loop

The Do/While Loop The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is…