Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2015
    Posts
    1

    simple c# help homework

    Can someone please help me give a sample answers for this question.

    Declare an int variable i initialize it to 4, and then test the following increment and decrement statements. Comment on the obtained output.


    1. Console.WriteLine (++i);
    2. Console.WriteLine (i);
    3. Console.WriteLine (i++);

    Thank you looking forwards!

  2. #2
    Join Date
    Jan 2014
    Location
    Lynnwood, WA
    Posts
    46
    If you post your code, and your explanations of why each number is the value, then it would not be hard to help if you've mis-stepped on your assessment of it, but it wont help you to learn to give the answer.

    I will give you two hints by adding these two lines of code to the end of your method:
    Console.WriteLine(i);
    Console.ReadKey(); // this keeps the window from closing before you can look at it

  3. #3
    Join Date
    Aug 2010
    Posts
    158
    ++i is pre-op - i gets incremented pre-return
    i++ is post-op - i gets incremented post return

    Code:
    int i = 4;
    Console.WriteLine (i);    // i = 4, Output = 4
    Console.WriteLine (++i);  // i = 5, Output = 5
    Console.WriteLine (i);    // i = 5, Output = 5
    Console.WriteLine (i++);  // i = 6, Output = 5
    Console.WriteLine (i);    // i = 6, Output = 6
    Last edited by am385; 07-02-2015 at 09:27 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •