4 Replies - 186 Views - Last Post: 02 February 2012 - 08:38 PM Rate Topic: -----

Topic Sponsor:

#1 Kalle114  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-February 12

loop weeks

Posted 02 February 2012 - 06:47 AM

Yes it is for school but you dont need to show it to the teacher.

    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 3; i <= 52; i++)

            {
                
                Console.Write("{0} {1,2}", "Week", i);
                Console.Write("{0,10} {1,2}", "Week", i);
                Console.Write("{0,10} {1,2}", "Week", i);
                Console.ReadLine();
            }
        }
    }
}


I know this is wrong but how can i get it so it show every 3 week up to 52. and is it a way to do is whit out typing console write line for every new week?

Is This A Good Question/Topic? 0
  • +

Replies To: loop weeks

#2 JackOfAllTrades  Icon User is online

  • No Sugar Coding Here!
  • member icon

Reputation: 4684
  • View blog
  • Posts: 20,362
  • Joined: 23-August 08

Re: loop weeks

Posted 02 February 2012 - 06:52 AM

HINT: Increments/decrements in a for loop construct are not limited to using ++ or --; you can also use something like -=, etc.
Was This Post Helpful? 0
  • +
  • -

#3 RexGrammer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 152
  • View blog
  • Posts: 664
  • Joined: 27-October 11

Re: loop weeks

Posted 02 February 2012 - 09:39 AM

Consider using the addition assignment operator (+=). To understand this problem you have to truly realize what x++ does. It is the same as if you wrote: x = x + 1; or x += 1;. So if you need to increment by three then, ...

Some suggested reading:
+= Operator (C# Reference)
++ Operator (C# Reference)
Was This Post Helpful? 0
  • +
  • -

#4 superkb10  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 230
  • Joined: 27-November 11

Re: loop weeks

Posted 02 February 2012 - 08:09 PM

I think where you're confused is that the first part of a for loop doesn't specify how large the i++ will be. As already stated the ++ operator just adds one to the number. The first part in the for loop just sets the starting value for i.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • Coder-ian
  • member icon

Reputation: 1401
  • View blog
  • Posts: 7,358
  • Joined: 29-May 08

Re: loop weeks

Posted 02 February 2012 - 08:38 PM

A for-loop (in C#) is a just while loop.
This
 for ( vari = init, cond_predicate, next_value_function )
 {
  loop_ code_body
 }

can be thought as this.
{
 var variable = init;
 while ( cond(variable) )
 {
  loop_code_body
  variable = next_value_function( varible )
 }
}

This post has been edited by AdamSpeight2008: 02 February 2012 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1