# print temp at intervals of 20

Page 1 of 1

## 6 Replies - 708 Views - Last Post: 28 May 2009 - 06:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=107387&amp;s=11bce7a266620f8556e36101cea15356&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 suzi1811

Reputation: 0
• Posts: 27
• Joined: 06-February 09

# print temp at intervals of 20

Posted 28 May 2009 - 04:11 AM

Hi,
I'm just learning c++ and have to do a for loop with an if statement to print out the temperature at intervals of 20 and I can't do it. This is my very poor attempt
```#include <iostream>
using namespace std;
int main(){
//declare variables
int temp = 0;
//for loop, with if-else statement
for (int a = 0; a >= temp; a++)
{
if (temp < 200)
{
cout<< "temperature is " <<temp;
}//end if
temp+20;
}//end for
return 0;
}

```

Any help would be greatly appreciated
Suzi

Is This A Good Question/Topic? 0

## Replies To: print temp at intervals of 20

### #2 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: print temp at intervals of 20

Posted 28 May 2009 - 05:30 AM

What do you need the if statement for?

```for (int temp = 0; temp < 200; temp+=20)
{
cout << "temperature is " << temp;
}

```

### #3 suzi1811

Reputation: 0
• Posts: 27
• Joined: 06-February 09

## Re: print temp at intervals of 20

Posted 28 May 2009 - 05:46 AM

AndyH1963, on 28 May, 2009 - 04:30 AM, said:

What do you need the if statement for?

```for (int temp = 0; temp < 200; temp+=20)
{
cout << "temperature is " << temp;
}

```

The exercise I've been given says I have to use a for with if-else.
Suzi

### #4 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: print temp at intervals of 20

Posted 28 May 2009 - 05:49 AM

Does the question state why use of if and else are required?

Suggest that you post the question as well.

### #5 suzi1811

Reputation: 0
• Posts: 27
• Joined: 06-February 09

## Re: print temp at intervals of 20

Posted 28 May 2009 - 05:52 AM

Hey
That really helped I got it working!
```#include <iostream>
using namespace std;
int main(){
//declare variables
int temp;

//for loop, with if-else statement
for (temp = 0; temp <=200; temp+=20)
{
if (temp != 200)
{
cout << "temperature is " << temp;
}//end if
cout<<"\n";
}//end for
return 0;
}

```

Prints out on each line with temp at intervals of 20 I'd been doing the increment bit wrong.
Thanks for helping me out
Suzi

### #6 AndyH1963

Reputation: 22
• Posts: 201
• Joined: 22-June 08

## Re: print temp at intervals of 20

Posted 28 May 2009 - 05:56 AM

Two points:

(1) you don't need to declare the variable temp outside of the for loop. Unless you are going to need it latter.

(2) I still don't see any need for the if condition.

```#include <iostream>
using namespace std;
int main()
{
for (int temp = 0; temp <=200; temp+=20)
{
cout << "temperature is " << temp << endl;
}

return 0;
}

```

### #7 computerfox

• straight vegetarian kid

Reputation: 50
• Posts: 3,772
• Joined: 29-January 09

## Re: print temp at intervals of 20

Posted 28 May 2009 - 06:55 AM