Arduino not able to perform if() function

void setup() {

}

void loop() {
while(Serial.available() == 0);
char val = Serial.read() ;//reads the signal
Serial.print(val);
if (val == ‘A’)
{
Serial.print(“hi”);
}
else if(val=! "L’’)
{
Serial.print(“bye”);
}
}

I am using this code and arduino is not able to print hi or bye, though its receiving message from bluetooth module hc-05, i am not sure what is the problems before it was doing it perfectly.
Help!!!

there is a screenshot of code and serial monitor in attachment

missing Serial.begin()

Arduino not able to perform if() function

Silly title.

Think: what is more likely - your inexperience at programming, or the fact that Arduino hasn't handled an "if" correctly?

Google "Occam's Razor"

Please remember to use code tags when posting code.

 else if(val=! "L'')

So many things wrong with that one line of code....

  1. val is a char. "L" is (maybe) a pointer to an array of char. i.e. - a memory address. So, ANY comparison between val and "L" is pretty much meaningless.

  2. Not clear what is really meant here but "L" above is actually a double quote, followed by the letter L, followed by two single quotes.

  3. The c/c++ language contains no "=!" operator.

So, what is this one line of code really doing?

  1. Fetch val, which presumably contains the ASCII character value for some printable character.
  2. Fetch the memory address of the char array containing "L"
  3. Convert that memory address to a boolean value. i.e. - almost certainly a boolean "true", or 1, since wherever that array is in memory, it will most certainly NOT be at address 0.
  4. Negate that boolean value (due to the "!" operator), resulting in a false or 0.
  5. Assign false/0 to val (due to the "=" operator)
  6. Since val now equates to false, the if test fails, and the code within the if clause does NOT execute.

Regards,
Ray L.

Juraj:
@RayLivingston, did you read my comment?

Your comment correctly identified the problem with quotes. But you still missed the problem with =!. The correction you suggested is still broken because of this.

Juraj:
char val
is not pointer to char array
and the error is missing Serial.begin()

Comments made by RayLivingston are spot on and correctly identify all problems in that if.

And he never said that val is a pointer to char array. Why?

Montmorency:
Your comment correctly identified the problem with quotes. But you still missed the problem with =!. The correction you suggested is still broken because of this.

Comments made by RayLivingston are spot on and correctly identify all problems in that if.

And he never said that val is a pointer to char array. Why?

sorry a read point 1 wrong

Silly title.

Once upon a time, we thought that these were also functions: for(), if(),..!

Sometimes later, the tutor told us that a keyword could not be used as a function name in C Language. for(), if(), and the like are called Control Structures.

GolamMostafa:
Once upon a time, we thought that these were also functions: for(), if(),..!

Who is that we you are talking about?

Whandall:
Who is that we you are talking about?

Were you there in that ‘we’ community 28 years ago? Certainly not – those were very dull kids!

GolamMostafa:
Were you there in that 'we' community 28 years ago? Certainly not -- those were very dull kids!

You still did not answer my question, what was that group of "very dull kids" you were a part of?

GolamMostafa:
Sometimes later, the tutor told us that a keyword could not be used as a function name in C Language. for(), if(), and the like are called Control Structures.

Well, pedantically speaking, they are called statements in the official parlance of C and C++.

if is an example of selection statement. for is an example of iteration statement.

Whandall:
You still did not answer my question, what was that group of "very dull kids" you were a part of?

The Group members so far I can trace are -- me and the poster of this thread.

Could you please rephrase statements like

Once upon a time, we thought that these were also functions: for(), if(),…!

in the future to omit the we?

Whandall:
Could you please rephrase statements like

in the future to omit the we?

Not obliged; but, it could be done with great care as readers might ask about the missing subject of the sentence (Once upon a time, we thought that these were also functions: for(), if(),..!).

Montmorency:
Well, pedantically speaking, they are called statements in the official parlance of C and C++.

if is an example of selection statement. for is an example of iteration statement.

1. This is the visual representation of if(). Is it selecting one of two alternatives or controlling the flow path of execution?
if.png

2. This is the visual representation of for(). Is it not controlling the flow path of execution?
for.png

if.png

for.png

GolamMostafa:
1. This is the visual representation of if(). Is it selecting one of two alternatives or controlling the flow path of execution?

2. This is the visual representation of for(). Is it not controlling the flow path of execution?

I'm not sure I understand why you are asking these questions.

My previous comment simply refers to the well-established official terminology of C and C++

http://eel.is/c++draft/stmt.select
http://eel.is/c++draft/stmt.iter

Hello Montmorency

Montmorency:
I'm not sure I understand why you are asking these questions.

My previous comment simply refers to the well-established official terminology of C and C++

[stmt.select]
[stmt.iter]

You should read better your quoted documents.

All the statements are not control structure.

Regards,
bidouilleelec

Not all the statements are not control structure.

I can’t cope with double negatives. Do you mean that some statements are not control structures ?

UKHeliBob:
I can’t cope with double negatives. Do you mean that some statements are not control structures ?

Yes

bidouilleelec:
You should read better your quoted documents.

And what exactly did I miss?

bidouilleelec:
All the statements are not control structure.

Er... I don't know what you mean exactly. However:

  • You seem to be refuting someone's point. But nobody here ever claimed that all statements are "control structures". So, who is that directed at?

  • There's no such term or concept as "control structure" defined standard C++ terminology at all. There's no need for it. There's a passing informative reference to "control structures" in the intro, but that's it.