cs31s11dis3

# cs31s11dis3 - CS31: Introduction to Computer Science I...

This preview shows pages 1–3. Sign up to view the full content.

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 More on Arithmetic Expressions The following two are equivalent: x = x + 5 ; x + = 5 ; The above expressions add 5 to x and store the increased amount back into x . Similarly, you can do: y = y - 10; or y -= 10; z = z * (x + 2); or z *= x + 2; The following is erroneous. Do you see why? x = 10; y /= x - 10; There is yet another shortcut for incrementing/decrementing a value. The following are equivalent: x = x + 1 ; x + = 1 ; x + + ; + + x ; / / i n c r e m e n t b y 1 y = y - 1 ; y - = 1 ; y - - ; - - y ; / / d e c r e m e n t b y 1 The incremental operator ++ is the reason behind the naming of language “C++,” as C++ started as the “extention” of the language C. (Can you guess the name of the predecessor of C?) (Maybe a little tricky) ++ and -- behave slightly differently based on their location. See the following: x = 5; y = x + + ; c o u t < < y < < e n d l ; / / I t p r i n t s : This is an assignment, so the first step is to “evaluate” the right hand side expression and the next step is to assign the evaluated value to the variable on the left hand side. The expression “ x++ ” evaluates to the original value of x . On the contrary, when the variable comes after ++ , it evaluates to the resulting value. x = 5; y = + + x ; c o u t < < y < < e n d l ; / / I t p r i n t s : Use this kind of increment-assignment combination only if you fully understand what is going on. It is wise to break it into two statements when it is not obvious what you are doing. The Type bool Remember, boolean expressions are expressions that evaluate to either true or false. Just like integers can be stored in int variables and real numbers can be stored in double variables, true and false values can be stored in bool variables. For instance, the following are equivalent: if (x > 5) { cout << "x is greater than 5" << endl; } and CS31: Introduction to Computer Science I Spring 2011 Copyright Brian Choi 2011. All Rights Reserved. Week 3, Page 1/10

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
bool test = x > 5; if (test) { cout << "x is greater than 5" << endl; } bool ’s can be handy when you have to evaluate the same condition multiple times. They let you evaluate the expression once and for all, as in: bool test = x > 5; // test is true if x is greater than 5 at this moment. if (test && x < 10) // true if x is greater than 5 and less than 10. { // do something } if (test || x < 0) // true if x is greater than 5 or less than 0. { // do something else } Nested If Statements Recall that an if statement takes the following form (“else” part omitted): if (condition) { // if body } This is a single if statement, and both if and else bodies may consist of multiple statements. The following if statement has two statements in the if body. if (x > 5)
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

### Page1 / 10

cs31s11dis3 - CS31: Introduction to Computer Science I...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online