For Loop Conditions

I have a bit of code that was written by someone else and I have never seen a for loop with a condition like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "Bitmap.h"
#include "Bitmap.h"     //test protection against double include
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    const char* fnames[]={"hoohah","atc203.bmp","atc204.bmp","bluesky.bmp",
        "flower1.bmp","flower2.bmp","ice.bmp","moon.bmp","yellowflower.bmp",
        ""};
    int collars[]={123,16,8,32,12,24,4,3,12};
        
    for(int i=0;fnames[i][0];++i){
        Bitmap b1(fnames[i]);
        b1.quantize(collars[i]);
        b1.save(string("quantification-")+fnames[i]);
    }
    

    return 0;
}


I'm guessing it ends when it comes across "". Am I right?
Last edited on
closed account (10oTURfi)
Its terminated with '\0'. (read: it loops till it reaches end of array)
If it help you understand it fnames[i][0] represents the condition part. If it was something like i<10 you wouldn't have any question, correct?
Well consider the condition as a statement being evaluated as true or false. If one the other hand consider that false for C (or C++) is all negative values like 0, NULL, \0, "" (I don't know if I am forgetting something) and true all others then the condition above just checks if any one these values is encountered otherwise is considered true.
Alright, So <=0 is false and >=1 is true.

Thank you guys for explaining. That's actually a rather neat way to do a for loop.

I also didn't, until now, realize that fnames was a 2d array.
Alright, So <=0 is false and >=1 is true.


Not quite. Zero is false, non-zero is true. Negative one is non-zero and thus true.
Last edited on
Topic archived. No new replies allowed.