C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,493 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,833 people online right now. Registration is fast and FREE... Join Now!




A 'for' question

 

A 'for' question

Boyan

15 Nov, 2007 - 07:35 AM
Post #1

D.I.C Head
**

Joined: 9 Sep, 2007
Posts: 170



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

CODE

#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 Nov, 2007 - 07:38 AM

User is offlineProfile CardPM
+Quote Post


ReggaetonKing

RE: A 'for' Question

15 Nov, 2007 - 07:44 AM
Post #2

D.I.C Head
Group Icon

Joined: 16 Nov, 2006
Posts: 73



Thanked: 1 times
Dream Kudos: 100
My Contributions
This is an example of a for loop in C++
CODE

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 Nov, 2007 - 07:44 AM
User is offlineProfile CardPM
+Quote Post

Boyan

RE: A 'for' Question

15 Nov, 2007 - 07:49 AM
Post #3

D.I.C Head
**

Joined: 9 Sep, 2007
Posts: 170



Thanked: 2 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

ReggaetonKing

RE: A 'for' Question

15 Nov, 2007 - 08:00 AM
Post #4

D.I.C Head
Group Icon

Joined: 16 Nov, 2006
Posts: 73



Thanked: 1 times
Dream Kudos: 100
My Contributions
You say you know but in your for loop it doesn't look like you do. No offense.

CODE

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.
User is offlineProfile CardPM
+Quote Post

no2pencil

RE: A 'for' Question

15 Nov, 2007 - 08:01 AM
Post #5

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,234



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
QUOTE(Boyan @ 15 Nov, 2007 - 09:35 AM) *

CODE

#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.

CODE

for(;;);

This is an example of an infinite loop.

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

CODE

for(begin;limit;incrimentor);


CODE

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


User is offlineProfile CardPM
+Quote Post

Boyan

RE: A 'for' Question

15 Nov, 2007 - 08:04 AM
Post #6

D.I.C Head
**

Joined: 9 Sep, 2007
Posts: 170



Thanked: 2 times
My Contributions
Thanks everyone.
I just made it work.

CODE

#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;

}

User is offlineProfile CardPM
+Quote Post

NickDMax

RE: A 'for' Question

15 Nov, 2007 - 08:57 AM
Post #7

Can grep dead trees!
Group Icon

Joined: 18 Feb, 2007
Posts: 5,216



Thanked: 285 times
Dream Kudos: 1175
Expert In: Java/C++

My Contributions
This:
CODE
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:
CODE
for (int i=1; i < 10; i++) {
    printf("%d", i);
}


to count the number of characters in a cstring:
CODE
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:
CODE
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.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 04:38AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month