Expressions vs. conditions Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
john_c
Posts: 474
Joined: 05 May 2017, 13:19

Expressions vs. conditions

06 May 2020, 18:12

I'm trying to understand the difference between expressions and conditions, in AHK.

Here are the articles and discussions I have found, but they aren't about AHK:
It seems that these languages share the same (across themselves) view about what conditions and what expressions are.
But this view is not the same as AHK view.

Expressions in AHK:

Code: Select all

                  ; Expressions:
var := "foo"      ; - the code within () only
if (var = "foo")  ; - the code within () only
  msgbox % 1 + 1  ; - the code after the % sign only
Expressions and conditions in JavaScript, Python, and Java. The code itself is AHK:

Code: Select all

                  ; Expressions and conditions:
var := "foo"      ; - expression, the code within () only
if (var = "foo")  ; - c o n d i t i o n, the code within () only
  msgbox % 1 + 1  ; - expression, the code after the % sign oly
Is my understanding correct?

That is to say, AHK doesn't use the word "condition". Conditions are actually called expressions. Right?
Last edited by john_c on 06 May 2020, 19:34, edited 1 time in total.
User avatar
Smile_
Posts: 300
Joined: 03 May 2020, 00:51

Re: Expressions vs. conditions

06 May 2020, 19:25

@john_c
I don't know what you mean exactly but expressions can be used to lead to conditions, but the expressions are still expressions and the conditions are still conditions.

Code: Select all

var := "foo"
Condition := (var = "foo") ; - Expression, the code within () only.
MsgBox % Condition ; - True return 1, False retuns 0.
if Condition  ; - c o n d i t i o n.
	msgbox % 1 + 1  ; - the code after the % sign only
User avatar
boiler
Posts: 6597
Joined: 21 Dec 2014, 02:44

Re: Expressions vs. conditions  Topic is solved

06 May 2020, 19:32

Basically, any expression can be a condition. Expressions have a result, and if you use the result to make executing code conditional on its value such as in an if or a while statement, it's a condition. Here's how using all of your example expressions are conditions when used in an if statement:

Code: Select all

if (var := "foo") ; var is the expression/condition which gets assigned "foo" and it evaluates to true because "foo" is a non-null string
if (var = "foo")  ; evaluates to true if var contains the string "foo" (not case sensitive)
if (1 + 1) ; evaluates to true because 2 is a non-zero number
john_c
Posts: 474
Joined: 05 May 2017, 13:19

Re: Expressions vs. conditions

06 May 2020, 19:51

This is really good explanation, Boiler, thanks.

One another question, closely related.

Is it correct to say that If, While, Until, and Switch are conditional statements, a subset of control flow statements?
User avatar
boiler
Posts: 6597
Joined: 21 Dec 2014, 02:44

Re: Expressions vs. conditions

06 May 2020, 19:55

Yes, that's a good way to put it.

One note, just to clarify for correctness, which you probably know. When I said if var contains the string "foo" (not case sensitive), by "contains" I means contains exactly that value, not a string that contains it within other characters, such as "foobar" which would evaluate as false.
john_c
Posts: 474
Joined: 05 May 2017, 13:19

Re: Expressions vs. conditions

06 May 2020, 20:05

Yes, of course :-) But thanks for clarification.
User avatar
Cuadrix
Posts: 224
Joined: 07 May 2017, 08:26

Re: Expressions vs. conditions

06 May 2020, 20:33

An expression is something that results in a value, for example a math operation (2 + 2) or a function that returns something.
A condition is a comparison of values in a conditional statement such as if, that returns either true or false.

See this example:

Code: Select all

message := "Hello world"

; if... is a conditional statement
if (message = "Hello world") { ; <-- This is a comparison. This comparison is a condition. The condition is either true or false depending on the comparison.
	; ...
}
Since the condition has to be either true or false, the condition can also be an expression.
Since a condition must be either true or false, ALL values are either "truthy" or "falsy" if they are in a conditional statement without a comparison operator. (E.g. "", "0" and 0 are falsy, thus evaluate to false in a conditional statement)
If a conditional statement does not contain a comparison operator, the conditional statement will only check whether the condition equals to true. The condition equals to true if the value in the condition is "truthy". (e.g. "Hello world", 1)
If the condition equals to true, then proceed to the block {.... } below...
----
john_c
Posts: 474
Joined: 05 May 2017, 13:19

Re: Expressions vs. conditions

07 May 2020, 04:28

Cuadrix, thanks.

Just in case, for people who may find it useful, here is another one closely related question (asked by me) and some answers to it:

https://www.reddit.com/r/AskProgramming/comments/gevxgp/the_difference_between_a_condition_and_a_boolean/
The expression becomes a conditional if it's being used as a condition (ie. in an if, while, etc.)
A "condition" is not really a syntax thing, but rather a context thing. What you call the condition is really just an expression in the context of an if statement

Return to “Ask For Help”

Who is online

Users browsing this forum: AlexIII, CluelessHotKeyUser, mikeyww, murataygun and 40 guests