What is wrong with my if-statement? - if-statement

I am now trying to explore pascal. And I ran into some compiler errors. I wrote a if else if statement like this:
if ((input = 'y') or (input = 'Y')) then
begin
writeln ('blah blah');
end;
else if ((input = 'n') or (input = 'N')) then
begin
writeln ('blah');
end;
else
begin
writeln ('Input invalid!');
end;
And it gives me an error at the first else:
";" expected but "ELSE" found
I looked for a lot of tutorials about if statements and they just do it like me:
if(boolean_expression 1)then
S1 (* Executes when the boolean expression 1 is true *)
else if( boolean_expression 2) then
S2 (* Executes when the boolean expression 2 is true *)
else if( boolean_expression 3) then
S3 (* Executes when the boolean expression 3 is true *)
else
S4; ( * executes when the none of the above condition is true *)
I tried to delete the begin and end but the same error occured. Is this a compiler bug?
P.S. I am doing this in a case statement. But I don't think it matters.

; is not allowed before else in the majority of cases.
if ((input = 'y') or (input = 'Y')) then
begin
writeln ('blah blah');
end
else if ((input = 'n') or (input = 'N')) then
begin
writeln ('blah');
end
else
begin
writeln ('Input invalid!');
end;
will compile.
But... Prefer using begin ... end brackets to avoid misunderstanding of code in complicated if then else statements.
something like this will be better:
if ((input = 'y') or (input = 'Y')) then
begin
writeln('blah blah');
end
else
begin
if ((input = 'n') or (input = 'N')) then
begin
writeln('blah');
end
else
begin
writeln('Input invalid!');
end;
end;
The second sample is much easier to read and understand, isn't it?
The code does not work when you remove begin and end because there is a semicolon before else. This will compile without errors:
if ((input = 'y') or (input = 'Y')) then
writeln('blah blah')
else
begin
end;
Appended on comment of #lurker
Please, see the following example without begin ... end brackets.
if expr1 then
DoSmth1
else if expr2 then
if expr3 then
DoSmth2
else
DoSmth3;//Under what conditions is it called?
It is not clearly seen here, if DoSmth3 is called on not (expr2) or (expr2) and (not (expr3)). Though we can predict the compiler behaviour in this sample, the more complicated code without begin ... end becomes subect to mistakes and is difficult to read. See the following code:
//behaviour 1
if expr1 then
DoSmth
else if expr2 then
begin
if expr3 then
DoSmth
end
else
DoSmth;
//behaviour 2
if expr1 then
DoSmth
else if expr2 then
begin
if expr3 then
DoSmth
else
DoSmth;
end;
Now the code behavior is obvious.

Related

Understand fmt formatter parse function

I'm trying to under the parse function for creating a formatter for a custom type in fmt. In their documentation (https://fmt.dev/dev/api.html) there is this line that has some sort of loop construct I haven't seen before:
auto it = ctx.begin(), end = ctx.end();
if (it != end && (*it == 'f' || *it == 'e')) presentation = *it++;
It's obviously a loop using iterators, presumably something new in C++17. What is it? Full example here: https://godbolt.org/z/fEGvaj
The formatter::parse function takes a parse context ctx and checks if the range [ctx.begin(), ctx.end()) contains format specifiers f or e in this example.
if (it != end && (*it == 'f' || *it == 'e')) presentation = *it++;
^ ^ ^
check if the check if the first
range is empty character is 'f' or 'e'
There is nothing particularly novel here, this code is compatible with C++98.

Condition "else" is not allowed? Why?

Why is the else statement is not allowed to have a then or other conditions?
Is it because it is the final condition within the else-if conditions it represents?
I am struggling to understand this concept since I'm a beginner who just learned about variables.
I'm asking this because I received an error with my else statement in the code:
message = 0
condition = 30
if condition <=10
message = “try harder”
elseif
condition <=20 then
message = "Almost learning"
else
condition = <=30 **—This is the line where I get the error message**
message = "Now you’re getting it"
end
print(message)
Would appreciate someone breaking down in laymen terms, why else is not allowed to have < or > or then or other conditions.
else condition = <= 30
(which is the way your code was originally formatted) would be a very unusual feature in a language.
The whole point of else on its own is to execute if none of the other conditions were true. So a condition on the else is absolutely useless.
The Programming in Lua book if statement shows the normal usage:
if op == "+" then
r = a + b
elseif op == "-" then
r = a - b
elseif op == "*" then
r = a*b
elseif op == "/" then
r = a/b
else
error("invalid operation")
end
However, your actual code (when formatted correctly) ends up looking like:
else
condition = <=30
which is correct in terms of the else but unfortunately makes the next line a statement. And this statement is very much incorrect syntax.
Now it may be that you meant to assign 30 to condition but, based on your other lines (that sort of match this as a condition), I suspect not. So it's probably best just to remove that line totally.

Use of Logical Operator in Loop Condition

In the below given code, why the || logical doesn't work, instead the loop terminates specifically when && is used ?
int main() {
char select {};
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while (select != 'q' && select != 'Q'); // <--- why || (or) doesn't work here ??
return 0;
}
This loop will go on while select is not q and it's not Q:
while (select != 'q' && select != 'Q');
This loop will go on while select is not q or it's not Q.
while (select != 'q' || select != 'Q');
Since one of them must be true, it'll go on forever.
Examples:
The user inputs q
select != 'q' evaluates to false
select != 'Q' evaluates to true
false || true evaluates to true
The user inputs Q
select != 'q' evaluates to true
select != 'Q' evaluates to false
true || false evaluates to true
You want to terminate the loop when select is equal either to 'q' or 'Q'.
The opposite condition can be written like
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while ( not ( select == 'q' || select == 'Q' ) );
If to open the parentheses then you will get
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while ( not( select == 'q' ) && not ( select == 'Q' ) );
that in turn is equivalent to
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while ( select != 'q' && select != 'Q' );
Consider the following diagrams:
The full ellipse are all characters. The white dots is q and Q respectively. The black filled area depicts characters that will make the expression true. First line is select != 'q' && select != 'Q', second line is select != 'q' || select != 'Q'.
&& means both conditions must be true. The resulting black area is the overlap of the two areas on the left.
|| means either of the conditions must be true. The resulting black area is the sum of the two areas on the left.

How to code a test a statement with three or more components

Excuse me for the use of the term "component", there probably is a better term to use in such context.
But moving on to my question, I want to use the else statement to execute a statement block based on the truth of the test statement. Here is the test statement I tried using:
else:
if ((reply != a) && (reply != c) && (reply =! e)):
I'm getting a syntax error, and the carrot is pointing at the first set of ampersands. I'm assuming now that I might be improperly using '&&'.
With this statement, my goal is to execute the statement block only if the test statement is true, meaning further, that 'reply' must not be equal to a, c, or e.
I know that I can use nested if's under the else statement, but I'm hoping StackExchange knows a better way. Thank you.
parenthesis check :
if (((reply != a) && (reply != c)) && (reply =! e))

Vector comparison with string

I have a string vector of user-input data containing strings. Now I need to make sure program won't execute if strings are different than specified few. Vector contains 4 fields and every has different condition:
vector[0] can only be "1" or "0"
vector[1] can only be "red" or "green
vector[2] can only be "1", "2" or "3"
vector[3] can only be "1" or "0"
I tried writing if for every condition:
if(tokens[0]!="1" || tokens[0]!="0"){
decy = "error";
}
else if(tokens[1]!="red" || tokens[1]!="green"){
decy = "error";
}
else if(tokens[2]!="1" || tokens[2]!="2" || tokens[2]!="3"){
decy = "error";
}
else if(tokens[3]!="1" || tokens[3]!="0"){
decy = "error";
}
else{
switch(){} //working code
}
return decy;
It always enters first if and returns error. I tried with if instead of else if but it doesn't work either. I checked vector[i] contents and it returns correct strings. No " " at the end of it etc. Removing else and releasing switch just makes program check first condition and ignore rest of it.
I'm probably doing something terribly wrong, but I can't find an answer on internet so I decided to ask here.
This line:
if(tokens[0]!="1" || tokens[0]!="0")
should be:
if(tokens[0]!="1" && tokens[0]!="0")
^^
The same goes for the rest of the if statements as well.
The conditions are invalid.
Any distinct value can satisfy your conditions.
You should use && instead of ||.
For example:
if (tokens[0] != "1" || tokens[0] != "0") {
Consider this line. If tokens[0] is "1", which is valid input, it will not satisfy the first condition, but it will satisfy the second. You only want to throw an error when the value is neither of the valid possible inputs.
This means that your condition should be:
if (tokens[0] != "1" && tokens[0] != "0") {
Same goes for all the others.
You should turn those || into &&. If the input can only be X or Y, this means that it is illegal when it is not X and not Y:
if (tokens[0] != "1" && tokens [0] !="0")
// ^^
The first if:
if(tokens[0]!="1" || tokens[0]!="0")
ALWAYS evaluates to true.