Is this if-else statement legal

P

paul

Hi all,
I've been handed some code and, unless I've got the numbering of
parentheses wrong, one of the functions has a curious if-else
statement. The thing compiles but is it right?

I know the code contains non-standard C++, but its only the if
statement I'm after. It seems to
go:

if (condition){

}
else
{

}
else

??????????????


void CAudioProcessFrame::Voice(int iChannel, bool bDetected)
{

if (g_bVoiceTraining)
return;

if (m_bVoice[iChannel]!=bDetected) {

char szCad[512];

if (bDetected) {

if (!m_iVoiceStarted[iChannel]) { // So we have detected a voice and
it hasn't started yet.
m_iVoiceStarted[iChannel]=g_ticks;
debug.printf_(" Sound started %i \n",g_ticks);

m_EventDetected[iChannel].in_msec=libVAD_GetCurrentTimestamp(); //
When has this sound event started?
m_EventDetected[iChannel].out_msec=libVAD_GetCurrentTimestamp();



#ifndef NDEBUG
g_AudioBuffer.clear();
#endif

m_iCaptureAudio[iChannel]=1;
}

m_iVoiceEnded[iChannel]=0;
sprintf(szCad," Voice detected on channel %i card %i [%s]",
iChannel, m_iIndex, m_sCardName.c_str());


} else {

if (!m_iVoiceEnded[iChannel]) {
m_iVoiceEnded[iChannel]=g_ticks;

debug.printf_(" Sound ended %i \n",g_ticks);
}

int iElapsed=g_ticks-m_iVoiceEnded[iChannel];
if (iElapsed>g_iMilisPause) {

int
iTotalVoice=(m_iVoiceEnded[iChannel]-m_iVoiceStarted[iChannel]);

if (iTotalVoice>g_iMilisMinimumLength) {

sprintf(szCad," End of voice on channel %i card %i [%s]",
iChannel, m_iIndex, m_sCardName.c_str());


m_EventDetected[iChannel].channel=iChannel; // OK, we have a voice
and it is of the correct length
// so, we write the details to the struct (above) and then onto the
queue.


m_iCaptureAudio[iChannel]=0;

#ifndef NDEBUG
g_AudioBuffer.setName(this->m_sCardName);
g_AudioBuffer.write();
#endif


debug.printf_(" Ok Sound length %i
\n",m_iVoiceEnded[iChannel]-m_iVoiceStarted[iChannel]);
debug.printf_(" Sound Energy on Channel %i = %f \n",iChannel,
m_fSoundEnergy[iChannel]);


} else {
debug.printf_(" Fail Sound length %i < %i
\n",m_iVoiceEnded[iChannel]-m_iVoiceStarted[iChannel],
g_iMilisMinimumLength);

#ifndef NDEBUG
g_AudioBuffer.clear();
#endif
}

m_iVoiceStarted[iChannel]=0;

} else {

return;
}
}

} else //<------------------------- extra else ????
m_iVoiceEnded[iChannel]=0;

m_bVoice[iChannel]=bDetected;

}
 
V

Victor Bazarov

I've been handed some code and, unless I've got the numbering of
parentheses wrong, one of the functions has a curious if-else
statement. The thing compiles but is it right?

If it compiles, it must be right. The 'if' statement is probably
the one all compilers by now handle correctly.
I know the code contains non-standard C++, but its only the if
statement I'm after. It seems to
go:

if (condition){

}
else
{

}
else

??????????????

Well, let's see...
void CAudioProcessFrame::Voice(int iChannel, bool bDetected)
{

if (g_bVoiceTraining)
return;

This one we just throw out, since it shouldn't affect anything.
The following is your code with extraneous (irrelevant) parts removed:
~~~~~~~~~~~~~~~~~
int main() {
if (condition1) {
if (condition2) {
if (condition3) {
} // closing 'if (condition3)'
} else { // this is the 'else' for 'if (condition2)'
if (condition4) {
} // closing 'if (condition4)'
if (condition5) {
if (condition6) {
} else { // this is the 'else' for 'if (condition6)'
} // closing 'if (condition6)'
} else { // this is the 'else' for 'if (condition5)'
return;
} // closing 'if (condition5)'
} // closing 'if (condition2)'
} else //<------------------------- extra else ????
// Nope, not extra. It's the 'else' for 'if (condition1)'
m_iVoiceEnded[iChannel]=0;
// closing 'if (condition1)'
}
~~~~~~~~~~~~~~~~~

Now, was it too hard? Just get yourself an editor that has "matching
parentheses" functionality and verify your curly braces (and add the
appropriate comments).

V
 
P

paul

Victor said:
I've been handed some code and, unless I've got the numbering of
parentheses wrong, one of the functions has a curious if-else
statement. The thing compiles but is it right?

If it compiles, it must be right. The 'if' statement is probably
the one all compilers by now handle correctly.
I know the code contains non-standard C++, but its only the if
statement I'm after. It seems to
go:

if (condition){

}
else
{

}
else

??????????????

Well, let's see...
void CAudioProcessFrame::Voice(int iChannel, bool bDetected)
{

if (g_bVoiceTraining)
return;

This one we just throw out, since it shouldn't affect anything.
The following is your code with extraneous (irrelevant) parts removed:
~~~~~~~~~~~~~~~~~
int main() {
if (condition1) {
if (condition2) {
if (condition3) {
} // closing 'if (condition3)'
} else { // this is the 'else' for 'if (condition2)'
if (condition4) {
} // closing 'if (condition4)'
if (condition5) {
if (condition6) {
} else { // this is the 'else' for 'if (condition6)'
} // closing 'if (condition6)'
} else { // this is the 'else' for 'if (condition5)'
return;
} // closing 'if (condition5)'
} // closing 'if (condition2)'
} else //<------------------------- extra else ????
// Nope, not extra. It's the 'else' for 'if (condition1)'
m_iVoiceEnded[iChannel]=0;
// closing 'if (condition1)'
}
~~~~~~~~~~~~~~~~~

Now, was it too hard? Just get yourself an editor that has "matching
parentheses" functionality and verify your curly braces (and add the
appropriate comments).

It is when you main developer is not too hot on English and leaves
comments (when he can be bothered to that is) in Spanish, and your boss
wants his code unmangled by yesterday!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top