Do while loop check for last iteration [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This may sound like an elementary question, but I want to check whether my thought process is correct.
I have a do while loop and I want to check when the loop has entered the last iteration so as to either set a value or not. I have come with this sort solution but doesn't feel and I was wondering whether there is a another way.
do
{
int iterationCheck ++;
if( iterationCheck! = counter)
// Do something
counter++;
} while (true)
Is this correct or is there a better way?

Sure you could do this to count the iteration
But put the 'int iterationCheck=0' variable declaration before the do/while scope.
You could go for a 'for' loop too ( which would be better )
However it all depends on the condition of your loop.
Because we can't infer the 'out' condition of your loop so we can't provide a accurate answer.

Related

Creating 100 variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So I created an array with 100 variables using Enumerable.Range. The data type is limited to Int32.
Problem
How can I create the same array with SByte?
Am I right in thinking I would need to use a loop to create and index the variables?
I have looked around online and most results touch on declaring counting variables for the loop but not using a loop to declare variables
Just cast them:
SByte[] array = Enumerable.Range(0, 100).Select(i => (SByte) i).ToArray();
note that SByte is not cls compliant, you might want to use short instead.

Understanding a for() statement in c# [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm going through a book at the moment (C# 8.0 and .NET Core 3.0) and I'm stuck with understanding / Googling the explanation for this question.
Does the following statement compile? for ( ; true; ) ;
I chucked the code in my editor and it does compile, but any code below is unreachable (which is fine). I'm hoping someone can explain what this statement is doing.
Cheers
In all variants of C a for is comprised of three parts: initialization, condition and update.
Initialization is executed once when the for is initialized, for example declaring or updating a variable: for(int a = 0;//...
Afther that comes the condition that is checked on each loop, for example: a < 10; //..
And finally we have the update, something that will be executed after each loop: a++)
It's not mandatory to set any of these, you can even do for( ; ; ) ;, that would create an infinite loop.
So, the code you have will create a for that will check if true is true creating an infinite loop.

How to check array for a value in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to help someone out and this isn't my area of expertise so thought maybe someone can help me help someone else.
I have a field called Master that contains an array. I also have a field called Original that contains a string. I want to check if the string in Original is in the array field called Field1 and then with an if statement do something if true / false
"Original":"1234",
"Master":[{"ID":1,"Field1":12345},
{"ID":2,"Field1":123456},
{"ID":3,"Field1":1234},
{"ID":4,"Field1":12344}]
The array can be different each time and have a different amount of records in it.
Can anyone help me?
if Original and Master would be properties of the same class and in a variable named instance then you could use Linq to do:
bool isPresent = instance.Master.Any(entry => entry.Field1 == instance.Original);
Obviously you would need to first serialize the json to an instance of this class.

What is MAX function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
var approve = UnitOfWork.Query.Lexis.ApprovedLexis2(DialogService.User.UserID, List.Where(x=>x.Check).Max(x=>x.TxnDate), _batch);
what does line of code returns? can someone explains that? TYIA
List.Where(x=>x.Check).Max(x=>x.TxnDate)
Presumably here List is a list/collection/some IEnumerable<T> for a type T that has (at least) a bool member named Check, and some other member TxnDate (presumably a DateTime of the transaction date).
The Where applies a predicate filter, i.e. it creates a filtered sequence of the items where Check is true. The Max finds the greatest (in terms of x>y, implemented by IComparable[<T>]) of the .TxnDate of each item in the filtered sequence.
So: the expression returns the greatest (last, timewise) transaction date of all the "checked" items in List. If there are no "checked" items, it will throw an exception (you can't ask for the largest of no values; or rather, you can ask, but you won't get a sensible answer).

why is Next() method called next? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why does Random.Next() have Next in it's name? I know what it does, but the name doesn't seem to correspond to it.
It returns the next number in the infinite sequence of numbers generated from your Random instance's seed.
In computer science jargon, a "generator" is a specific kind of function: one that returns a different result each time it is called. It is traditional to call this function something like next(), because they are often used to return the next piece of a sequence (perhaps infinite). RNGs are just a special case of generator function, returning the next value in a calculated sequence.

Categories