Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
c++ switch - not evaluating and showing "Condition is always true"

I was experimenting whether I can use "logical or" inside case clause for a switch statement. I tried this way for the first case and the system skipped it all together when I gave input as "A" or "a" in both the cases.

# include <iostream>
using namespace std;

int main () {
  /* prg. checks if the input is an vowel */

  char ch;
  cout << "please input a word to check if it is vowel or not" << endl;
  cin >> ch;

  switch (ch) {
      case ('A'|| 'a') :
          cout << "it is vowel" << endl;
          break;

      case 'E':
          cout << "it is vowel" << endl;
          break;

      case 'I':
          cout << "it is vowel" << endl;
          break;

      case 'O':
          cout << "it is vowel" << endl;
          break;

      case 'U':
          cout << "it is vowel" << endl;
          break;

      default:
          cout << "it is not a vowel" << endl;
          break;


  }

is there a proper way to use or inside the case clause ?

Thank you for the help.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The case ('A'|| 'a') should be written as:

switch (ch) {
case 'A':
case 'a':
    cout << "it is vowel" << endl;
    break;
// ...

If it matches on 'A' it will fall through to the next case (unless there's a break in between).

In this case, you may want to combine all vowles:

switch (ch) {
case 'A':
case 'a':
case 'E':
case 'e':
//... all of them ...
    std::cout << "it is vowel\n";
    break;
// ...

Note that some compilers will issue a warning for fall through cases with statements in them (which is not the case above). Since C++17, if you use fall through cases, you can use the fallthrough attribute to silence such warnings where you actually want a fall through.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda