‹header›
‹date/time›
Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level
‹footer›
‹#›
My description of whitespace and how it is used in C++ was confusing last time.
The purpose of whitespace in C++ is to make the code easier to read for the programmer. You could write your entire program on one line if you wanted but it would be impossible to read and understand.
Use whitespace liberally to make your code clearer.
My description of input and output was confusing last time.
My description of input and output was confusing last time.
My description of input and output was confusing last time.
My description of input and output was confusing last time.
My description of input and output was confusing last time.
My description of input and output was confusing last time.
After today’s class you will have seen all the elements needed to write this program. Basic mathematical operators, the “if” control structure, input and output, the sqrt() function.
Basically we saw how to use C++ can be used as a calculator.
This is the hardest part because if we find problems we have to reevaluate not only the “coding” phase but the design phase as well. The algorithm we developed in step one as well as our translation of that algorithm into C++ has to be thoroughly checked for mistakes. The longer and more complex the program we are writing the greater the percentage of development time that this type of debugging uses up.
You have already been through the “coding” and compilation phases when you copied down and compiled the addition program yesterday. Some of you also went though part a) of the debugging phase.
You can expect to go through the programming cycle numerous times. That is why it is important for you to become as familiar as possible with your development environment (which for most of you will be emacs). The more familiar you are with your environment the faster and more accurately you will be able to make changes to your source code, compile your program and address any errors.
Most of the time you spend on Project 1 will be spent looking for bugs. Once you have an idea of the correct algorithm for solving the problem implementing it in C++ shouldn’t take more than an hour or two. But you will spend a lot of time fixing typos and checking that your algorithm really does what you thought it should do. 90% of the time spent C++ programming is spent in the debugging phase.
12 days may look like a long time but you are probably going to need all of it. I would recommend that you have an outline of how your program is going to work at least by the end of the weekend. Then you can spend next week fixing errors and if necessary redesigning your algorithm.
Boolean operators like greater than or less than are called “comparators”
Be careful about confusing the == operator and the = assignment operator. This is a very common error and is surprisingly difficult to catch since the compiler will often not report an error.
The only time the && operator returns true is when both sides are true
“Or” always returns true unless both sides are false, this is not exclusive or “xor” which would not be true if both sides were true
Be careful about confusing the == operator and the = assignment operator. This is a very common error and is surprisingly difficult to catch since the compiler will often not report an error.
Be careful about confusing the == operator and the = assignment operator. This is a very common error and is surprisingly difficult to catch since the compiler will often not report an error.
When you insert a boolean value into the output stream (i.e. when you print it) true is printed as 1 and false is printed as 0.
The and operation occurs first due to precedence
So x && y is false since y is false
False || y is false since y is false
The program prints 0
x + z != w, x + z is 5 which is not equal to w (14). So the != operator returns true
x > 6 is not true since x is 6 so > returns false
The ! Operator takes the false from > and makes it true
The && requires both sides to be true which they are so the program prints 1
z does not equal w so == returns false
x <= 6 does equal 6 so <= returns true
False || true is true
So the program prints 1
Normally statements in C++ are executed in strict order from the top of the source file to the bottom. We can change this order of execution with control structures like the if statement.
What does this program print?
Since x > 5 is true the cout statement between the if statements braces is executed and y = 12 is printed. Then the statements after the if statement are executed so the program then prints x = 6.
What does this program print?
Since x < 5 is false the code between the if statements braces is not executed. The next statement to be executed is cout << “x = “ << x << endl; So the program prints x = 6
What does this program print?
x < 5 is false
y != 15 is true
false || true returns true
Since the if condition is true the code between the braces is executed and y = 12 is printed, then x = 6 is printed
What does this program print?
x < 5 is false
y != 15 is true
false || true returns true
Since the if condition is true the code between the braces is executed and y = 12 is printed, then x = 6 is printed