-4

First of all, I am not very good at programming, and sorry for my bad english. I am having trouble understanding this following C-program. I can see the program consists of a for-loop, but why not use a do- or a while loop? Sorry if this is a stupid question. Thanks for your help.

void opgave_1 (loebsdata2017 *alle_loebsdata2017 ) {
    int i = 0;

    for (i = 0; i < MAX_RYTTERE; i++) {
        if(alle_loebsdata2017[i].rytteralder < 23 && 
           strcmp(alle_loebsdata2017[i].nationalitet, "BEL") == 0)
        {
            printf("%s %s %d %s \n", 
                alle_loebsdata2017[i].rytternavn, 
                alle_loebsdata2017[i].rytterhold,
                alle_loebsdata2017[i].rytteralder, 
                alle_loebsdata2017[i].nationalitet);
        }
    }
} 
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • you could, but since you don't want to change `i` in the loop, it's better with t a `for` loop. – Jean-François Fabre Jan 05 '18 at 17:19
  • what do you thing happen if `MAX_RYTTERE` is 0 or less with a `do {} while ();` loop ? In iterating context a for loop is idiomatic. – Stargateur Jan 05 '18 at 17:19
  • 1
    Well this was author's choice. And yes you can use `while` loop. – user2736738 Jan 05 '18 at 17:20
  • Its neater and more concise - but thats about all I can think of. – FreudianSlip Jan 05 '18 at 17:20
  • a `do` / `while` loop will execute at least once, so if your max value is <= 0 this isn't equivalent. – Jean-François Fabre Jan 05 '18 at 17:21
  • 3
    for loop is the idiomatic way of looping with an incrementing counter / index. You can code many other ways but 99% of C programmers will be surprised. Its also nice and simple syntax and sematics – pm100 Jan 05 '18 at 17:22
  • Thanks for all your answers. Would it be more simple to use a while-loop? I am not very good at this, but I really want to know how I can argue using a while-loop instead of a for-loop.. – Rachel Hutchings Jan 05 '18 at 17:30
  • 4
    Possible duplicate of [Understanding For loop in C](https://stackoverflow.com/questions/12327652/understanding-for-loop-in-c) – Uwe Keim Jan 05 '18 at 17:30
  • @RachelHutchings.: argue for? – user2736738 Jan 05 '18 at 17:31
  • 2
    @RachelHutchings Usually a for loop is when you have to increment a counter. A while loop is when you need to loop until a certain condition is met. That's the reasoning I use. – Rivasa Jan 05 '18 at 17:31
  • You can use a `while` loop .. you'll just have to update the loop control variable `i` "manually". The `for` loop does this for you (in a sense) all in one line when you create the loop. It would be perfectly fine to do `int i=0; while (i < MAX_RYTTERE) { ... /* do work */ ... i++ }` – yano Jan 05 '18 at 17:33
  • The `for(...)` loop is easiest to read, all loop logic happens on one single line. After reading this line you can easily verify that no `xxx[i-1]` or `xxx[i+1]` happens inside the loop body, an that `i` is not changed. (also: `break` and `continue` will work as expected. ...in most cases...) – joop Jan 05 '18 at 17:35
  • I now understand why for loops are easiest to read and control. Thank you so much for your help! – Rachel Hutchings Jan 05 '18 at 18:06
  • Possible duplicate of [For vs. while in C programming?](https://stackoverflow.com/questions/2950931/for-vs-while-in-c-programming) – Mark Benningfield Jan 05 '18 at 18:55
  • `continue` in a `for` loop will update the loop count, but in a `while` loop you have to do it before you execute the `continue` (mind you, `continue` often means badly thought-out flow control). – cdarke Jan 05 '18 at 20:28

1 Answers1

1

you can use a while loop but in this case you have to increment the i inside the loop for example

while(i < MAX_RYTTERE)
{

/*Some stuff*/

i++;
}

and if you want to use the do while that's means you want the code inside the brackets to be executed at least one time

do
{
/*Some stuff*/
}
while(/*Condition*/)

But if you are asking about the for loop syntax that's will be pretty simple for(/initialization statement/; /Condition to check after each iteration/;/*iteration code executed after each iteration */) {

/code to be repeated/

}

M.Fatouh
  • 36
  • 4