Previous Lecture lect03 Next Lecture

lect03, Tue 04/09

Variables and types, expressions, control structures (if-else), output

CSIL

This is the guide on how to SSH into CSIL, so you can work from your laptop away from any COE computers https://ucsb-cs16.github.io/topics/ssh_connect/

Github Repos

This segment only serves as a demo/reference, and does not represent the full features of Github

C++ Variables and Types

PRO TIP: initialize variable to a value that doesn’t make sense, so you can determined when it is not being set to the expected value later in the code. For example, if 0 was an acceptable value, then you may not notice there was an error.

PRO TIP / Best practice: make your variable names relevant to what they do (random names for your variables is a big NO-NO). Naming them with something that indicates what the variable is used for makes your code mode readable and maintainable.

C++ shortcut: To increment the value of a numeric variable by 1, simply add ++ to it. E.g.,

                    int count = 7;
                    count++; //count is now 8!

which is the shortcut for

                    int count = 7;
                    count = count + 1; //count is now 8!

also a shortcut

                    int count = 7;
                    count += 1; //count is now 8!

Initializing, Assigning, and Modifying Variables

int x;      // declare variable x of type int
int y, z;   // declare variables x and y in one statement
x = 10;     // assign x to an integer value 10.

int a = 10;   // declare and initialize (i.e., assign its initial value) in one statement
int b = 20, c = 30;

b = 6 + 4;

cout << a << "," << b << "," << c << "," <<
x << "," << y << "," << z << endl;
//which means
int x = 12.345;
cout << x << endl;  //will print out 12
double x = 1.2;
double y = 2.3;
int z = x + y;
cout << z << endl;  //will print out 3

Boolean Expressions

==  // true if two values are equivalent
!=  // true if two values are not equivalent
< // true if left value is less than the right value
<=  // true if left value is less than OR EQUAL to the right value
> // true if left value is greater than the right value
>=  // true if left value is greater than OR EQUAL to the right value
bool x = 5 == 1;  // x = 0
bool x = 3 != 2;  // x = 1
!   // inverts true to false or false to true
&&  // boolean AND; Returns false if either expression is false, e.g., "false && true" returns false; otherwise, returns true
||  // boolean OR; Returns true if either expression is true, e.g., "false && true" returns true; otherwise, returns false
bool x = true;
bool y = true;
x = !x;     // x = false
x = x && y    // x = false
x = x || y    // x = true

Control Structures

If-else statements

if (BOOLEAN_EXPRESSION) {
  // code1
} 
else {
  // code2
}
int x = 4;
if ((x > 3) && (x < 6)) {
  cout << "x is either 4 or 5" << endl;
} 
else {
  cout << "x is not 4 or 5" << endl;
}
int x = 4;
if ((x > 3) && (x < 6))
  cout << "x is either 4 or 5" << endl;
else
  cout << "x is not 4 or 5" << endl;
// Will have the same output as the last statement.

int x = 6;
if ((x > 3) && (x < 6))
  cout << "1" << endl;
  cout << "2" << endl; // outside if block
  cout << "3" << endl; // outside if block

Multi-way If-else Statements

int x = 3;
if (x == 1)
  cout << "x equals 1" << endl;
else if (x == 2)
  cout << "x equals 2" << endl;
else if (x == 3)
  cout << "x equals 3" << endl;
else
  cout << "x does not equal 1, 2, or 3" << endl;

Remember to surround all code blocks with braces! We prefer that you write the above code block as shown below. This way, if you ever need to add another line of code to a block, you won’t have to remember to add the curly braces, they will already be there.

int x = 3;
if (x == 1) {
  cout << "x equals 1" << endl;
}
else if (x == 2) {
  cout << "x equals 2" << endl;
 }
else if (x == 3) {
  cout << "x equals 3" << endl;
}
else {
  cout << "x does not equal 1, 2, or 3" << endl;
}

Vim navigation trick

Having to alternate between vim and Unix by repeatedly typing :q and vim [filename] can sometimes get tedious. You can temporarily push Vim to the background so that you can use the command line.

To do so, make sure that you are in the “Command mode” for Vim.

Pressing Ctrl key followed by z (usually written in instructions as Ctrl+z) will put the Vim window in the background, giving you back the command-line prompt.

Type fg and press Enter, to get back into Vim (fg stands for “foreground”).

w and v in Vim will jump the cursor from the next and previous word, respectively, while in “Command mode”.

Shortcut of the day

ll can be the same as ls -l, if you type alias ll='ls -l' on the command line (it will be remembered until you close that session).

Personally, I use alias ll='ls -lFG'.

Reminder

Remember to practice syntax (include semicolons, pound signs, etc.). Observing in-class code is not sufficient, review the lab and textbook examples to practice programming and better prepare for quizzes and tests. When writing programs, try refine them to be more efficient (eliminate repetitive code when possible).

Practice Questions

  1. What is the following output of this code snippet? Assume it’s implemented in a working C++ program.
    int x;
    cout << x << endl;
    
  2. What is the following output of this code snippet? Assume it’s implemented in a working C++ program.
    double y = 1.2;
    int x = 3.5 + 2.4 + y;
    y += x;
    cout << x << endl;
    cout << y << endl;
    
  3. Exclusive-or (xor) is defined as the following on two boolean values:
    xor returns true only if one of the boolean value is true
    

    Write a logical statement in C++ for two boolean values x and y, to represent xor by using a combination of only !, &&, ||