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
I am calculating month's days based on cmbYear & cmbMonth with the below code:
int days = DateTime.DaysInMonth(Convert.ToInt16(cmbYear.SelectedItem), Convert.ToDateTime("01-" + cmbMonth.SelectedItem + "-2011").Month);
What I am trying to achieve, using for loop, is to create a drop down list that includes all numbers starting from 1 to int days for cmbDay combo box. Below is my code which generates an error when I am trying to import the end point of the code i = days;. Error in Cannot implicitly convert type int to bool.
Any help will be appreciated.
for (int i = 1; i = days; i++)
{
}
the Loop should Looks like
for (int i=1; i <= days; i++)
or
for (int i=1; i < days; i++)
depends on what you Need.
You have to use <= instead of =
for (int i=1; i <= days; i++)
{
}
The second parameter is the condition. As long as it is true, the loop goes on
for (C# reference)
The for statement defines initializer, condition, and iterator
sections:
for (initializer; condition; iterator)
body
The condition section
The condition section, if present, must be a boolean expression. That
expression is evaluated before every loop iteration. If the condition
section is not present or the boolean expression evaluates to true,
the next loop iteration is executed; otherwise, the loop is exited.
You had i = days which is an assignment, and results in an int which is not a condition (bool).. What you probably intended was i == days which is still wrong.
What you needed was the following, which says "while i is less then days, loop the body"
for (int i=1; i < days; i++)
it's a simple syntactical error. you've misses an '<'
for(int i=1; i<=days;i++){..}
Related
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
So I currently have the problem that I am unable to increment my variables. Using 'i++' in my code seems to do nothing and using any other form of incrementing a value does nothing too.
static void Main(string[] args)
{
Console.WriteLine("Enter a number to see its times tables");
int number = Convert.ToInt16(Console.ReadLine());
int i;
for (i = 10; i == 10; ++i)
{
int output;
output = i * number;
Console.WriteLine(output);
}
Console.ReadKey();
}
All I am trying to do is create a basic loop but it is just stuck in an infinite loop, setting my variable 'i' to 10 causes the loop to work like its supposed to but it only executes once. The loop part becomes irrelevant.
Try
for (i = 0; i <= 10; i++)
Your condition means that the loop executed an incorrect number of times.
By incorrect number looking at your post and various attempts this means anything from zero to 'a positive' (but incorrect) number of times.
An if statement is a compound statement. In your case your if statement is composed of three sub-statements - an initialization, a test and an increment.
When the if statement is executed these three statements are executed in order.
If the test returns false the loop is not entered so when the condition is 'i==10' and the initialization is 'i=1' the loop is not executed at all. When the initialization is 'i=10' then the loop executes once.
Sub-statements 2 and 3 are executed after each execution of the content of the loop and as long as statement 2 resolves to true the loop will continue being executed.
In our example using a condition 'i <= 10' would cause the code within the loop to be executed 11 times. 'i<10' would cause it to be run ten times. Care has to be taken when choosing sub-statement 2 (i.e. the loop condition). You have to make sure that the initial value in statement 1 and the increment in statement 3 (or alternatively a change to the value made within the body of the loop itself) guarantee that the condition will eventually fail.
If not then code has to be added to the loop to ensure that the loop will exit.
This will loop indefinitely
for (int x=0; x==0; ) {
}
This will NOT loop indefinitely
for (int x=0; x==0; ) {
x++;
}
A test and break out of a loop could be done like shown in the following.
for (int x=0; x==0; ) {
if (x==0) {
break;
}
}
If you do not ensure that the loop is guaranteed to terminate it could potentially never exit.
By Using
(i = 0; i < 10; i++)
The range of i will be from 0 to 9
By using
(i = 0; i <= 10; i++)
The range of i will be from 0 to 10
By using
(i = 1; i <= 10; i++)
The range of i will be from 1 to 10
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Stuck for hours on this task. I really appreciate help, it is not shown in the literature how this is to be done. I can create the vector but after that I'm totally blocked.
Create a string vector with five elements. The user will then enter 5 names via a for loop. The program then writes these names via another for loop.
Please type out the code so I can understand. Without solving this first task I can't do the other ones.
Here's my futile attempt so far
int [] namn = new int [5];
for (int i = 0; i < 5; i++);
{
Console.Write("Ange fem namn");
string str = Console.ReadLine();
int names = Convert.ToInt32(str);
}
It seems that the main (and the most difficult error to find) is ; after for loop
for (int i = 0; i < 5; i++);
this loop does nothing five times.
//DONE: "string[]" - Create a STRING vector with five elements...
string [] namn = new string [5];
//DONE: namn.Length - no magic numbers (5)
for (int i = 0; i < namn.Length; i++) // !!! no ";" !!!
{
Console.Write("Ange fem namn");
//DONE: put into array, not to a local variable
namn[i] = Console.ReadLine();
}
// Print out the names
foreach(var item in namn)
Console.WriteLine(item);
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
I'm using below code to find out what is the length of a given string:
string foo = "Welcome to stack overflow";
int strlen;
for (int i=0; i<foo.length; strlen++){}
Console.WriteLine(strlen.ToString());
But the code never leaves the loop.
Well, that's weird.
I don't understand your logic, you already have the string length, why use a loop to apply it to another int?
But, well, who am I to judge?
The problem on your loop is that you're not increasing the value of i.
Do this instead:
for (int i=0; i<foo.Length; i++)
{
strlen++;
}
You could remove the loop and do this to your code:
string foo = "Welcome to stack overflow";
Console.WriteLine("String length: " + foo.Length.ToString());
Edit:
As mentioned in the comments:
the length property must have it's first letter uppercased, since C# is case sensitive. - Jon Skeet
You are never increasing "i" so the "i < foo.length" will always be true
You should iterate over i, not over strlen:
for (int i=0; i<foo.length; i++){}
You have one typo (foo.Length, not foo.length) and two errors:
Do not forget to assign 0 on local variabale declaration (int strlen = 0)
Do not forget to increment counter (i++)
Something like that:
string foo = "Welcome to stack overflow";
// error: assign 0 to strlen
int strlen = 0;
// Typo: foo.Length instead of foo.length
// error: do not forget to increment "i" as well as "strlen"
for (int i = 0; i < foo.Length; strlen++, i++) {}
// 25
Console.WriteLine(strlen.ToString());
Test:
// 25
Console.WriteLine(foo.Length);
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 7 years ago.
Improve this question
I made the algorithm below for bubblesort exercise at school.
//Bubble Sort (My code)
static void _BubbleSort(int[] a)
{
for (int i = 0; i < a.Length - 1; i++)
{
for (int j = 0; j < a.Length - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
swap(a, j);
}
}
}
}
But when I check the internet I see different algorithm below.
The algorithm I found in internet uses different "for" loop as follows. Mine is nested for loops but the code below is not nested.
public void BubbleSort(int[] b)
{
for (int pass = 1; pass < b.Length; pass++) // passes
for (int i = 0; i < b.Length - 1; i++) // one pass
if (b[i] > b[i + 1]) // one comparison
Swap(b, i); // one swap
}
What I want to ask is my code is an example of bad programming or not? or my brain is working different than you computer science guys? I am arts student by the way if you wonder.
What I want to ask is my code is an example of bad programming or not?
Your code almost is identical. There is a difference in that you are (correctly) using curly brackets to explicitly state code blocks, while the other example isn't. One problem is your loop bounds check.
#Sriram also pointed to the fact that your loop uses a.Length - 1 - i while the latter simply checks for b.Length - 1, which isn't actually necessary on your part and would cause the loop to prematurely end. Use the latter approach from the second example.
Other than the fact you shouldn't use _ at the beginning of your method (this is simply a naming convertion), your algorithm is identical.
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 8 years ago.
Improve this question
I am writing C# program in Visual Studio 2010, to build a windows application.
I have created a class named Store. And inside Store class, I have created a method named StoreLength(Store ob)
public int StoreLength(Store ob)
{
int i = 0, L = 0;
while (i < 100)
{
if (ob.a[i] != null)
L += 1;
}
return L;
Now from other class I have created an object of Store class. And using that object I am trying to use the StoreLength method.
private void buttonEqual_Click(object sender, EventArgs e)
{
int l = ob.StoreLength(ob);
DisplayUnit.Text = Convert.ToString(l);
}
Now when I am running the program, everythign is working properly but as soon as I am clicking on buttonEqual, the window freezes. I think there is some problem with in buttonEqual1_Click mothod, or in StoreLength method.
Please help.
It's because the while loop's condition is always satisfied.
Every time it evaluates i, it'll always be less than 100 because you set it to 0 and its value does not change.
The typical approach would be to increment i by 1 on every iteration:
while (i < 100)
{
i++;
if (ob.a[i] != null)
L += 1;
}
The reason this causes it to freeze is because your UI is 'waiting' (in a roundabout way) for this while loop to complete.
Also, consider using a for loop for scenarios like this. It suggests the declaration, initialisation, condition and increment all on the one line for readability and consistency sake:
for(int i = 0; i < 100; i++)
{
if (ob.a[i] != null)
L += 1;
}