0

Can anyone explain every code repeated to 'started' variable in the following code?

#include <stdio.h> int main() { float latitude; float longitude; char info[80]; int started = 0; puts("data=["); while (scanf("%f,%f,%79[^\n]",&latitude , &longitude,info ) == 3) { if (started) printf(",\n"); else started =1 ; printf("{latitude: %f, longitude: %f, info: '%s'}", latitude, longitude, info); } puts("\n]"); return 0; }

31st Aug 2020, 6:37 AM
Phyo Htet Aung
Phyo Htet Aung - avatar
3 odpowiedzi
+ 4
Oh I see ... Well, before the loop begin you assigned 0 for variable <started>. Being zero, this means <started> is evaluable as false e.g. in your `if` branch inside loop body. When the loop starts, <started> value was 0, so `else` block is executed, which sets <started> to 1. On successive loop iterations, <started> value will be 1, and thus the `if` block will be executed. The `else` block is only executed once, in the first loop iteration ... (EDITED)
31st Aug 2020, 7:08 AM
Ipang
+ 1
I didn't understand the question. Can you elaborate more clearly please?
31st Aug 2020, 6:52 AM
Ipang
0
I mean "if(started) and else started = 1" in this code the first is true and should break but why the else statement still be run?
31st Aug 2020, 6:57 AM
Phyo Htet Aung
Phyo Htet Aung - avatar