6 Replies - 1920 Views - Last Post: 15 November 2007 - 09:57 AM Rate Topic: -----

#1 Boyan   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

A 'for' question

Post icon  Posted 15 November 2007 - 08:35 AM

Hello.
I'm trying to write this simple program using 'for', and probably I'm making a mistake that I cannot figure it out.

#include <stdio.h>

int main()

{

int i, j, k;

printf("Enter j:\n");
scanf("%d", &j);
printf("Enter k:\n");
scanf("%d", &k);

i = j + k;

for (i=0, j+k)

{

printf("i = %d", i);

}

return 0;

}



Thank you.

This post has been edited by Boyan: 15 November 2007 - 08:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: A 'for' question

#2 ReggaetonKing   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 73
  • Joined: 16-November 06

Re: A 'for' question

Posted 15 November 2007 - 08:44 AM

This is an example of a for loop in C++
int numOfTimes = 10;
for(int x = 0; x < numOfTimes; x++)
{
	 //body of loops goes here! Runs 10 times
}


First part (int x = 0) is only runs when the loops is first executes. The second part ( x < numOfTimes) is the conidition and checks it when the loop is finish going through the entire body. Last part is what you want the execute after the loop is finish going through the entire body.

Hope this helps.

This post has been edited by ReggaetonKing: 15 November 2007 - 08:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 Boyan   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: A 'for' question

Posted 15 November 2007 - 08:49 AM

I know this, but thanks. I tried to complete my program without using the last part, because I thought there's no need for incrementing the value. :S
Was This Post Helpful? 0
  • +
  • -

#4 ReggaetonKing   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 73
  • Joined: 16-November 06

Re: A 'for' question

Posted 15 November 2007 - 09:00 AM

You say you know but in your for loop it doesn't look like you do. No offense.

i = j + k;

for (i=0, j+k)

{

printf("i = %d", i);

}


^Above code does not make sense. You store j + k in i and then put zero in i again. j + k is not a boolean condition, since j nor k doesn't change, the loop will always run unless j + k = 0.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6727
  • View blog
  • Posts: 31,140
  • Joined: 10-May 07

Re: A 'for' question

Posted 15 November 2007 - 09:01 AM

View PostBoyan, on 15 Nov, 2007 - 09:35 AM, said:

#include <stdio.h>

int main() {
  int i, j, k;

  printf("Enter j:\n");
  scanf("%d", &j);
  printf("Enter k:\n");
  scanf("%d", &k);
  i = j + k;

  for (i=0, j+k) {
	printf("i = %d", i);
  }
  return 0;
}


Your for loop only has 2 arguments, you must have 3.

for(;;);


This is an example of an infinite loop.

Also, you used a comma for your delimiter, you must use the semi-colon.

for(begin;limit;incrimentor);



for(i=0;i<100;i++);


Was This Post Helpful? 0
  • +
  • -

#6 Boyan   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 254
  • Joined: 09-September 07

Re: A 'for' question

Posted 15 November 2007 - 09:04 AM

Thanks everyone.
I just made it work.

#include <stdio.h>

int main()

{

int i, j, k;

printf("Enter j:\n");
scanf("%d", &j);
printf("Enter k:\n");
scanf("%d", &k);

i = j + k;

for (i=j+k;; j+k)
break;

{

printf("i = %d\n", i = j + k);

}

return 0;

}


Was This Post Helpful? 0
  • +
  • -

#7 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: A 'for' question

Posted 15 November 2007 - 09:57 AM

This:
for (i=j+k;; j+k)
break;
is not very helpful. I don't even think it would compile.

the syntax for a for-loop is as follows:

for (<initialize loop>; <condition>; <step>) statement

the inner parts of the for-loop are three expressions. The first one is used to set the loop up, it is executed before anything else. The next expression is evaluated as a boolean expression (either true or false) and it can be though of as:

loop as long as <condition> is true.

The last expression is evaluated at the bottom of the loop (just before it loops back to the top). Normally the three expressions are linked -- that is that the first one sets thing up, and the third one will eventually cause the second expression to evaluate to false.

so for example to count to 10:
for (int i=1; i < 10; i++) {
	printf("%d", i);
}


to count the number of characters in a cstring:
char *str = "I had a tiny turtle his name was Tiny Tim.";
int length = 0;
for (char* ptr = str; str != 0x00; str++) length++;


this can be simplified to:
char *str = "I had a tiny turtle his name was Tiny Tim.";
int length;
char * ptr;
for ((ptr = str, length = 0); str != 0x00; (str++, length++)); 


Some programmers like to use "infinite" loops using the break statement to exit the loop. Although there is nothing WRONG with this, you should use the control structures as they were designed. You should use the ending condition if it can be used. The break statement can become very complicated to use and often causes problems when code is updated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1