Learning Objectives:

Today's lab will continue our introduction to C++, covering more data types and control structures.

Software tools needed: web browser and the C++ compiler, gcc and a graphical editor, such as gEdit.

Download the Skeletal Notes and Focus Questions to guide you while studying this lab.
These are a useful tool for note taking and you can keep these handy to study for and refer to during the final exam.


At the end of this lab, don't forget to take Lab Quiz 13! See the quiz page for details of the work due this week.

Using Python, Gradescope, and Blackboard

See Lab 1 for details on using Python, Gradescope, and Blackboard.

Variables in C++

In C++ (and C, Java and many other languages), you must first state (or 'declare') a variable's name and its type before using. For example,
int num;
declares a variable, called num of type int (integer or whole number). If you do not request the space in advance in C++, you will get an error. This requirement (as well as the `typing' of variables) is to avoid errors where misspellings access the wrong variable and are never caught. It also makes the underlying design of the compiler (the program that converts your program to an executable file) simpler.

There are many types of variables availabe in C++. Here are a few that we will use:

(here is a chart with a few more common types).

Decisions in C++

As with Python, we can make decisions that take different branches through the code. The basic structure is:

if (condition)
The syntax of Python and C++ are very similar, but there's a few differences to keep in mind when translating from Python to C++.

Here's a sample program. Read through it first, and guess what it does:

//Name:  CSci 127 Teaching Staff
//Date:  November 2017
//A program demonstrating if-else statements in C++
#include <iostream>
using namespace std;

int main()
  int num;
  cout << "Hello!" << endl;
  cout << "Enter a number: ";
  cin >> num;
  if (num % 2 == 0)
    cout << "Even number!\n";
    cout << "Odd number\n";
  return 0;
Let's look in more depth at the code:

How would you modify this program to print out a different message based on the month? For example, if the user entered the month (as a number), you should print out "Happy Winter" if it is smaller than 3 or larger than 11, "Happy Spring" for 3 through 6, "Happy Summer" for 7 and 8, and "Happy Fall" otherwise. See Programming Problem List.

Indefinite Loops in C++

Now that we have discussed conditionals and definite loops, we can introduce indefinite loops. They follow a similar format to those in Python:

while (...condition...)
Again, we do not have the colon (':') at the end of the line with while, but we do end individual commands with a (';').

Take the Python code from Lab 10 that asks the user to enter their age (and continues checking until they enter a non-negative number):

and translate it into C++. An easy way to approach this is to start with the C++ program above that demonstrates if statements and modify it to handle the indefinite loop for checking ages. See Programming Problem List.

More Useful Unix Commands

Here are a few more commands, especially useful when you're using a public machine, and trying to figure out where things are and what is installed:

What's Next?

Now is a great time to take Lab Quiz 13! (15 minutes) It is directly based on Lecture and Lab 13.
After that, you can continue working on the programming assignments. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.
Keep in mind that the due dates are one week late for flexibility (if one week there is a setback and you can't submit your programs, you will have time catch up). Still, each week you should work on the programming assignments for that week, even if they are due a week later. If you are on a roll, you are welcome to work ahead!!!