Previous Lecture lect03 Next Lecture

lect03, Tue 01/15

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

Used no slides for this lecture; mostly live coding.

Announcements

Exams

Exam dates have been posted on the calendar, your next midterm is in class on January 31st.

Midterm 1: January 31st; Midterm 2: February 28th; Final: March 21st

Verify that you have no conflict with the exams by the end of the week (otherwise, no possibility for makeup will be provided).

Midterm Preparation

As the first midterm draws near, make sure to take advantage of all resources available to you!

Gradescope:

Labs and homeworks will be submitted using Gradescope.

Labs

Autograder will be enabled for all labs so be sure to make sure your code passes all test cases before submitting to receive full credit. If your program is failing the autograder and you are absolutely sure that the program does what it is supposed to, post any issues you may have on piazza so potential fixes can be made to the autograder if needed.

Fill out the google form for partners if you have not done so (MANDATORY) https://goo.gl/forms/ouvjRmS2Gmcr0g6a2

Syllabus outlines guidelines for partner work, be sure to follow them.

Homework

Homework will be turned in through Gradescope.

Can turn in either by:

Piazza:

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!

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;

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”).

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).

Files created during lecture

3:30 lecture

// undef.cpp

#include <iostream>

using namespace std;

int main() {
    int a = 42;
    int b = -42;
    double c = 3.14;
    char d = 'K';
    //cout << "The sum of a and b is " << a+b << endl;
    cout << "The sum of a = " << a << " and b = " << b;
    cout << "  is " << a+b << endl;
    cout << "c = " << c << endl;
    cout << "d = " << d << endl;

    if ((a == 42) && (d == 'k')) {
        cout << "It is " << a << d << endl;
    }
    else {
        if (d == 'K')
        {
            cout << "Prof. K?" << endl;
        }
        else {
        cout << "It is  NOT 42 :(" <<  endl;
        }
    }

    return 0;
}

6:30 lecture

// myfile.cpp

#include<iostream>

using namespace std;

int main() {
    int a = 42;
    int b = -42;
    double d = 3.14159;
    char c = 'N';
    //cout << "The sum of a and b is " << a+b << endl;
    cout << "The sum of a=" << a << " and b=" << b << " is ";
    cout << a+b ;
    cout << endl;
    cout << "Char c = " << c << endl;
    cout << "D is = " << d << endl;

    if (c == 'Y') {
        cout << "Hello, hitchiker!" << endl;
    }
    else if ((c == 'K') & (a == "K"))
    {
        cout << "Prof. K, welcome!" << endl;
    }
    else
    {
        cout << " Who are you?" << endl;
    }
    return 0;
}