How To Use Do/While Loops in C++

 How To Use Do/While Loops in C++

In this blog I will teach users how to use Do/While Loops in C++,.


A While Loop is a controlled flow which allows users to execute lines of codes repeatedly according to a boolean condition, For example in the brackets of a while loop the condition could be loop while num1 is equals to 1 and the lines of code within the while brackets will continue to loop until the condition is false.



while (boolean condition) {
  Lines of code to be repeated
                                }


The example below will show a line of code with a while loop with a condition containing a variable that will increment after every loop until it returns false and ends the loop.

EXAMPLE: 

int num1 = 1;

while(num1 <= 5)
{
printf("%d\n", num1);

}

OUTPUT:







A Do-While Loop is simialar to a while loop but the only difference is that the loop within the "do" curly brace will run atleast once before verifying if the condition that determines if the loop will run again is true or false.




do{
    Blocks of code to be executed atleast once
}while (boolean condition);
 



EXAMPLE:

int num = 1;

do{
printf("%d\n", num1); 

}while(num <= 5);


OUTPUT:












Comments

Popular posts from this blog

How to Open a C++ Project on VS22 and get started

How To Write to File on C++

How To Use Arrays in C++