C++ Loops & Switch


1. Details of C++ Loops

Loops in C++ allow certain instructions to be carried out repeatedly without writing them multiple times. Instead of manually repeating the same lines of code, loops automatically execute a block again and again based on specific rules or limits you set. This saves time, reduces errors, and makes your programs more efficient.

By using loops, your program gains the power to cycle through values, check items in a sequence, or perform tasks until a goal is met.


Types of Loops

  • for Loop: fits perfectly when the repeat limit is predetermined and does not change during execution.
  • while Loop: Runs as long as a given condition remains true; good for uncertain repetitions.
  • do-while Loop: Similar to while, but guarantees at least one execution before checking the rule.

for Loop

Use a for loop when the repetition count is fixed and you already know how many times the steps should run.

Example

#include <iostream>  

int main() {     
      for (int num = 1; num <= 3; num++) {         
              std::cout << "Count: " << num << "\n";    
       }     
       return 0; 
} 

Explanation

  • int num = 1; begins the counter from one.
  • num <= 3; keeps the loop going while the number is less than or equal to three.
  • num++ bumps the value by one after each round.
  • Output appears three times: Count 1, Count 2, Count 3.

while Loop

The while loop keeps running as long as a condition stays true. It’s perfect when you don’t know in advance how many times something should repeat.

Example

#include <iostream>  

int main() {     
      int value = 0;      

      while (value < 4) {         
             std::cout << "Value is " << value << "\n";         
             value++;     
        }      

        return 0; 
} 

Explanation

  • value begins at zero.
  • The condition value < 4 is checked before each cycle.
  • Output shows numbers from 0 to 3.
  • The loop ends once value reaches four.

do-while Loop

This loop always performs its content at least once, even if the condition is false right from the beginning. The rule is checked after the body runs.

Example

#include <iostream>  

int main() {     
       int tries = 1;      

       do {         
               std::cout << "Try number: " << tries << "\n";         
               tries++;     
        } while (tries <= 2);      

        return 0; 
} 

Explanation

  • tries starts with one attempt.
  • Message prints before the condition is verified.
  • It runs twice and exits after tries reaches three.

Conclusion

Loops make your code smarter by cutting out unnecessary repetition. Whether you're printing patterns, processing lists, or waiting for a condition to change — loops help you handle it all automatically. Learning when and how to use each loop gives you strong control over repetition in your C++ programs.


2. switch Statement in C++

The switch structure chooses what code to run based on a fixed value. It checks against different possible cases and runs the matching block. It's often used as a cleaner alternative to many if-else checks.

Example

#include <iostream>  

int main() {     
      int day = 3;      

      switch (day) {         
           case 1:             
                   std::cout << "Monday\n";             
                   break;         
           case 2:             
                   std::cout << "Tuesday\n";             
                   break;         
           case 3:             
                   std::cout << "Wednesday\n";             
                   break;         
           default:             
                  std::cout << "Other day\n";     
       }      

        return 0; 
} 

Explanation

  • Sets day to 3.
  • Compares that value with each case.
  • When a match is found (case 3), it runs that section.
  • break stops extra code from running.
  • If no match is found, the default section runs.

Conclusion

Loops allow repeated tasks without writing the same lines again and again, while the switch command offers a neat way to choose from multiple actions based on one value. Together, they give your code flexibility and cleaner logic. With these tools, you can build more dynamic, smarter, and efficient C++ programs.


Prefer Learning by Watching?

Watch these YouTube tutorials to understand C++ Tutorial visually:

What You'll Learn:
  • 📌 Conditionals (Switch)
  • 📌 for and while Loops
Previous Next