CSC121 Module 6
CSC121 Module 6
Basic Programming
Computer programming is the process of writing instructions that are to be
executed by computers. These written instructions are often called “code,” as
they are written in one of several special programming languages which the
computer can understand. Examples of programming languages include;
python, java, C++, C, etc.
1
• C++ can be found in today's operating systems, Graphical User
Interfaces, and embedded systems.
• It is used in systems programming, game development, and more.
• A text editor.
• A compiler, like GCC, to translate the C++ code into a language
that the computer will understand.
There are many text editors and compilers to choose from (e.g., Block::Code,
Visual Studio Code, Sublime Text etc).
2
Locate and double click on “codeblocks-20.03-setup”. This will open a
user control dialog box, asking you to click on yes if you want to
continue installation
Click on Yes, and click next again
Read the license agreement term and click on “I agree”
Follow “next” and “Yes” to finish installation
This will display a Code Block welcome page as shown below;
The C++ language also has its syntax for the functionalities it provides.
Different statements have different syntax specifying their usage but C++
3
programs also have basic syntax rules that are followed throughout all the
programs.
The basic syntax of a C++ program consists of various elements and rules that
must be followed to write a valid C++ program. Here is a breakdown of the
key components of the basic syntax from the „Hello CSC121 Class‟ below:
4
Statements: Inside the main function, you write statements that are
executed sequentially. Statements are instructions that perform actions
like input, output, calculations, and more.
std::cout << "Hello, World!" << std::endl;: This is an example of an
output statement. std::cout is used to print text to the console. << is
used for outputting, and << std::endl is used to insert a newline after the
text.
return 0;: The return statement is used to exit the main function and
return a status code to the operating system. A return value of 0
typically indicates a successful execution, while non-zero values can
indicate errors.
Semicolons (;): In C++, statements must end with a semicolon (;). It
tells the compiler that a statement is complete.
Operators
Once introduced to variables and constants, we can begin to operate with
them by using operators. What follows is a complete list of operators. At this
point, it is likely not necessary to know all of them, but they are all listed here
to also serve as reference.
x = 5;
This statement assigns the integer value 5 to the variable x. The assignment
operation always takes place from right to left, and never the other way
around:
x = y;
5
This statement assigns to variable x the value contained in variable y. The
value of x at the moment this statement is executed is lost and replaced by the
value of y.
Consider also that we are only assigning the value of y to x at the moment of
the assignment operation. Therefore, if y changes at a later moment, it will not
affect the new value taken by x
Arithmetic operators ( +, -, *, /, % )
The five arithmetical operations supported by C++ are:
operator description
+ addition
- subtraction
* multiplication
/ division
% modulo
x = 11 % 3;
Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=)
Compound assignment operators modify the current value of a variable by
performing an operation on it. They are equivalent to assigning the result of
an operation to the first operand:
6
Operator Meaning Example Result
Adds the value on the right to the variable on the left c = a +=
+= and assigns the result back to the left variable. b c=7
Subtracts the value on the right from the variable on c = a -=
-= the left and assigns the result back to the left variable. b c=3
Multiplies the variable on the left by the value on the c = a *=
*= right and assigns the result back to the left variable. b c = 10
Divides the variable on the left by the value on the c = a /= c =
/= right and assigns the result back to the left variable. b 2.5
Performs a modulo operation on the variable on the
left and the value on the right and assigns the result c=a
%= back to the left variable. %= b c=1
Shifts the bits of the variable on the left to the right
by the number of bits specified on the right and c=a
>>= assigns the result back to the left variable. >>= b c=2
Shifts the bits of the variable on the left to the left by
the number of bits specified on the right and assigns c=a
<<= the result back to the left variable. <<= b c = 20
Performs a bitwise AND operation on the variable on
the left and the value on the right and assigns the c = a &=
&= result back to the left variable. b c=0
Performs a bitwise XOR operation on the variable on
the left and the value on the right and assigns the c = a ^=
^= result back to the left variable. b c=5
7
Expression Equivalent to...
y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
price *= units + 1; price = price * (units+1);
++x;
x+=1;
x=x+1;
These are all equivalent in its functionality; the three of them increase by one
the value of x.
In the early C compilers, the three previous expressions may have produced
different executable code depending on which one was used. Nowadays, this
type of code optimization is generally performed automatically by the
compiler, thus the three expressions should produce exactly the same
executable code.
Example 1 Example 2
x = 3; x = 3;
y = ++x; y = x++;
// x contains 4, y contains 4 // x contains 4, y contains 3
Relational and comparison operators ( ==, !=, >, <, >=, <= )
Two expressions can be compared using relational and equality operators. For
example, to know if two values are equal or if one is greater than the other.
The result of such an operation is either true or false (i.e., a Boolean value).
operator description
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Here there are some examples:
9
Logical operators ( !, &&, || )
The operator ! is the C++ operator for the Boolean operation NOT. It has only
one operand, to its right, and inverts it, producing false if its operand is true,
and true if its operand is false. Basically, it returns the opposite Boolean value
of evaluating its operand. For example:
The logical operators && and || are used when evaluating two expressions to
obtain a single relational result. The operator && corresponds to the Boolean
logical operation AND, which yields true if both its operands are true, and
false otherwise. The following panel shows the result of operator &&
evaluating the expression a&&b:
&& OPERATOR
(and)
a b a && b
true true true
true false false
false true false
false false false
10
The operator || corresponds to the Boolean logical operation OR, which yields
true if either of its operands is true, thus being false only when both operands
are false. Here are the possible results of a||b:
|| OPERATOR (or)
a B a || b
true true True
true false True
false true True
false false False
For example:
When using the logical operators, C++ only evaluates what is necessary from
left to right to come up with the combined relational result, ignoring the rest.
Therefore, in the last example ((5==5)||(3>6)), C++ evaluates first whether
5==5 is true, and if so, it never checks whether 3>6 is true or not. This is
known as short-circuit evaluation, and works like this for these operators:
operator short-circuit
if the left-hand side expression is false, the combined result
&&
is false (the right-hand side expression is never evaluated).
if the left-hand side expression is true, the combined result is true (the
||
right-hand side expression is never evaluated).
11