Question:

Help on if statements ?

by  |  earlier

0 LIKES UnLike

Which of the following three if statements are equivalent?

(i) if (aa == bb)

{ if (xx == yy) flag = true; }

else flag = false;

(ii) if (aa == bb)

{ if (xx == yy) flag = true;

else flag = false; }

(iii) if (aa == bb) if (xx == yy) flag = true; else { flag = false; }

Here is my analysis:

i. aa equal bb, xx equal yy (flag = true),

aa not equal bb (flag = false),

aa equal bb, xx not equal yy (flag = unknown)

ii. aa equal bb, xx equal yy (flag is true)

aa not equal bb (flag = unknown)

aa equal bb, xx not equal yy (falg = false)

iii. aa equal bb, xx equal yy (flag is true)

aa not equal bb (flag = unknown)

aa equal bb, xx not equal yy (falg = false)

Therefore ii & iii shd be the ans. Am i rite?

Tks a lot!!!

 Tags:

   Report

2 ANSWERS


  1. (i) if (aa == bb)

    { if (xx == yy) flag = true; }

    else flag = false;

    if aa does not equal bb, then flag equals false.

    (ii) if (aa == bb)

    { if (xx == yy) flag = true;

    else flag = false; }

    if aa equals bb and xx does not equal yy, then flag equals false

    (iii) if (aa == bb) if (xx == yy) flag = true; else { flag = false; }

    if aa equals bb and xx does not equal yy then flag equals false.

    ii and iii is the correct answer.


  2. Actually, to properly answer this question, it would be necessary to know what the flag was initialized to. Without knowing that explicitly, it is impossible to give an answer.

    If we assume that it is initialzed to "true", then I would say that i and ii are equivalient, and iii is just horrible programming - I don't even know that a compiler would accept that syntax.

    That's my 2 cents : )

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.