Week 2 - Boolean Expressions and Conditionals

Learning Objectives

Upon finishing this learning module, you should be able to:

  • Use library functions to do math
  • Write a program using conditional expressions
  • Use operators to manipulate boolean values

Day 1

Day 2

Day 3

Day 4

Multiple File Sample Projects

If you look at the sample code for week 2 from the CS161 git repository, you will notice that some project folders have extra text files with the extension ".bak" after the filename.

A  project folder with .cpp.bak files

The .bak files have alternative samples. Rather than make 3 different projects to show off 3 closely related samples, there is just one project with the different samples stored in those "backup" files.

The .bak files will not be compiled. To switch which sample you are building and running, simply copy all the code from one of the .bak files and paste it into the main.cpp file, replacing the code that was there.

CPPLab

We will be using a website called CPP Lab to be getting programming practice.

Follow this guide to get logged into CPPLab and learn about it.

Some of the problems are easy, some are hard. Your goal should be to attempt all the problems in each problem set and, hopefully, complete all of them. But if you get stuck, don't spin your wheels for hours trying to solve a particular problem. Move on to something else and get help with that one.

Using Math Functions

Read Ch 4.1 and 4.2. These videos explain some of the basics:

Do Numeric Functions CPP Lab .

Basic Conditionals

  • Read Ch 3.1-3.4. This video introduces some of the key concepts:

  • Watch this video on the concept of Scope:

  • Do Conditionals 1 CPP Lab

Random Numbers

Read 3.9 on random numbers. This video explains some of the basics:

Complex Conditionals

  • Read Ch 3.5-3.8. This video introduces some of the ideas:

  • This video demonstrates using the debugger to trace your way through the computer tax example:

  • Check out the assignment - you should know enough to do it at this point. The rest of this week's material has tricks you might choose to use, but do not need.

  • Do Conditionals 2 CPP Lab .

Boolean Operators

  • Read Ch 3.10-11 about the Boolean Operators || and &&. Although you can solve any problem without them, they are important tools for writing cleaner logic. This video covers them:

  • Do the Boolean Practice worksheet (Remember that the Files link in Elearn/Canvas is where you can access this and future worksheets).

Other Conditionals

  • Read Ch 3.13-16 about switch and the ? : operator. You should recognize them if you see them, but should not worry about using them for now - they are special-purpose tools. Focus on writing code using the other ideas from the week before worrying about mastering these "extra" tricks.