Programmingoneonone is known for sharing best programming knowledge.

While and do while statements in c++ programming


While and do while statements:

while and do while statements in c++ programming, learn c++ programming
learn c++ programming

In c++ we used while and do while statements for the repetition of action till the condition is not satisfied. 


while statement:

while statement is used in to repeat an action while some condition remains true. 

syntax:
             while(condition)
             {

             code for execution;

             }

Note: in while statement the loop continuation condition test occurs at the beginning of the loop before the body of the loop executes.    

Example:
              #include <iostream>
              int main()
             {
                 int a;
                 a=1;

              while(a<=10)

              {

                 cout<<"\n you learning c++";

               }

               return 0;

              }
           
conclusion:
The initial value for the i is 1. then the compiler should check the first condition for while loop. if the condition is true then compiler entered into the while block and execute the defined code. 
in this example the initial value for i is 1. and the condition for while loop is [1<100] is true. so the compiler entered into the while block and print the message "your number is 1" and after that value of i is increment by one. so now the value of i is 2. and now the compiler again checks the condition for while statement and if it's true then again it follows the same process and executes the code which is in the while statement. 
so in this example, the while statement is executed 9 times because the conditions are I <10.

Note: the major big difference in the while and if statement is that if statement is only executed for one time if the condition is true for it. and in case of while statement the while statement is repeatedly executed till the condition is satisfied.  

likewise if statement in while statement we can also combine two conditions:

Example
        while(a<100 && a>1)
        {

         std::cout<<"your number is:"<<a;

        }



Do while statement in c++ programming:


do while statement is also used for repeat an action while some condition remains true. 

syntax:
             do
            {
               code for execution;       

             }while(condition);


Note: in do while statement tests the loop continuation condition after the loop body executes, therefore the loop body always executes at least once. 

Example:
          #include <iostream>

          int main()

          {

             int a;

             a=1;

              do

              {

               cout<<"\n you learning c++";

              }while(i<=10);

               return 0;
          }

Note: do not forget to place a semicolon at the end of while statement.

conclusion:
in this example the initial value for i is 1. so the compiler first enters into do block and then print the statement "you are learning c++". and then check the while condition. if the condition is true then it again goes to the do block and executes the code under the do block. in this example the condition for while statement [1<10] is true. so the compiler again goes to the do statement and execute the code under the do block. and stop when the condition for while statement is false. 


Difference between the while and do-while loop


the major difference between the do while and while loop is that do while loop always executed once. if the condition is true or not. but the while condition depends on the condition of while statement. 

if you like this post or you think that this post is helpful for you or it gives you something new knowledge, then please share this post.

No comments:

Post a Comment