Whale Talk - Question about 'if' placement

I’m confused about something in the Whale Talk project in the C++ course.

Can somebody please explain to me why this works and outputs the correct string uueeieeauuee:

#include <iostream>
#include <string>
#include <vector>

int main() {
  std::string input = "turpentine and turtles";
  std::vector<char> vowel = {'a', 'e', 'i', 'o', 'u'};
  std::vector<char> result;

  for (int i = 0; i < input.size(); i++) {
    for (int j = 0; j < vowel.size(); j++) {
      if (input[i] == vowel[j]) {
        result.push_back(input[i]);
        if (input[i] == 'e' || input[i] == 'u') {
          result.push_back(input[i]);
        }
      }
    }
  }

  for (int i = 0; i < result.size(); i++) {
    std::cout << result[i];
  }
  std::cout << "\n";
}

but when the second if inside the for isn’t inside the first if it outputs uuuuuueeeeeeieeeeeeauuuuuueeeeee

#include <iostream>
#include <string>
#include <vector>

int main() {
  std::string input = "turpentine and turtles";
  std::vector<char> vowel = {'a', 'e', 'i', 'o', 'u'};
  std::vector<char> result;

  for (int i = 0; i < input.size(); i++) {
    for (int j = 0; j < vowel.size(); j++) {
      if (input[i] == vowel[j]) {
        result.push_back(input[i]);
      }
      if (input[i] == 'e' || input[i] == 'u') {
        result.push_back(input[i]);
      }
    }
  }

  for (int i = 0; i < result.size(); i++) {
    std::cout << result[i];
  }
  std::cout << "\n";
}

To me it should output exactly the same thing with both codes. Or am I missing something?

  std::string input = "turpentine and turtles";
  std::vector<char> vowel = {'a', 'e', 'i', 'o', 'u'};
  for (int i = 0; i < input.size(); i++) {
    for (int j = 0; j < vowel.size(); j++) {
      if (input[i] == vowel[j]) {
        result.push_back(input[i]);
      }
      if (input[i] == 'e' || input[i] == 'u') {
        result.push_back(input[i]);
      }
    }

First iteration of your for loop:
input[0] is "t" Since "t" isn’t in the vowel{} vector, and isn’t an ‘e’ or a ‘u’ neither or your if code blocks execute.

Second iteration:
input[1] is "u" Now as your code iterates through the vowel{} vector:
vowel[0] is "a" so… is "u" == "a" ? No.
is "u" == "e" or "u"? Yes, so result is now {u}

Second iteration of for (int j = 0; j < vowel.size(); j++)
vowel[1] is "e" and input[1] is still "u" so… is "u" == "e"? No.
is "u" == "e" or "u"? Yes, so result is now {uu}

Third iteration of for (int j = 0; j < vowel.size(); j++)
vowel[2] is "i" and input[1] is still "u" so… is "u" == "i"? No.
is "u" == "e" or "u"? Yes, so result is now {uuu}

Fourth iteration of for (int j = 0; j < vowel.size(); j++)
vowel[3] is "o" and input[1] is still "u" so… is "u" == "o"? No.
is "u" == "e" or "u"? Yes, so result is now {uuuu}

Fifth iteration of for (int j = 0; j < vowel.size(); j++)
vowel[4] is "u" and input[1] is still "u" …so is "u" == "u"? Yes, so result is now {uuuuu}
is "u" == "e" or "u"? Yes, so result is now {uuuuuu}

See where this is going? The nested if in the first example makes a big difference. It is only checked when the input[i] matches vowel[j] instead of all 5 times through the vowel{} vector.
Hopefully that all made some sense.

1 Like

Thanks a lot @midlindner for your detailed response. That cleared things up for me.

1 Like