Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# if statement

prerna boja
Ranch Hand
Posts: 67
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).

David Harkness
Ranch Hand
Posts: 1646
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?

Rick Portugal
Ranch Hand
Posts: 243

"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.

Surasak Leenapongpanit
Ranch Hand
Posts: 341
"if(number >70 && number<80)" is in range (70, 80) but
"if(number >70 || number<80)" in in range ANY NUMBERS

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.