What will be printed if the input is 100?
- Good question, because
else if (85 <= X and X < 100)
This is basically saying "if your input is equal or greater than 85 and smaller than 100". So what happens if you input 100? Then input is greater than 85 but also equal to 100 and there is no equal to 100 in that condition. I would say the output is "you did great" because it seems like it's a sort of an 'else' being indented a few spaces (I don't know what programming language that is).
I'm fairly certain that nothing would be printed; I imagine the indentation is there to throw off the observer, but everything in that block until the end if (the two outputs) are certainly part of it- otherwise why would there be end if in this language (whatever it is)
What will be printed if the user enters “Wingding”?
- I don't think anything will be printed then since it's not in the program logic. Maybe an exception might be thrown, depending on the language.
Yep, without knowing what the language is and therefore it's type system it's impossible to say. If it's a dynamic or typeless language nothing will be printed- a strongly typed language will emit a error when X is read (depending of course on the type of X).
What if you enter 2965687923 as a number? Maybe it falls out of scope and the program will think it's greater than 0 because it starts all over (crosses 0 and begins to count again)?
Probably not; I would think if they put a number outside the range of the numeric variable (assuming X is a numeric variable) it would simply give an overflow. of course this is also completely dependent on the type system the language uses and so knowing the language would have been great. agree with your other sentiments regarding robustness.
- How many levels of nesting are there in this design?
I think there is 1 level of nesting.
Nesting is basically the indented stuff you see. e.g.:
output "your grade is" X
output "you did well"
"you did well" is the nested statement.
This depends on the language as well. in C/C++/C#:
if (X < 50)
{
Console.Writeln("X is smaller than 50");
}
else if (X==75)
{
Console.Writeln("X is 75");
}
It looks like a single level of nesting (like the original code) but in this case there are actually two levels of nesting in the else; the else contains a nested if statement ,if (x==75) which itself has the nested Console method call. But this is purely semantics; and that's C#, it's impossible to say wether the language in question here considers else if to be a special token or wether it's treating the statements as an if within and else clause. I'd be inclined to guess the latter simply because the if and else are separated by a space.
You know, I think it's funny because this program is written in a strange way to me.
If (y < x). Makes it a bit confusing. =) Usually I would write if (x < y).
So, it's not just me then! I think the method of testing, whereby the value goes before the variable being tested arose from C, where a programmer might mistakenly write this:
if(value=null)
...
of course, = is the assignment operator, so instead of testing the value, it would assign it to null and then execute the if! quite a source of bugs. So, programmers instead opted to do it this way:
if(null==value)
This way if they accidentally forgot a equals sign, it would cause a compiler error, rather then a difficult to find logic error. The actual reason was apparently lost in time to some people, who not only extended this to the relational operators (for which there is no assignment to avoid) but also started using them in other languages. It's a case of Cargo Cult programming.
Operators explanationIn programming, combinations of operators are used. An operator is something like / * - +.
An operand is the value to be calculated, such as 1 2 3 6 -1 0,1 1,55.
-1 wouldn't be an operand, since the - is a unary minus operator
Combining operators is a short way of writing an expression; e.g. if (0 == x OR 0 < x) can be written short version as: if ( 0 <= x) because it combines == and < at the same time, i.e. <=
yeah... but that's not really a programming concept (aside from the transliteration of ≥ to >= and ≤ to <=. most languages preserve the order even... => is used in a lot of languages for something entirely different, usually to indicate a predicate/lambda expression.) It's more a math concept. Not that you are wrong, it just seems like your trying to go a bit too low level; It would sort of be like describing the history of how we came to write the various math symbols and numbers as they are now "well, we started with roman numerals..." type thing)
Another example is ++ and +=. e.g.: x + 1; can be written shorter as x++, because the '++' operator does the same thing as '+1'. x += 1; does also the same thing because the long version of that is: x = x + 1;
depends entirely on the language; ++, --, as well as the operator= assignments aren't supported universally in all languages.
in C and it's variants, the ++ operator is both a postfix as well as a prefix operator, you can say:
y=12;
x=y++;
z=++y;
printf("%d,%d,%d",x,y,z);
which will print:
13,14,13
the first statement of course initializes y to 12, the second increments y and stores that in x (13), and the last stores y in z (13), and then increments it (to 14).