Lost in if-else conditions

I am reading a piece of code, which confuses me by its if-else conditions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
		  if( depth(resgni)==depth(curgni) )
			 if(!adj) {
				Index3 bb(path(resgni)-path(curgni));
				assert( bb.linfty()<=3 );
				Vset.insert(resgni);
			 }
			 else
				if(terminal(curgni)) {
				  queue<int> rest;
				  rest.push(resgni);
				  while(rest.empty()==false) {
					 int fntgni = rest.front(); rest.pop();					 //int fntgni = fntgnt.gni();
					 if(adjacent(fntgni, curgni)==false)
						Wset.insert( fntgni );
					 else
						if(terminal(fntgni))
						  Uset.insert(fntgni);
						else { //IdxRan fran(0,2); for(IdxIter fii=fran.begin(); fii!=fran.end(); ++fii) //for(int ord=0; ord<8; ord++)
						  for(int a=0; a<2; a++) for(int b=0; b<2; b++) for(int c=0; c<2; c++)
							 rest.push( child(fntgni, Index3(a,b,c)) );
						}
				  }
				}


Since the indentations here may not reflect the pairs correctly, can anyone let me know which "else" corresponds which "if" from a logic point of view? Are the codes starting from Line 2 thru the end are all a statement of the "if" on Line 1? Thank you.
Last edited on
Fix the indentation and see for yourself. Remember that you must have braces to include more than one statement inside the if or else parts.
WebJose,

Thanks for your input. But the thing is that I don't exactly know the correct indentation! The codes were written by someone else, not me. Therefore, my only hope is that there is a logical way to tell which "else" matches which "if". There is such a way, isn't there?

Thanks a lot!
Last edited on
It looks like this is what he was going for but I'm not sure. If it is there should be brackets around all of it after the first if statement.

1
2
3
4
5
6
7
if( depth(resgni)==depth(curgni) )
			 if(!adj) 
			 {
				Index3 bb(path(resgni)-path(curgni));
				assert( bb.linfty()<=3 );
				Vset.insert(resgni);
			 }




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
else
				if(terminal(curgni))
				{
				  queue<int> rest;
				  rest.push(resgni);
				  while(rest.empty()==false) {
					 int fntgni = rest.front(); rest.pop();					 //int fntgni = fntgnt.gni();
					 if(adjacent(fntgni, curgni)==false)
						Wset.insert( fntgni );
					 else
						if(terminal(fntgni))
						  Uset.insert(fntgni);
						else 
						{ //IdxRan fran(0,2); for(IdxIter fii=fran.begin(); fii!=fran.end(); ++fii) //for(int ord=0; ord<8; ord++)
						  for(int a=0; a<2; a++) for(int b=0; b<2; b++) for(int c=0; c<2; c++)
							 rest.push( child(fntgni, Index3(a,b,c)) );
						}
				  }
				}


Next time make him indent better and maybe have him write a comment or two because the way it's laid out is very hard to read. Plus I'm sure there are some missing brackets in there somewhere.
Last edited on
Here. The indentation matches the resulting code structure. Note that the first if encompasses everything.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
if( depth(resgni)==depth(curgni) )
    if(!adj){
        Index3 bb(path(resgni)-path(curgni));
        assert( bb.linfty()<=3 );
        Vset.insert(resgni);
    } else if(terminal(curgni)) {
        queue<int> rest;
        rest.push(resgni);
        while(rest.empty()==false) {
            int fntgni = rest.front();
            rest.pop();
            //int fntgni = fntgnt.gni();
            if(adjacent(fntgni, curgni)==false)
                Wset.insert( fntgni );
            else if(terminal(fntgni))
                Uset.insert(fntgni);
            else {
                //IdxRan fran(0,2); for(IdxIter fii=fran.begin(); fii!=fran.end(); ++fii) //for(int ord=0; ord<8; ord++)
                for(int a=0; a<2; a++)
                    for(int b=0; b<2; b++)
                        for(int c=0; c<2; c++)
                            rest.push( child(fntgni, Index3(a,b,c)) );
            }
        }
    }
Last edited on
helios,

Thanks a lot for your explanation. I think what you interpreted is exactly what the author meant.



thechad90000,

Thank you anyway for your comments!
Topic archived. No new replies allowed.