#include
using std::cout;
int main()
{
int a = 0;
while (a != 11) //While a does not equal 11
{
a++; //add 1 to a
cout << a << "\n"; //print a and add newline
}
cout<<"The loop has ended";
return 0;
}
A 'do while' loop in C++ is a way to perform a few lines of code multiple times in a row. Say you wanted to print out the line "Hello, world" ten times to the screen. To print it out just one time you would use the code
cout << "Hello, world" << endl;
Let's say you wanted to do this ten times. To do this, you need a counter to track the number of times you have done it. You can accomplish this with a 'while' loop:
int i = 0; // This is your counter
while (i < 10) {
cout << "Hello, world" << endl; // This prints out the line
i++; // aka i=i+1. This indicates that you have performed the code 1 more time
}
As another example, let's say you already had a variable called indicator, and you only wanted to execute some code when indicator was less than 3 (or when indicator was false, or whatever you want). BUT maybe you want to execute the code at least once, even if indicator starts off greater than 3. This is the situation in which you want to use a 'do while' loop.
In the first example, nothing gets printed out to the screen:
int indicator = 4;
while (indicator < 3) { // At the very beginning, indicator is NOT less than 3!
cout << "Hello, world" << endl;
indicator++;
}
In this second example, "Hello, world" gets printed ONCE:
int indicator = 4;
do {
cout << "Hello, world" << endl;
indicator++;
} while (indicator < 3);
There are four ways to make a loop in C or C++, three of which are standard, and one of which (the goto) is non structured, non preferred...
for (init_expression; test_expression; loop_expression) statement;
while (loop_expression) statement;
do statement; while (loop_expression);
loop: statement; if (loop_expression) goto loop;
There are basically 3 looping statements :
1. for loop
2. while loop
3.do while loop
printf ("x")
Yes, you can use for-loop in a C program compiled by Turbo C.
No, why did you think so?
#includeint main(){int i;for(i=2;i
* ** *** **** simply use dis... { int x,y; for(x=1;x<=4;x++) { for(y=1;y<=x;y++) printf("*"); printf("\n"); } }
printf ("x")
Add the missing parts.
how do we use loops in c plus plus programing and what are basic differences between do,for and while loop
It depends on what program you design really
void line(int length) { for(int i=0; i<length; ++i) std::cout<<'_'; std::cout<<std::endl; }
Yes, you can use for-loop in a C program compiled by Turbo C.
odd loop means at least the loop execute once.
In C++, a for loop is structured as follows: for( int index = 0; index < 10; ++i ) { //do something }
No, why did you think so?
kk
Yes, you can program games with C++.
Exactly what do you mean by 'C program in Java'