469,282 Members | 1,604 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Is this if-else statement legal

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;

}

Jun 26 '06 #1
2 1534
pa**@paullee.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 26 '06 #2

Victor Bazarov wrote:
pa**@paullee.com wrote:
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!

Jun 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ryan Lowe | last post: by
5 posts views Thread by Angelina | last post: by
145 posts views Thread by Sidney Cadot | last post: by
73 posts views Thread by Martin Johansen | last post: by
19 posts views Thread by wetherbean | last post: by
8 posts views Thread by fredda054 | last post: by
4 posts views Thread by William | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.