I think its a forever loop but can a for loop also do this?
Pls help.
I think its a forever loop but can a for loop also do this?
Pls help.
"Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
-Bruce Lee
What is it exactly that you are looking for. Do you mean a while loop?Originally posted by correlcj
I think its a forever loop but can a for loop also do this?
Pls help.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
do/while - posttest always one trip
while and for- pretest- 0 or more times
Those are the only three loops in C!
Mr. C: Author and Instructor
i would guess he's asking for one trip loops, but maybe thats just meOriginally posted by MrWizard
What is it exactly that you are looking for. Do you mean a while loop?
mister C has already outlined the answer tho
hello, internet!
quick recap. The 3 types of conditional loop in c/c++ are :-
TOPTESTED LOOP
This is a while loop. The loop will continue while the condition is true.
BOTTOMTESTED LOOPCode:while(1) // will loop forever. same as while(true) { // do something}
This is a do/while loop. This type of loop always executes once and then the condition is tested and looping continues while condition is true.
COUNTED LOOPCode:do {// do something} while(condition);
This is a for loop. This type of loop occurs for a counted number of iterations. for instance this will loop 10 times.
Code:for(int i=0;i<10;i++) {// do something}
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
A forever loop.I think its a forever loop but can a for loop also do this?
Maybe this?
/*edit*/...damn smilies...Code:#define EVER ( ;; ) forEVER { }
Last edited by Sebastiani; 10-26-2002 at 12:20 AM.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
in other words...
What I understood from the 1-trip loop is that you are taliking about the do/while loop.
do{
//statements
} while( condition )
the statements will be executed at least once even if the condidiotn is false.
that's a very poor and inaccurate way to describe a for loop.COUNTED LOOP
This is a for loop. This type of loop occurs for a counted number of iterations. for instance this will loop 10 times.
[/B]Code:for(int i=0;i<10;i++) {// do something}
hello, internet!
three types? i thought there were only two types:
Conditional: Do..while / while etc
Counter: For
I don't see how for loops are counted and while/do..while loops are conditional. Most while/do..while loops are counted anyway (if not all)
int i = 0;
while (i < 10) { /* do something */ };
for (int i = 0; i < 10; i++) { /* do something */ };
int i = 0;
do { i++ /* do something else */ } while (i < 10);
All loops can be both counted and conditional, but for-loops are especially made to be counted and while-loops are made to be conditional.Originally posted by Eibro
I don't see how for loops are counted and while/do..while loops are conditional. Most while/do..while loops are counted anyway (if not all)
int i = 0;
while (i < 10) { /* do something */ };
for (int i = 0; i < 10; i++) { /* do something */ };
int i = 0;
do { i++ /* do something else */ } while (i < 10);
Making for-loops conditional have no real purpose (it's way easier to use a while loop).
Examples of non-counted while loops:
Code:bool Looping = true; while(Looping) { ... if(kbhit()) Looping = false; }Edit:Code:while(GetMessage(...)) { TranslateMessage(...); DispatchMessage(...); }
If you want an easier counted loop function:
Code:#define LOOP(n) for(int i=0; i<n; i++) int main() { LOOP(5) cout << i; LOOP(100) { cout << "I'm the best!" << endl; } return 0; }
Last edited by Magos; 10-26-2002 at 10:21 AM.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Zero and 1 trip ability sounds like an 'if' statement to me, not a loop.
That is the question posted to me in the book. Its asking for one or more than one loop(ie. while, do/while, forever or for) these can only be the answers. I can only assume that it must be them all and is a trick question. I am stillWhat C++ loops can provide both zero and 1-trip behavior
I understand that there are 3 types of loops really but c++ provides 4 different loops.
1. while
2. do
3. for
and forever which is a simpified for loop eg;
but actually there are 3.Code:for ( ; ;) { statement1; if (condition) break; statement2; }
Thanks for the posts, i am going to say all of them and see how that flies. weird question?
"Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
-Bruce Lee
The correct answer is the forever loop because it eliminates redundant coding and it can be used in both times where statements must be executed 0 or more times and other statements must be executed 1 or more.
THANKS! for everyones input!!
"Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
-Bruce Lee
You should try to only use the for loop and the while
loop. Try to never use infinite loops, do while, goto,
break(except in switch statements) or continue.
This is because you
reason somewhat about the correctness. So you have
Code:while(<condition>) { <condition> = true inside the loop } <condition> = false for(<stmt_op>; <condition>; <stmt_op>) { <condition> = true inside the loop } <condition> = false