By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,315 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Multi Variable For statements

P: 4
I need to come up with a For statement that would allow me to do something like this...

c:\> countup
Enter a start value: 10
Enter an ending value: 100
Enter an increment: 20
Counting: 10 30 50 70 90


I am not able to get it to count by a variable I input and also to have it bounded by the upper and lower limits i Input... any got any ideas ...thank you
Jun 8 '07 #1
Share this Question
Share on Google+
9 Replies


Savage
Expert 100+
P: 1,764
I need to come up with a For statement that would allow me to do something like this...

c:\> countup
Enter a start value: 10
Enter an ending value: 100
Enter an increment: 20
Counting: 10 30 50 70 90


I am not able to get it to count by a variable I input and also to have it bounded by the upper and lower limits i Input... any got any ideas ...thank you
Yes,we have a lot of ideas,but we cannot realise them if u don't show us ur code.

Savage
Jun 8 '07 #2

ilikepython
Expert 100+
P: 844
I need to come up with a For statement that would allow me to do something like this...

c:\> countup
Enter a start value: 10
Enter an ending value: 100
Enter an increment: 20
Counting: 10 30 50 70 90


I am not able to get it to count by a variable I input and also to have it bounded by the upper and lower limits i Input... any got any ideas ...thank you
That's pretty simple:
Expand|Select|Wrap|Line Numbers
  1. int start = 10;
  2. int end = 100;
  3. int incr = 20;
  4.  
  5. for (int i = start; i < end; i += incr){
  6.     //code
  7. }
  8.  
Is that what you need?
Jun 8 '07 #3

P: 4
well the starting value and ending value and the increments are all user input values....my code wouldn't help out at all....it is just a basic FOR statement code
Jun 8 '07 #4

ilikepython
Expert 100+
P: 844
well the starting value and ending value and the increments are all user input values....my code wouldn't help out at all....it is just a basic FOR statement code
What do you mean?
You don't know how to get input or you don't know how to code the for loop?
Jun 8 '07 #5

Savage
Expert 100+
P: 1,764
well the starting value and ending value and the increments are all user input values....my code wouldn't help out at all....it is just a basic FOR statement code
Can we see that thing?(code)

Savage
Jun 8 '07 #6

P: 4
#include <stdio.h>


main ()
{
int a, w=0, x, y, z;

// Stetting up the Starting Values
printf("Enter a Starting Value:");
scanf("%d", &x);
printf("Enter an Ending Value:");
scanf("%d", &y);
printf("Enter an Increment:");
scanf("%d", &z);


for( a; a <= y; ++w) { //setting up the conditions for the program and making it count accurately
x + (w * z) == a;


if( y < x ){
printf("Invalid Option Starting Value must be smaller than Final Value\n");
break; //If person makes a mistake on which value is bigger
} if (z < 0){
printf("Increments Cannot Be a Negative Number\n");
break; } //If user forgets that increments cannot be a negative number


}
printf("Counting: %d\n",a);//For Statement so the program can count out loud


system("pause");
return(0);
}



This is what i have idk how much help it will be
Jun 8 '07 #7

ilikepython
Expert 100+
P: 844
#include <stdio.h>


main ()
{
int a, w=0, x, y, z;

// Stetting up the Starting Values
printf("Enter a Starting Value:");
scanf("%d", &x);
printf("Enter an Ending Value:");
scanf("%d", &y);
printf("Enter an Increment:");
scanf("%d", &z);


for( a; a <= y; ++w) { //setting up the conditions for the program and making it count accurately
x + (w * z) == a;


if( y < x ){
printf("Invalid Option Starting Value must be smaller than Final Value\n");
break; //If person makes a mistake on which value is bigger
} if (z < 0){
printf("Increments Cannot Be a Negative Number\n");
break; } //If user forgets that increments cannot be a negative number


}
printf("Counting: %d\n",a);//For Statement so the program can count out loud


system("pause");
return(0);
}



This is what i have idk how much help it will be
Did you see the for loop I posted earlier? I think that might help you.
Jun 9 '07 #8

P: 4
yes i saw that, but the start value, end value, and the increment value are all variables ...when using the program you can insert any numbers you would like...that is my main problem with this program
Jun 9 '07 #9

ilikepython
Expert 100+
P: 844
yes i saw that, but the start value, end value, and the increment value are all variables ...when using the program you can insert any numbers you would like...that is my main problem with this program
Ok, so you just input the variables like you did in your program. Just change the for loop.

Like this:
Expand|Select|Wrap|Line Numbers
  1. //input for x (start)
  2. //input for y (end)
  3. //input for z (increment)
  4.  
  5. for (int i = x; i < y; i += z)
  6. {
  7.     //code here
  8. }
  9.  
  10. // or another version
  11.  
  12. for (; x < y; x += z)
  13. {
  14.     //code here
  15. }
  16.  
Does that help?

For loops need to have at least three statements but the computer doesn't care what they are:
Expand|Select|Wrap|Line Numbers
  1. for (;;)
  2. {
  3.     //code here
  4. }
  5.  
That is a valid for loop.
Jun 9 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.