Incrementor logic
Quoting Java Language Specification, 15.7 Evaluation Order:
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable's value for use in the implied binary operation.
So, essentially, i += ++i
will remember the old value of i
on the left side, before evaluating the right side.
Remember, evaluation order of operands and precedence of operators are two different things.
Showing evaluation order, step by step, with saved value in {braces}:
int i = 0;
i = i += (++i + (i += 2 + --i) - ++i); // i = 0
i{0} = i += (++i + (i += 2 + --i) - ++i); // i = 0
i{0} = i{0} += (++i + (i += 2 + --i) - ++i); // i = 0
i{0} = i{0} += (1 + (i += 2 + --i) - ++i); // i = 1
i{0} = i{0} += (1 + (i{1} += 2 + --i) - ++i); // i = 1
i{0} = i{0} += (1 + (i{1} += 2 + 0 ) - ++i); // i = 0
i{0} = i{0} += (1 + (i{1} += 2 ) - ++i); // i = 0
i{0} = i{0} += (1 + 3 - ++i); // i = 3
i{0} = i{0} += (4 - ++i); // i = 3
i{0} = i{0} += (4 - 4 ); // i = 4
i{0} = i{0} += 0 ; // i = 4
i{0} = 0 ; // i = 0
0 ; // i = 0
Followup to edits to question
If we name the initial value I
and the constant N
:
int i = I;
i = i += (++i + (i += N + --i) - ++i);
Then we can see that the values are:
i{I} = i{I} += ((I+1) + (i{I+1} += N + I) - ((I+1+N+I)+1));
i{I} = i{I} += (I + 1 + (I + 1 + N + I) - (I + 1 + N + I + 1));
i{I} = i{I} += (I + 1 + I + 1 + N + I - I - 1 - N - I - 1);
i{I} = i{I} += I;
i{I} = I + I;
i = 2 * I;
Increment and Decrement Operators Work Logic
Here is the logic for code fragment B, showing the updated value of var1
in parentheses.
int var1 = 10;
var1 = var1++ + var1 + var1-- - var1-- + ++var1;
var1++ = 10 (var1 = 11)
var1 = 10 + var1 + var1-- - var1-- + ++var1;
var1 = 11 (var1 = 11)
var1 = 10 + 11 + var1-- - var1-- + ++var1;
var1 = 21 + var1-- - var1-- + ++var1;
var1-- = 11 (var1 = 10)
var1 = 21 + 11 - var1-- + ++var1;
var1 = 32 - var1-- + ++var1;
var1-- = 10 (var1 = 9)
var1 = 32 - 10 + ++var1;
var1 = 22 + ++var1;
++var1 = 10 (var1 = 10)
var1 = 22 + 10 ;
var1 = 32 ;
32 (var1 = 32);
What is the logic here for post increment?
(*ptr)++; // increment the value (to 6)
// other stuff
printf( .... // print the value
The (post)-increment occurs substantially before the value is fetched to be printed.
Decrement / increment logic in counter app with double digits
<Counter
id="count1"
value={values.count1 < 10 ? `0${values.count1}` : values.count1}
handleValue={handleValue}
/>
Here the type of value
prop in Counter
component isn't number
. If you want to do some caculation, you'd better keep the type as number something like below
<Counter
id="count1"
value={values.count1}
handleValue={handleValue}
/>
So here what you need is to show the number starting with "0" if it is less than 10. In this case you can just make some trick on the visual side of the component.
Actually, I am not sure where you are showing value (maybe in Counter
component?), but that's where you should use string iteral.
Logical AND OR and increment precedence
Both ||
and &&
force left-to-right evaluation - the LHS is evaluated first and all side effects applied, then based on the result the RHS is evaluated.
Both operators short-circuit:
- for
a || b
, ifa
is non-zero, then the result of the expression is1
regardless of the value ofb
, sob
is not evaluated; - for
a && b
, ifa
is zero, then the result of the expression is0
regardless of the value ofb
, sob
is not evaluated.
&&
has higher precedence than ||
, so a || b && c
is parsed as a || (b && c)
.
Putting all that together, (a||c--)&&(c&&b--)
is evaluated as follows:
a || c--
is evaluated as follows:a
is evaluated - its result is1
, soc--
is not evaluated; because of thisc
's value is not changed, and- the result of the expression is
1
c && b--
is evaluated as follows:c
is evaluated - its result is1
, sob--
is evaluated - its result is3
; as a side effectb
is decremented, and- the result of the expression is
1
- both
a || c--
andc && b--
evaluate to1
The values of a
and c
are unchanged (1
and 1
, respectively), while b
has been decremented and its value is now 2
.
increment logic
There are some parts that you will have to complete/fix since I don't know how you are connecting to your DB, but this will give you a general idea
protected void Button1_Click(object sender, EventArgs e)
{
//If today is 31 March 2019, filename = 20190301. If second person
//submit on same day, filename = 20190302
//But if date is 1 April 2019, filename = 20190401
DateTime processDate = DateTime.Now; // get current date
string caseNo = "";
string casePrefix = string.Format("{0}{1:D2}", processDate.Year, processDate.Month); // to help you search previous cases and create the new caseNo
Int16 caseInMonthNumber = 0; // Int value of the case so you can increment
string sqlResult = ""; // result from your query
string sqlString = string.Format("SELECT caseNo from TABLE WHERE caseNo LIKE '{0}%' ORDER BY caseNo DESC LIMIT 1", casePrefix); // syntax might change depending on your DB
// Function to call your database or whatever you are using to keep count of cases, this will get you the highest value so the highest case... return the caeNo value
// sqlResult = functionQueryToDB(sqlString); // or just send the prefix and create the 'sqlString' string in the function
//process the result from the database if there are no results, you don't need to do anything extra, start with caseInMonthNumber = 1,
//but if you get a result, you need to read the last part of the string and increment by one... Depending on the logic for your program you might want to add
// extra validations for the string
if ( sqlResult != "" ) {
if ( !Int16.TryParse(sqlResult.Substring(sqlResult.Length - 2) , out caseInMonthNumber) ) {
//error handling, the last two digits are not a valid number
return;
}
}
caseNo = string.Format("{0}{1:D2}", casePrefix, ++caseInMonthNumber);
// Do what you need with the new caseNo
}
Some things you need to consider:
- What happens if you get more than 99 cases in a month
- What happens if your app gets concurrent calls
- What happens if you have users in different time zones, might want to use DateTime.UtcNow instead
Hope this helps.
Regards
EDIT: your function is void, I was returning and empty string in the error handling.
Logical Operators and increment operators
||
or logical OR operator has a short-circuit property. It only evaluated the RHS is the LHS is FALSY.
In your case, the evaluation of ++x
produces a value of -2
, which is not FALSY (0). Hence, the RHS is never evaluated.
To break it down:
m = ++i || ++j && ++k;
>> m = (++i) || (++j && ++k);
>> m = (-2) || (++j && ++k);
>> m = 1 // -2 != 0
So, only the value of m
and i
are changed, remaining variables will retain their values (as they are not evaluated).
That said, the result of the logical OR operator is either 0
or 1
, an integer value. The result is stored in m
, in your case.
Related Topics
How to Send List of Objects to View and Back to Post Method in Controller
Xml Element with Attribute and Content Using Jaxb
Count Occurrences of Words in Arraylist
How to Convert Timestamp to Date in Java
Java Date Cut Off Time Information
Tomcat in Idea. War Exploded: Server Is Not Connected. Deploy Is Not Available
Generating All Possible Permutations of a List Recursively
What Is the Jvm Thread Scheduling Algorithm
How to Replace Groups in Java Regex
Iteratively Compute the Cartesian Product of an Arbitrary Number of Sets
Equivalent Function to C's "_Getch()" in Java
Calendar.Month Gives Wrong Output
Getting Enum Associated with Int Value
Java Hashmap Performance Optimization/Alternative
In Java Critical Sections, What Should I Synchronize On
Foreach VS Foreachordered in Java 8 Stream
Can a Spring Boot @Restcontroller Be Enabled/Disabled Using Properties