# Using Loop Commands and odd integers

• 10-05-2011
Sammy2011
Using Loop Commands and odd integers
Hello there!

I am new to programming and I am having trouble with a homework assignment. I was hoping someone could point me in the right direction.

I need to use three of the loop commands, while, for and do. I am supposed to ask the user for an integer n and use the loop commands to display all odd integers from 0 to n. Then I need to display these values calling on each loop command.

I have no idea how to start! :(

This is what I have.. Don't make fun!

Code:

#include <iostream>

int odd(int n)
{
for (int i = 1 : i <= n ; std::cout << i << std::endl, i+=2 )
{ }
return 0;
}

int main()
{

std::cout <<" Please enter n: ";
std::cin >> n;

I don't know where to start or if I'm even heading the right direction. I don't expect you to do this for me but I need help!

Thanks!

Sam
• 10-05-2011
valaris
Your syntax for a for loop is not even close to being correct. Review in your book/notes the syntax for the loops. For instance for a "for" loop:

Code:

for (int i = 1; i <= n; i+=2)
{
std::cout << i << std::endl;
}

The general syntax is for (initializer; condition; increment;) { body }. Where initializer is ran immediately and once at the beginning of the loop, then condition is checked. If condition is true then the body of the loop runs until the end, where increment is ran. Note increment doesn't necessarly have to increment, but in many cases this is the common behavior. Finally condition is checked again and the process either repeats, or the loop is finished.
• 10-05-2011
Sammy2011
Thanks for the speedy reply. I looked in my book but it doesn't give me a lot of examples. Also..

I have to declare n as an integer still, correct? Also what does i+=2 mean? I know it is i = i + 2. I don't see how this works.

Also, can I write my syntax for each loop function before I run the main function?

Sam
• 10-05-2011
manasij7479
• 10-05-2011
Sammy2011
OK, I read up a little. I understand why my syntax was wrong. Here is my next question. First my code..

Code:

#include<iostream>

usingnamespacestd;

int main()
{
int n;
for(int i=1;i<=n;i+=2)
{
cout<<i<<endl;
}

cin>>n;
cout<<"For_odd is:"<< n;

return0;
}

I don't how to display every integer from 0 to n using i in my syntax.Any suggestions??

Sam
• 10-05-2011
manasij7479
Think of the order logically..
How can you input the number after running the loop ?
• 10-05-2011
Sammy2011
Could I input the number within the braces containing "cout" after I run the loop? Where n is equal to every iteration of i?
• 10-05-2011
manasij7479
Quote:

Originally Posted by Sammy2011
Could I input the number within the braces containing "cout" after I run the loop? Where n is equal to every iteration of i?

NOT AFTER... You need the value of n before the loop runs,..
And within the loop, you have to output the value of the counter.
• 10-05-2011
Sammy2011
I'm sorry but I honestly can't figure this out..
• 10-05-2011
manasij7479
You need to think ..

Okay.. try writing a flowchart for it... not code.. just sequential steps in human language.
• 10-06-2011
Elysia
The execution order is sequential. That is, the lines are executed top down. Anything that is below any given line is not related to the line in any way.
So, for example, you cannot say

int n;
for (int i = 1; i <= n; i++)
//...
std::cin >> n;

because at the for line, n has an undefined value. The fact that the std::cin statement becomes later has little meaning. It will only execute after the for loop.
C++ is a do this, then do that, kind of language. You write the things you want it do with every line.

So how would you do this in real life?
You would ask the user for what value n they want.
Then you would loop n times.
This implies that you need to have n before the loop, because that's what the logic says.

The current code just says
Loop an undefined amount of times (because n is undefined).

As for how to print an odd number... one idea might be to check if the number you're going to print is odd.
If you know some discrete mathematics, you know that any even number is dividable by 2. But odd numbers aren't.
• 10-06-2011
Sammy2011
Very helpful! Thank you very much. I fixed it. It makes sense, it took me 2 seconds lol.