e6390af40fd246928c9f6b67562cf0fa37712fa8

10/14/2017

How to use While loop in c++ | While Loop Structure and Examples in c++

In this post we will discus about the loops and their basic structures.First of all we will discus why we use loop in c++ programming language and other languages like Java, PHP, and C# etc.The basic reason of using loop is to minimize the code and effort of a person to write the code.There are three types of loop in c++ and the one other thing we use is goto statement in c++ that is use as a loop.
The work of the all three loops is same that is to repeat some code in c++ but one thing is different that is the Basic Structure of the loops.Here we first will discus about the WHILE LOOP and its basic structure.That is given bellow.

Syntax :
#include<iostream>
using namespace std; // Header files
void  main()
{ //start of main
       
    int i=1; //this is the starting point of the loop
    while(condition);
     {
           // body of the loop
           i++; //this will increment  in i of 1 after every itration.

     } 
     system("pause");

}//end of main

Example 1 : 
Write a program that take Table no from the use and print table of the given number from 1 to 10 in c++ programming Language.
Solution : 
#include<iostream>
using namespace std; // Header files
void  main()
{ //start of main

     int no;  //this is the variable to take table Nimber from the user.
     int i=1; //this is the starting point of the loop

     cout<<" Enter the Table Number "<<endl;
     cin>>no; 
      while(i<=10);//this is condition of loop
     {
      cout<<no<< " * "<<i<<" = "<<no*i<<endl; // this line print table
           
       i++; //this will increment  in i of 1 after every itration.

     }
     cout<<"\n\n\n"<<endl;
     system("pause");

}//end of main

Example 2 : 
Write a program that take Table number, starting and end point of the table from the use and print table in c++ programming Language.
Solution : 
#include<iostream>
using namespace std; // Header files
void  main()
//start of main
           
     int no;  //this is the variable to take table Nimber from the user.
     int st; //this is the starting point of the loop
     int end;//this is the ending point of the loop
     cout<<" Enter the Table Number "<<endl;
     cin>>no; 
     cout<<" Enter the starting point "<<endl;

     cin>>st; 
     cout<<" Enter the Ending Point "<<endl;

     cin>>end; 
      while(st<=end);//this is condition of loop
     {
      cout<<no<< " * "<<i<<" = "<<no*i<<endl; // this line print table
           
       st++; //this will increment  in start of 1 after every iteration.

     }
     cout<<"\n\n\n"<<endl;
     system("pause");

}//end of main







TAGS:

tutorial, c++, programming, beginners, game, computer, tutorials, c (programming language), loops, how to use for loop in python, how to use for loop in matlab, how to use for loop in c++, how to use for loop in jquery, how to use for loop in html, how to use for loop in javascript, how to use for loop in swift, how to use for loop in php, how to use for loop in java, how to use for loop in laravel, how to use for loop in c++, how to use for loop in matlab, how to use for loop in python, how to use for loop in php, how to use for loop in excel, how to use for loop in javascript, how to use for loop vba, how to use for loop in shell script, how to use for loop in c++ in hindi, how to use for loop in vba, how to use for loop in c programming, how to use for loop in python, how to use for loop, how to use for loops in java, how to use for loop in android studio, how to use for loop, how to use for loop c++, program, thecherno, basic, how, plus, vector, c++ tutorial, learn c++, development, c programming, do while, control flow, while loop, tutorial (industry), how to make a game engine, how c++ works, thechernoproject, game engine, complete, project, code, game development, game programming, how to make a game, source, language, string, roberts, thenewboston, lesson, pointers, class, opengl, glfw, howto, learn, advanced, while, for, engine


No comments:

Post a Comment

Privacy Policy for Apps

Privacy Policy Muhammad Shahbaz built the Health Physio Association app as a Free app. This SERVICE is provided by Muhammad Shahbaz at no...