Thread: Iteration

  1. #1
    Registered User
    Join Date
    Sep 2007


    What is the value of variable i and of variable j on line 4 below:

    Line  Code
    1    j=0;
    2    for(i=0; i<10;i+=3)
    3         j=j+2;
    4    // What is the value of variable i and variable j?
    Is this how it iterates:
    i j
    0 0
    3 2
    6 4
    9 6

    therefore i=9 and j=6 ?????

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    What happened to just trying it out?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Check for yourself by printing them out. Note that line 4 is after the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2007
    unfortunatley i dont have a compiler

    but i think i get it.

    i = 0
    j = 2
    i = 3
    j = 4
    i = 6
    j = 6
    i = 9
    j = 8

    Therefore j = 8 i = 12

    at uni we use linux and i think C90 compiler. where can i get a compiler similar to that but for windows vista

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Iteration?
    By Aliaks in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2009, 11:17 PM
  2. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM
  3. recursivity & iteration
    By Marlon in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2005, 01:58 PM
  4. The "continue" statement in JAVA
    By Hexxx in forum A Brief History of
    Replies: 1
    Last Post: 02-26-2004, 06:19 PM
  5. Iteration help please
    By incognito in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2001, 07:37 AM