How is the exit condition "x=10" in this loop " for (x=0;x=10;x++)" always true? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
i have typed this code and ran it , and it runs infinitely , i couldn't understand why since it should have stopped once x gets to 10
i expected it to stop at 10 since it started from 0 and incremented by 1 until it gets to 10

for loops do not have an exit condition. They have a continue condition. When the middle condition is tested, the loop continues if it is true. x = 10 is an assignment expression. It assigns 10 to x, and the value of the assignment expression is the new value of x, so it is always 10 (or, if x is a _Bool, it is 1), which serves as “true” for the condition. You may have wanted x == 10, which is a comparison expression. But that is true only when x is 10, so the loop would never execute even the first iteration. You need to a condition to continue the loop, not to exit it, so you want x < 10.

Related

C# infinite Loops [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
Can someone help me understand why the loop below is infinite and how to make it finite?
int index = 1;
while (index != 10)
{
Console.WriteLine("Hello");
index += 2;
}
I tried putting it in Visual Studio but it did not run. I need to understand the logic. I am struggling with this course
It starts with index at 1 and increases by 2 (index += 2)
1
1 + 2
3
------:
-----:
3 + 2
5
------:
-----:
5 + 2
7
------:
-----:
7 + 2
9
------:
-----:
9 + 2
11
------:
-----:
11 + 2
13
Since you only stop at 10, it runs infinite.
(If you stop at >= 10, it will stop at 11)
Try using a debugger.
Visual studio will give you an error 'While' does not exist in the current context. Intellisense is your friend when learning a new language. Your error here is that While should be lower case while.
int index = 1;
while(index != 10)
{
Console.WriteLine("Hello");
index += 2;
}
Why is the loop infinite?
Set a breakpoint on the debugger to step through each line of the while loop. Hover over index to see it's new value upon each iteration. You'll see that your design only increments by odd numbers thus always satisfying your while condition index != 10.
There's many ways to make the loop finite. Start index at 0, or increment by one, or adjust your while condition. But I think the main thing from this question is that you need to brush up on c# syntax and VS intellesense\debugging. These tools will help you eleminate questions about the fundamentals of c#.

Using the C# indexOf() method and it does not work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am using IndexOf() and it does not work.
The code is:
if (sqlex.Message.IndexOf("Critical") > 0)
and Message does contain the word - Critical (see image), but does not equate to true. It takes the false path.
I also put in test code:
int index = sqlex.Message.IndexOf("Critical");
and index is = 0.
Why?
The first position in the string is 0, not 1. So if your string is
Critical Error - do not continue. Contact IT...
And you search for Critical, then 0 is the expected result.
If the string is not found, the result will be -1 ... unless the string is also empty, so make sure to check that, too.
The method string.IndexOf returns -1 if the character or string is not found. 0 means it is found. Change your code to if (sqlex.Message.IndexOf("Critical") >= 0)

What happened on my `Enumerable.Range(,)`? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
What happened on my Enumerable.Range(,)?
It produced a number 5030 which is out of range.
sellableItems is a small List<T>, I need to randomly pick 9 items from it.
Here is the code:
var targetIndexes = Enumerable.Range(i + 1, sellableItems.Count-1)
.OrderBy(x => random.Next())
.Take(9)
.ToArray();
for (var j = 0; j < targetIndexes.Length; j++)
{
...
}
The Enumerable.Range method takes in two parameters - a starting value and a count.
When you do: Enumerable.Range(i + 1, sellableItems.Count - 1), you are starting with the value 37 (i + 1) and a count of 4999 (sellableItems.Count - 1).
Since each iteration in the call to Range increments the previous value by one (except the first iteration, which uses the starting value), the range will be from 37 to 5035.

Fastest Algorithm for Shortest Paths with negative values? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I'm currently using Bellman Ford algorithm to find the shortest paths with negative value. Is there any faster algorithm that would outperform Bellman Ford for finding shortest paths with negative values?
A simple improvement is to only check for "active" nodes instead of iterating on all of them as the naive implementation does.
The reason is that if a node didn't lead to improvements on any of its neighbors and didn't change value in last iteration there is no need to redo the computation again (it will still produce no improvements).
Pseudocode (Python, actually):
A = set([seed])
steps = 0
while len(A) > 0 and steps < number_of_nodes:
steps += 1
NA = set()
for node in A:
for nh in neighbours(node):
x = solution[node] + weight(node, nh)
if x < solution[nh]:
# We found an improvement...
solution[nh] = x
pred[nh] = node
NA.add(nh)
A = NA
A is the "active" node set, where an improvement was found on last step and NA is the "next-active" node set that will need to be checked for improvements on next iteration.
Initially the solution is set to +Infinity for all nodes except the seed where the solution is 0. Initially only the seed is in the "active" set.
Note that in case of negative-sum loops reachable from the seed the problem has no "minimum path" because you can get the total as low as you want by simply looping; this is the reason for the limit on the "steps" value.
If when coming out of the loop A is not empty then there is no solution to the minimum cost problem (there is a negative-sum loop and you can lower the cost by simply looping).

How to get the first value of a variable in C# without using array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Main()
{
int X=10;
X=20;
X=30;
Console.WriteLine ("X :"+X);//will print 30.
}
How to get the first value of X without using array.
I want the result as
X=10
Imagine you have a plate X and you put potatoes on it 10
int X=10;
then you remove the potatoes and put salad 20 on it
X=20;
then you remove the salad and put a steak 30 on it
X=30;
and now you ask yourself how do I serve potatoes to your guest. You guest will receive the steak because it is the last value that you put on the plate.
Console.WriteLine ("X :"+X);//will print 30.
I would suggest to use a List. It would be the equivalent of a plate with a memory.
List<int> x_es = new List<int>();
x_es.Add(10);
x_es.Add(20);
x_es.Add(30);
Now you can serve whatever you have placed already before on your plate
x_es[0];
If you really want to avoid array or List you need further plates/variables to store the content temporarily:
int X=10;
int mem_1 = X;
X=20;
int mem_2 = X;
X=30;
Console.WriteLine ("X :"+mem_1);//will print 10.

Categories

Resources