Previous Lecture Lecture 2 Next Lecture

Lecture 2, Thu 06/28

Basic I/O, Variable Types, Boolean Expressions, Control Flow

Breaking down the Hello World Program

// hello.cpp
#include <iostream>

using namespace std;

int main() {
	cout << "Hello CS 16!" << endl;
	return 0;
}
#include <iostream>
using namespace std;
int main() { ... }
cout << "Hello CS 16!" << endl;
return 0;

Comments

Example

// single line comment

/* multi
line
comment
*/

C++ Variables and Types

Initializing, Assigning, and Modifying Variables

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

int a = 10;		// initialize and assign 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
* Combine boolean expressions using Logical Operators
!	// inverts true to false or false to true
&&	// boolean AND
|| 	// boolean OR
* Example
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
* The last two statements will always execute because it’s considered outside of the code block.
* A syntax error will appear if you try to insert an “else” after the statements since “else” can only be used after an if code 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;

Switch Statements

int a = 3;
switch (a) {
	case 1:
		cout << “a == 1” << endl;
		break;
	case 2:
		cout << “a == 2” << endl;
		break;
	case 3:
		cout << “a == 3” << endl;
		break; // remove this and notice code continues without break statement
	default:
		cout << “a != [1, 2, 3]” << endl;
}
int a = 3;
switch (a) {
	case 1:
	case 2:
		cout << “x == [1,2]” << endl;
		break;
	case 3:
		cout << “x == 3” << endl;
		break;
	default:
		cout << “x != [1,2,3]” << endl;
}
// Change to a = 1, remove 1st break statement, and see "x == 3" printed in this scenario.

Embedded if-else statements

int x = 7;
if (x >= 5) {
	cout << “x >= 5” << endl;
	if (x == 5) {
		cout << “x == 5” << endl;
	} else {
		cout << “x > 5” << end;
	}
}