repetitive task ----> use loop!
<for loop>
#include <iostream>
int main()
{
using namespace std;
int x;
for(x=0;x<9;x++) //int the parameter you need 3 separate things(how many times for each=1, where stop, the start point)
{
cout<< x <<endl; //0 to 8
}
system("pause");
return 0;
}
<while loop> //naked for loop
#include <iostream>
int main()
{
use namespace std;
int x = 1;
while(x<=10) //task condition in the parentheses
{
cout<< x <<endl; //1 to 10
x++; // x = x+1;
}
system("pause");
return 0;
}
Diffrence?
- at the beginning, increment in the body
- while(...if it is false the body is not gonna run)
<Do While Loop> //Famous!
//Allow the execute the code at least one time and then verify the condition
#include <iostream>
int main()
{
using namespace std;
int x=55;
do
{
//instruction to do
cout<< x <<endl;
x=x+1;
}while(x<=10);
//then how many times you want to do this?
//if false then it prints out just once
system("pause");
return 0;
}
'Computer General > C++ development from the beginning' 카테고리의 다른 글
The if Statement (0) | 2014.12.12 |
---|---|
Address Operator & Pointer (0) | 2014.12.12 |
Beginning Arrays (0) | 2014.12.12 |
User Defined Functions (0) | 2014.12.12 |
Beginning Math Functions (0) | 2014.12.12 |