When is it better to use a "do while" vs a "for" loop?

When is it better to use a "do while" vs a "for" loop?

Attached: for_vs_while.jpg (432x411, 71K)

If you need to ask, u shouldnt be Programming u logic less nigger

I like how flowcharts make a huge mess of arrows and blocks for something that could be simply explained with gotos.

expr1;
while(expr2)
expr3;


becomes

expr1;
loop:
if(!expr2)
goto end;
expr3;
goto loop;
end:


and

for(expr1;expr2;expr3)
expr4;


becomes

expr1;
loop:
if(!expr2)
goto end;
expr4;
expr3;
goto loop;
end:

Take a look at these two examples:
while (condition) {
block.execute();
condition = check_condition(block);
}

vs
for(i = 0; i < known_number; ++i){
operation(value[i]);
}

Without knowing a larger context (as I don't care to write it here), the wile loop runs until a condition is met, where the for loop runs a predetermined number of times.
If you were to use it in a sentence, do you want to do an operation for each value or while something is required?

>all you need are if and while statements to write the logic of any program.
>However, many programming languages provide additional statements because they allow
>you to express some kinds of logic more conveniently than using if and while.
>The for statement is a good example

while - if you dont know exact number of needed iteration "a priori", you just know what is condition and that it will be eventually achieved

No-one has actually answered your question (which is specifically about do-while, not regular while).

It's slightly unusual to compare do-while to for; normally you would compare it to regular while. I'd say do-while makes sense if you /always/ need at least one iteration, and might need more depending on the circumstances.

I tend to avoid it anyway for readability's sake. I prefer to know what the condition is before I read the loop body, not after.

don't listen to these stupid examples. for loops aren't only for when you have a fixed number of elements.

for loops should be used when you're advancing a pointer/index until a certain condition is met, and said condition is a function of the pointer/index.

for (initialize(p); condition(p); advance(p)){
do something with p

// note that conditions which do not depend on p should not be in the loop's conditional statement. example:
if (user_canceled) {
break;
}
}


if your condition is not a function of p, you should use while.

finally, if your condition does not apply to the first iteration, you should use do-while:

int failed;
do {
failed = download_file();
} while(failed);

You can think of one as result dependent, and the other as initial conditions dependent.
I guess the problem is that you're taught to make loops with while and for and if in a simple example, not within a context.
But let's say it's a subroutine, then you have variables that are changing.
If cases look at certain conditions and then apply a routine or not, you could loop inside if but it's not strict in that regard.
While will make the flow enter into a loop until a condition is met, and it's predetermined how while acts, the flow operates within while but not dependent directly on it.
For will take the flow, and operate based on a variable which may have been handled before, or applied to a condition which depends on a variable that will be transformed in the for, not a standalone condition so to speak, the variable may be transformed outside too and then input back into the for.

Just try to make sentences in your head where something is achieved, using FOR or WHILE, and it has to be true or false.
So...
For X packages, print X labels.
As you can see, let's say 100, it'll know it has to be 100, then the instruction will be fulfilled and the for will be met, it'll have no problem because the variable will always be met and then used as a way to complete it.
While X packages available, print label for each.
So, this one will operate continuously since the variable hasn't been input, it doesn't know, but it can still work because it'll keep checking that they are available and keep printing labels for each.

You can combine them to be more effective. Let's say, tables, you want the loop to work until result is met, but you work with tables (matrices) so you can use the for to work within parameters and not all the data in the tables.

Loading data from a file terminated by an EOF marker
^

For loops unless unless you have to use a do while

note that here when I say function I mean in the math sense, not in the programming sense.

so,
for (int i = 0; i < length; i++) {

}


the condition i < length is a function of i and length. the advancing logic (i++) is a function of the previous value of i.

for (char *p = str; p != '\0', p++)

(for iterating over a null-terminated string where you don't know the length before-hand)

for (element *p = start; p->next != NULL; p = p->next)

for iterating over a linked list

and so on...

notice that all of these examples have an initialization logic for a pointer/index, a condition **that is a function of the pointer/index**, and an advancing logic that modifies the pointer/index **as a function of the previous value of the pointer/index**.

Sorta unrelated but is it better to break a while loop using the arguments, or using a break statement within the statement? I usually only use the break statement in conjunction with a switch.

Attached: 1473438013594.png (272x265, 74K)

An example is when writing digits out. This writes the digits of x (in reverse order)
for (; x != 0; x /= 10) write(x%10)
but it doesn't write anything for 0 while this
do {
write (x%10)
x /= 10
} while (x != 0)
works correctly.

I usually terminate for loops with using i=999999 (or i=0 if I'm counting backwards)

do X while B is just equivalent to
while true {
X;
if !B break;
}

You're doing it wrong.
If you can't meet the conditions of your loops then you're doing your instructions wrong.
Always fulfill a part of your instructions which will release it to the next one.
Use IF before, then implement a loop to the specific IF scenarios.

This is the kind of shit that makes people use GOTO like fucking retards.

not true because in do X while B, while B is outside the scope of the loop block, and inside the scope of the loop block in the [while ... if !B break] variant.

so
declare B;
do {
redeclare B;
X;
} while (B);


vs

declare B;
while (true) {
redeclare B;
X;
if (!B) break;
}


breaks

But what about while loops? I had a professor mark me down for using an argument to break instead of
while (true){
If(x)
break ;
}

Also doesn't do the same thing if B contains a continue.

>learning vb loop syntax
who the fuck came up with that shit

break if it's an edge case or you're doing something after breaking

example:
while (true) {
input = get_input();
if (validate_input(input) == false) {
break;
}
process_input(input);
}

if you were to use a flag variable to indicate you're done, the code would be significantly more confusing and harder for the compiler to optimize:

bool flag = true;
while (flag) {
input = get_input();
if (validate_input(input) == false) {
flag = false;
}
else {
process_input(input);
}
}


jesus. just use break. even goto is better than this. use the actual statement that signifies breaking out of the loop.

You shouldn't be using loops, you should be using list operators (map, fold, reduce, zip...).

never, you should only use jumps

using a while loop let's you have better control and flexibility over the loop, which you need sometimes.

This. Since it checks conditions after it loops, you have 1 guranteed run through unlike a while. A for loop is for when you know how many times to iterate through like if you want to iterate through everything in a list.

You use do-while when you need to perform a while loop at least once no matter what.

You use a for-loop when you need to iterate over some range or data structure.

There aren't going to be many cases where you'll feel that either or would be appropriate, and when those do occur, it doesn't fucking matter which one you use. Ask yourself how you want the program to behave, and write code that makes it behave like that.

Your professor is a moron.

t. A guy working on his PhD so he can become a professor.

only use for, for consistency.
You will make less errors this way, even if you have to be a little more verbose sometimes (not often at all)

If im trying to find something in a collection, break removes redundant work.

while is more generic, you can accomplish everything a for loop can with a while loop by manually incrementing an iterator and setting a limit to it in the condition.

use tail recursion instead because it looks cooler.

this

Main usage for do while:
#define _stuff(x) do { foo(x); bar(x) } while(0)

That's what I thought

while loops are better when you need to loop potentially n plus 1 (or more) times instead of just n times. Very rarely does this occur though