Ad Code

Responsive Advertisement

C++ do while - learn CPP

C++ do while - learn CPP, do while is used to iterate a part of the program being used multiple times, and the iteration is executed at least once even if the condition is not met. This is because the condition is checked after execution, and this is the difference between (do while) and (while) or (for), that the condition is written after the statements to be repeated, and it is also used when we want a part of the program at least once.


C++ do while - learn CPP



Repetition begins with the command do or repeat (do), then comes a group of statements and injunctions to be repeated ending with the command (while), that is, as long as the condition comes after it. When the translator finds a statement (do), it will re-execute the statements between this command and the command (while) ), and every time the compiler accesses (while), it checks the condition that follows it. If the condition is true, the compiler returns to the command (do) and starts executing from (do) down to the command (while) again, and this process continues until it is not fulfilled. the condition.


The general form of do while:



do

{

statement 1;

statement 2;

statement n;

}

while(condition)


What is the difference between while and do while commands?


  • The while condition is at the beginning of the iteration.
  • The do while conditional is at the end of the iteration.
  • The while initially tests the condition and then executes it if this is true, and nothing can be executed.
  • The do while is executing first and then testing the condition continuously, and it has at least one execution before the condition is checked, meaning do a print or something before entering the condition.


An example of the do while command


#include<iostream>

using namespace std;

void main()

{

int j=0;

do

{

count<<j<<"\t";

j++;

while(j<9);

}


In this example, we defined the variable j=0 and we gave it an initial value and put it inside the do statement, here you will print the value of j which is 0, then the value will increase by 1, and then enter the while condition since the condition is that the value of j is less than 9 and since The condition is true. It goes back to the command do and executes what is inside it, and so on unless the condition becomes unfulfilled. In this example, it will print the numbers from 1-to 8 and a space between each number and the other.


Note: t\  Its function is to leave 8 blanks.

Learn about c++ data types here.