Why does for-loop show nothing in cmd [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 6 years ago.
Improve this question
I have problem with for loop in C#.
I have the following code, in main method:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for(int i=0;i>20;i++)
{
if(i%2==0)
{
Console.WriteLine("{0},", i);
}
}
Console.ReadLine();
}
}
}
The for loop does not execute. Why?

Change condition, > to <:
...
for (int i = 0; i < 20; i++) // i < 20, not i > 20
...
In your original code you assign int i = 0 then check i > 20 get false and do not enter the loop at all

The following statement in your code:
for (int i = 0; i > 20; i++)
First assigns 0 to i then evaluates the condition i > 20 which is wrong, so the for block can not be executed.

Other people have pointed out the fact that the test should actually be "i < 20" rather than "i > 20) but just to make one other point: if all you're trying to do is write out all the even numbers, you can actually increment "i" by 2 every time - that way you never have to test to see if it's even (just write "i += 2" instead of "i++").

Related

Why is the for-loop choosing the wrong IF statement path? [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
So I am doing an online coding challenge and have come across this issue that has me stumped:
This is my code:
static void Main(String[] args)
{
int noOfRows = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < noOfRows; i++)
{
string odds = "";
string evens = "";
//get the input word from console
string word = Console.ReadLine();
for (int j = 0; j < word.Length; j++)
{
//if the string's current char is even-indexed...
if (word[j] % 2 == 0)
{
evens += word[j];
}
//if the string's current char is odd-indexed...
else if (word[j] % 2 != 0)
{
odds += word[j];
}
}
//print a line with the evens + odds
Console.WriteLine(evens + " " + odds);
}
}
Essentially, the question wants me to get the string from the console line and print the even-indexed characters (starting from index=0) on the left, followed by a space, and then the odd-indexed characters.
So when I try the word 'Hacker', I should see the line printed as "Hce akr". When I debugged it, I saw the code successfully put the letter 'H' on the left (because it is index=0, thus even), and put the letter 'a' on the right (odd index). But then when it got to the letter 'c', instead of going through the first IF path (even index), it skips it and goes to the odd index path, and places it on the right hand side?
The funny thing is when I try the word 'Rank' it works fine and prints the correct statement: "Ra nk", yet other words do not.
Its just bizarre that I'm getting different results.
What am I missing?
word[j] is a character in your string; j is the index you want to check the evenness of.
if (j%2) should provide the correct path. You're using if( word[j] %2) which is doing modular arithmetic on a character, not an index. Most likely using modulo on the ASCII value. Hope this helps.
you want to check if the index is even,yet you compare word[j] % 2 == 0 which is not an index.
what you should do:
if(j % 2 == 0){
}

Cannot assign to 'writeline' because it is a 'method group' [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 7 years ago.
Improve this question
The below code is giving me error "Cannot assign to 'writeline' because it is a 'method group;" at every Console.Writeline statement. I am trying to find out why but I could not find anything to point me in right direction. Any help appreciated.
{
class FizzBuzz
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
if ((i % 3 == 0) && (i % 5 == 0))
{
Console.WriteLine = "Fizzbuzz" ;
}
else if (i % 3 == 0)
{
Console.WriteLine = "Fizz";
}
else
(i % 5 == 0)
{
Console.WriteLine = "Buzz";
}
System.Console.ReadLine();
}
}
}
You need to set the string in parenthesis:
Console.WriteLine("This string goes to console.");
You tried to assign the method with a value, that is not possible. That works only with properties and fields.

how to separate numbers by asterisks c# [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 8 years ago.
Improve this question
I don't understand how to do this im new to programming but im sure its simple. help the code I have so far is:
I need the asterisks to be separating the numbers
for (int i = 0; i < 101; i++)
Console.WriteLine(i);
Console.WriteLine("********");
but the asterisks is only showing at the bottom
You should put the body of the loop into a scope:
for (int i = 0; i < 101; i++)
{
Console.WriteLine(i);
Console.WriteLine("********");
}
Otherwise only the following next command is interpreted as the body of the loop, equivalent to this:
for (int i = 0; i < 101; i++)
{
Console.WriteLine(i);
}
Console.WriteLine("********");
The way you have it is only doing the for loop on the first line. You need to put both lines into a 'block' using curly braces:
for (int i = 0; i < 101; i++)
{
Console.WriteLine(i);
Console.WriteLine("********");
}

C# - only assignment call increment decrement await and new object expressions can be used as a statement [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 8 years ago.
Improve this question
I got the the following error when I use a code:
only assignment call increment decrement await and new object expressions can be used as a statement
I get this when I use this code:
for (int i = enemies.Count - 1; i >= 0; i-)
{
enemies[i].Update(gameTime);
if (enemies[i].Active == false)
{
enemies.RemoveAt(i);
}
}
PS: I get the error on I- first line : I >= 0; I-
Use i-- to decrement. Use two minuses.
This is the equivalent of i -= 1; or i = i - 1; It is the expression that progresses the iteration
It could be a class level issue, but more likely it is that you are not decrementing i properly.
It should read:
for (int i = enemies.Count -1; i >= 0; i--) { // etc... }
You are just using i- which is incorrect.
Your error located in this row:
for (int i = enemies.Count - 1; i >= 0; i-)
You need to use this code:
for (int i = enemies.Count - 1; i >= 0; i--)
You probably meant i--
for (int i = enemies.Count - 1; i >= 0; i--)
The error message indicates that some piece of code is not a complete statement. For this line, there are 3 "statements":
int i = enemies.COunt -1;
i >= 0;
i-;
If you imagine writing these lines outside of the for loop declaration, you immediately see that i- is not a complete line of code.
only assignment call increment decrement await and new object expressions can be used as a statement
Translated into easy wording:
Only assignemnt (variable = value), call ( DoSomething() ), increment (i++), decrement, (i--), await (await DoSomethingAsync() ), and new object (new List<Object>() ) expression can be used as a statement
Your problem is with your for statement, you only have i- not i--.
Since i- is not an operation it is viewed as a statement.

Replace string value in C#? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I am new to C#, and need a little help, i know in PHP that i can to something like this
$SelectedEvent="event selected";
if (i > 0)
{
$SelectedEvent = "event";
}
It replace a string easy, but in c# that is not working ok, here is example code i have that is not working
string SelectedEvent = "event selected";
if (i > 0)
{
SelectedEvent = "event";
}
This is not working like in PHP, i can not override the varible?
Here is my example code
EDITED
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
string SelectedEvent = "event";
}
}
You should not redeclare the variable twice but only modify its value:
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
SelectedEvent = "event";
}
// here you can use the SelectedEventVariable
// its value will be "event selected" on the first and second
// iterations and "event" on subsequent
}

Categories

Resources