Question regarding if statement (Using If Statements in C++)

About using and implementing if statements, I was able to accomplish so, but in a slightly different manner and the program works as expected. The way I implemented if statement is in the following manner:

What I intend to ask is:

  1. Is this way correct (In terms of making a complex if statement)?
  2. Is it necessary to break down if statements in the form of nested if(s) instead of implementing complex conditions (In terms of programming habit)?

PS: I don’t have complain about code readability afaik what I did up there.
Any advice or improvement tips are appreciated :slight_smile:

The else/continue isn’t needed because the for loop will automatically continue after checking your if conditions.

1 Like

Thank you for pointing that out, I’ll refactor my code :slight_smile:

Hard to make a judgement call with such small examples.

You are not required to nest your ifs. I normally nest, but I find your code easier to read since the Bulls condition and the Cows conditions are fully separated. If you mind works this way you should not make changes to suit some arbitrary coding standard.
The case for nested if’ statements is

  1. two lines to retype if anything changes in the character comparison
  2. work is potentially being done twice.
    In this example game you won’t notice the performance difference, but as written the code will check the characters once when the character is a Bulls match, but twice when there is no match for Bulls. The nested version only does the comparison once.
    Bulls
    Guess[j] == MyHiddenWord[i] && i==j
    When there is no match this code compares the characters again.
    Guess[j] == MyHiddenWord[i] && i != j
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms