C Handouts 4
C Handouts 4
C Handouts 4
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
C++ Operators
• Operators are used to perform operations on variables and values.
• In the example below, we use the + operator to add together two
values:
Example
Arithmetic Operators
• Arithmetic operators are used to perform common mathematical
operations.
Assignment Operators
• Assignment operators are used to assign values to variables.
• In the example below, we use the assignment operator (=) to
assign the value 10 to a variable called x:
Example
int x = 10;
1 | Page
COMPUTER PROGRAMMING PART 4
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int x = 10;
x += 5;
Comparison Operators
• Comparison operators are used to compare two values (or
variables). This is important in programming, because it helps us
to find answers and make decisions.
• The return value of a comparison is either 1 or 0, which means
true (1) or false (0). These values are known as Boolean values,
and you will learn more about them in the Booleans and If..Else
chapter.
• In the following example, we use the greater than operator (>) to
find out if 5 is greater than 3:
Example
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
Logical Operators
• As with comparison operators, you can also test for true (1) or
false (0) values with logical operators.
• Logical operators are used to determine the logic between
variables or values:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The if Statement
Use the if statement to specify a block of C++ code to be executed if
a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Example
3 | Page
COMPUTER PROGRAMMING PART 4
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Example
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
Example explained
In the example above we use two variables, x and y, to test whether x
is greater than y (using the > operator). As x is 20, and y is 18, and
we know that 20 is greater than 18, we print to the screen that "x is
greater than y".
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
Example explained
In the example above, time (20) is greater than 18, so the condition
is false. Because of this, we move on to the else condition and print
to the screen "Good evening". If the time was less than 18, the
program would print "Good day".
4 | Page
COMPUTER PROGRAMMING PART 4
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
Example
Example explained
In the example above, time (22) is greater than 10, so the first
condition is false. The next condition, in the else if statement, is
also false, so we move on to the else condition since condition1 and
condition2 is both false - and print to the screen "Good evening".
However, if the time was 14, our program would print "Good day."
Syntax
Instead of writing:
Example
5 | Page
COMPUTER PROGRAMMING PART 4
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Example
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
6 | Page
COMPUTER PROGRAMMING PART 4
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
break;
case 7:
cout << "Sunday";
break;
}
// Outputs "Thursday" (day 4)
The break Keyword
When C++ reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the
block.
When a match is found, and the job is done, it's time for a break.
There is no need for more testing.
Example
int day = 4;
switch (day) {
case 6:
cout << "Today is Saturday";
break;
case 7:
cout << "Today is Sunday";
break;
default:
cout << "Looking forward to the Weekend";
}
// Outputs "Looking forward to the Weekend"
Loops are handy because they save time, reduce errors, and they make
code more readable.
Syntax
while (condition) {
// code block to be executed
}
In the example below, the code in the loop will run, over and over
again, as long as a variable (i) is less than 5:
7 | Page
COMPUTER PROGRAMMING PART 4
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Example
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
Syntax
do {
// code block to be executed
}
while (condition);
The example below uses a do/while loop. The loop will always be
executed at least once, even if the condition is false, because the
code block is executed before the condition is tested:
Example
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
Syntax
Statement 3 is executed (every time) after the code block has been
executed.
8 | Page
COMPUTER PROGRAMMING PART 4
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Example
Example explained
Statement 1 sets a variable before the loop starts (int i = 0).
Statement 2 defines the condition for the loop to run (i must be less
than 5). If the condition is true, the loop will start over again, if
it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in the
loop has been executed.
Another Example
This example will only print even values between 0 and 10:
Example
Nested Loops
The "inner loop" will be executed one time for each iteration of the
"outer loop":
Example
// Outer loop
for (int i = 1; i <= 2; ++i) {
cout << "Outer: " << i << "\n"; // Executes 2 times
// Inner loop
for (int j = 1; j <= 3; ++j) {
cout << " Inner: " << j << "\n"; // Executes 6 times (2 * 3)
}
}
Syntax
9 | Page
COMPUTER PROGRAMMING PART 4
LECTURE HANDOUTS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Example
*Note: Don't worry if you don't understand the example above. You will
learn more about arrays in the C++ Arrays chapter.
10 | P a g e