Jump to content

C# Counter

Hey i have to write a little console Porgramm in C# for school that count up to your input.

 

So when the input is 5 for example the result should be like this:

0 1 2 3 4 5

 

and so on...

 

can somebody help me how i get this?

 

Thanks!

|| CPU: I7 6950X @4.6 (1.35V) || Cooler: CUSTOM EK LOOP || Motherboard: ASUS RAMPAGE V EDITION 10

|| GPU: 2x Vega 64 Strix OC @1762 / 1100 MHz || Memory: 16GB G.Skill Royal RGB 3200 Mhz ||

HDD: Segate Barracuda 3TB || SSD: Samsung 970 Evo 512 GB || PSU: Corsair HX 1200 W || Case: Fractal Design Define R6 Gunmetal || Fans: Corsair HD120 (x4) / BeQuiet Silent Wings 3 (x4) || Monitor: ACER XF27HU ||

Second Monitor: BENQ RL2455HM || Mouse: Logitech G502 Pretus Core || Keyboard: Logitech G810 Orion Spectrum || Headphones: Sennheiser IE80 ||

 

                                                                                                          Buildlog expirience swapping to x99: 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

This sounds like homework or an assignment, so I'm not going to answer it directly. Read up on loops and you should be able to figure this out.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, joshuawi99 said:

can somebody help me how i get this?

Well, I'm sure your teacher went over loops before handing you this assignment. So I don't feel too bad repeating @tikkers statement that you should look into loops.

However, I will go to the length of teaching some things about the different types of loops available in C#:

  • Foreach loops
  • While loops
  • For loops
  • Do while loops

Foreach Loops:

Spoiler

A "foreach" loop iterates over some items in a collection, doing some action on each of those items. You cannot assign anything to the elements of the collection inside a foreach loop.

Here's a simple example:


SomeCollection myCollection = new SomeCollection();
// add things to myCollection

foreach (type t in myCollection)
{
  // do some action. Action cannot assign to variable t
}

 

 

While loops:

Spoiler

A while loop iterates over some block of code as long as some condition is true. You can assign to any variables that are in scope inside a while loop.

 

Here's a simple example:


// this is a fizzbuzz program

int myInt = 0;

while (myInt < 101)
{
  if ((myInt % 3 == 0) && (myInt % 5 == 0))
    Console.WriteLine("FizzBuzz");
  
  else if (myInt % 3 == 0)
    Console.WriteLine("Fizz");
  
  else if (myInt % 5 == 0)
    Console.WriteLine("Buzz");
  
  else
    Console.WriteLine(myInt);
  
  myInt++;
}

 

 

For loops:

Spoiler

A for loop is the same thing as a while loop. Infact, the compiler output for the while loop example above is exactly identical to the compiler output for the for loop example below. The difference is that a for loop lets you describe all of the conditions that affect the iteration (the variable, the condition, and the action on the variable) in the loop declaration. 

Here is the while loop example, modified to use a for loop:


for (int myInt = 0; myInt < 101; myInt++)
{
  if ((myInt % 3 == 0) && (myInt % 5 == 0))
    Console.WriteLine("FizzBuzz");
  
  else if (myInt % 3 == 0)
    Console.WriteLine("Fizz");
  
  else if (myInt % 5 == 0)
    Console.WriteLine("Buzz");
  
  else
    Console.WriteLine(myInt);
}

 


Do While loop:

Spoiler

A "do while" loop is the same as a while loop, except, the do while loop is guaranteed to execute atleast one time. To see why this is different than a while loop, we need to examine a simple while loop. Take this one for example:


while (myBool)
{
	// Do stuff
}

 

In the above example, if myBool is false before the first iteration, this loop will never execute. If we need the loop to execute atleast once regardless of the value of myBool, then we would use a do while loop:


do
{
	// Do something
} while (myBool);


In this example, the loop will execute atleast once, regardless of the value of myBool before the first iteration.


Given these examples, which loop, or loops, do you think are applicable to your problem? 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, straight_stewie said:

Well, I'm sure your teacher went over loops before handing you this assignment. So I don't feel too bad repeating @tikkers statement that you should look into loops.

However, I will go to the length of teaching some things about the different types of loops available in C#:

  • Foreach loops
  • While loops
  • For loops
  • Do while loops

Foreach Loops:

  Hide contents

A "foreach" loop iterates over some items in a collection, doing some action on each of those items. You cannot assign anything to the elements of the collection inside a foreach loop.

Here's a simple example:



SomeCollection myCollection = new SomeCollection();
// add things to myCollection

foreach (type t in myCollection)
{
  // do some action. Action cannot assign to variable t
}

 

 

While loops:

  Hide contents

A while loop iterates over some block of code as long as some condition is true. You can assign to any variables that are in scope inside a while loop.

 

Here's a simple example:



// this is a fizzbuzz program

int myInt = 0;

while (myInt < 101)
{
  if ((myInt % 3 == 0) && (myInt % 5 == 0))
    Console.WriteLine("FizzBuzz");
  
  else if (myInt % 3 == 0)
    Console.WriteLine("Fizz");
  
  else if (myInt % 5 == 0)
    Console.WriteLine("Buzz");
  
  else
    Console.WriteLine(myInt);
  
  myInt++;
}

 

 

For loops:

  Hide contents

A for loop is the same thing as a while loop. Infact, the compiler output for the while loop example above is exactly identical to the compiler output for the for loop example below. The difference is that a for loop lets you describe all of the conditions that affect the iteration (the variable, the condition, and the action on the variable) in the loop declaration. 

Here is the while loop example, modified to use a for loop:



for (int myInt = 0; myInt < 101; myInt++)
{
  if ((myInt % 3 == 0) && (myInt % 5 == 0))
    Console.WriteLine("FizzBuzz");
  
  else if (myInt % 3 == 0)
    Console.WriteLine("Fizz");
  
  else if (myInt % 5 == 0)
    Console.WriteLine("Buzz");
  
  else
    Console.WriteLine(myInt);
}

 


Do While loop:

  Hide contents

A "do while" loop is the same as a while loop, except, the do while loop is guaranteed to execute atleast one time. To see why this is different than a while loop, we need to examine a simple while loop. Take this one for example:



while (myBool)
{
	// Do stuff
}

 

In the above example, if myBool is false before the first iteration, this loop will never execute. If we need the loop to execute atleast once regardless of the value of myBool, then we would use a do while loop:



do
{
	// Do something
} while (myBool);


In this example, the loop will execute atleast once, regardless of the value of myBool before the first iteration.


Given these examples, which loop, or loops, do you think are applicable to your problem? 

Thank you very much I got it ! :)

|| CPU: I7 6950X @4.6 (1.35V) || Cooler: CUSTOM EK LOOP || Motherboard: ASUS RAMPAGE V EDITION 10

|| GPU: 2x Vega 64 Strix OC @1762 / 1100 MHz || Memory: 16GB G.Skill Royal RGB 3200 Mhz ||

HDD: Segate Barracuda 3TB || SSD: Samsung 970 Evo 512 GB || PSU: Corsair HX 1200 W || Case: Fractal Design Define R6 Gunmetal || Fans: Corsair HD120 (x4) / BeQuiet Silent Wings 3 (x4) || Monitor: ACER XF27HU ||

Second Monitor: BENQ RL2455HM || Mouse: Logitech G502 Pretus Core || Keyboard: Logitech G810 Orion Spectrum || Headphones: Sennheiser IE80 ||

 

                                                                                                          Buildlog expirience swapping to x99: 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×