Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statment/condition....but unsure how to say it?? [Newbie]

Status
Not open for further replies.

webstaff

Technical User
Jan 2, 2003
97
0
0
GB
Hi Guys
Im trying out some C and I find I need an IF statment

Im just not sure how to put my condition and wondered if someone could guide me??

Thanks again


if(number is between 10 and 15)<< stumped on correct systax
{
printf("whatever here");
}
 
Code:
#include <stdio.h>

int main(void)
{
   int number;
   for ( number = 0; number < 20; ++number )
   {
      if ( [red]number >= 10 && number <= 15[/red] )
      {
         printf("number = %d\n", number);
      }
   }
   return 0;
}

/* my output
number = 10
number = 11
number = 12
number = 13
number = 14
number = 15
*/
 
Hey dave,,

I tryed tha and it errors out on the line

if ( number >= 10 && number <= 15 )

The message says "Cannot convert int to int in function main"

Any ideas??
Thanks again
 
Code:
number >= 10 && number <= 15
could be (and arguably should be) written
Code:
10 <= number && 15 >= number

The idea is that getting in the habit of writing comparisons with constants first gaurds against the "if( i = 3)" error, when the intent was "if(i == 3)"...

Just a suggestion for a habit I'd like to force myself into.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top