Good Morning,
My name is Greg and I am trying to teach myself to write code. But I have a question I wrote a program that compares a range of numbers and tells you the grade. The program works and dose what it is supposed to do but I have some questions.
1) When you compare a range such do you use && or do you use the or statement to compare them? As I understand them when you and both conditions must be ture and with an or statement only one has to be ture. So then can they both not work to compare a range of numbers?

2) In c++ you can use the word "and" or the && do these have the same effect?

3) When you are useing nested if-then-else such as these do you nest the parenthese such as in the first if statement? When you nest them how dose this work? Is like the order of opprations where the parenthese are done first or is it read left to right just like it is typed in?

if((gradeIn >= 0) && (gradeIn <= 49 ))  {
                cout << " You fail try again "<< endl ;

            }  else if (gradeIn >= 50  and gradeIn <= 69 ){
                cout << " Your grade is  " << gradeIn <<" You passed "<< endl;

Thank you,
Greg

1) When you compare a range such do you use && or do you use the or statement to compare them?

You can use either, but typically the result is reversed between then:

// Compare range [lbound, ubound) using &&
if (x >= lbound && x < ubound)
{
    // In range
}
else
{
    // Out of range
}

// Compare range [lbound, ubound) using ||
if (!(x < lbound || x >= ubound))
{
    // In range
}
else
{
    // Out of range
}

2) In c++ you can use the word "and" or the && do these have the same effect?

As a matter of fact, you can and they do. C++ supports alternative keywords for several operators that are problematic internationally. However, note that some compilers won't enable them by default and you need to include the <ciso646> header.

3) When you are useing nested if-then-else such as these do you nest the parenthese such as in the first if statement?

I'm not entirely sure what you're asking, but it sounds like a style question. Do whatever you find to be most readable, but there are a few common bracing styles.

When you compare a range such do you use && or do you use the or statement to compare them?

You cannot compare ranges. You can only compare two objects at a time. You could write code yourself to compare, for example, two lowest nubmers and then two highest numbers and compare the range yourself like that, but C++ doesn't come with that.

In c++ you can use the word "and" or the && do these have the same effect?

Same effect. It's more common to use &&

When you are useing nested if-then-else such as these do you nest the parenthese such as in the first if statement?

The code you have there is not nested. I think you might be asking "What order are these comparisons done in? if((gradeIn >= 0) && (gradeIn <= 49 ))

Left to right. In this case, if gradeIn is less than zero, the comparison on the right will not be done, as it won't make any difference. This is known as short-circuit evaluation.

When you compare a range such do you use && or do you use the or statement to compare them?

If you want to check whether a value is inside a range, use &&: (x >= 1) && (x <= 10).

If you're checking whether it's outside a range, use ||: (x < 1) || (x > 10).

As I understand them when you and both conditions must be ture and with an or statement only one has to be ture.

Yes.

So then can they both not work to compare a range of numbers?

It depends on what you're comparing for; like in my examples above.

When you're not sure which to use, it can be helpful to draw the inequalities on a number line and see what they do

In c++ you can use the word "and" or the && do these have the same effect?

They are the same thing. and and others like it exist for historical reasons. Nothing wrong with using it, but the symbolic && seems to be more widely used.

Ok I understand it now, thank you all so much.
@Moschops that is what I was asking sorry if I was not clear on the question I have seen them writen both ways and am trying to understand both C++ and JAVA at the same time.

Thanks again everyone.
Greg

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.