• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

if statement

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


I have a question like :

Print Average if grade is less than 80 Sbut greater than 70.

For this question Should I use && (and) operator in the if block or the ||(or) operator. because it is given as but ,I am bit not sure. Please help me out.

Tell me which statement should I use:
1) if(number >70 && number<80)
2) if(number >70 || number<80).
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand which grades should be included in the statement "grade is less than 80 but greater than 70"? Is 90 included? How about 60?

If you're still confused by but, look at the two options you've posted. Do they both make sense? What range of grades would each allow to pass the if condition?
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is #1.

"if(number >70 && number<80)" means "If the number is greater than 70 and the number is less than 80". That's what you want.

If you did "if(number >70 || number<80)" you would get all numbers because all numbers are greater than 70 OR less than 80.
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"if(number >70 && number<80)" is in range (70, 80) but
"if(number >70 || number<80)" in in range ANY NUMBERS
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic