0% found this document useful (0 votes)
1 views

CSC121 Module 6

Module 6 introduces basic programming concepts, focusing on the C++ programming language, which is versatile and widely used across various applications. It covers the setup of a C++ environment, including the necessary text editor and compiler, as well as the fundamental syntax and operators used in C++. The module also explains key programming constructs such as comments, the main function, and various types of operators including arithmetic, relational, and logical operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

CSC121 Module 6

Module 6 introduces basic programming concepts, focusing on the C++ programming language, which is versatile and widely used across various applications. It covers the setup of a C++ environment, including the necessary text editor and compiler, as well as the fundamental syntax and operators used in C++. The module also explains key programming constructs such as comments, the main function, and various types of operators including arithmetic, relational, and logical operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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.

For the purpose of this course “CSC121-Introduction to Problem Solving”,


we are considering C++ programming language. C++ is a widely-used
programming language known for its power and versatility. C++ is a general-
purpose programming language, which means it can be used for a wide range
of applications, from system software development to game development and
more.

Why do we learn C++?


Learning C++ is valuable for several reasons, depending on your interests and
career goals. Here are some compelling reasons to learn C++:

• C++ is a versatile and widely-used programming language.


• Learning C++ provides a strong foundation for other languages.
• C++ is fun and easy to learn!
• C++ is one of the world's most popular programming languages.
• C++ is portable and can be used to develop applications that can be
adapted to multiple platforms.
• C++ is an object-oriented programming language which gives a clear
structure to programs and allows code to be reused, lowering
development costs.

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.

Setting up C++ Environment


To start using C++, you need two things:

• 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).

To download Code Block‟

 Type the following address “https://www.codeblocks.org/” into address


bar of your computer browser
 Click on downloads from the left side code of the code block home
page
 Select " Download the binary release”
 Click on “Windows XP / Vista / 7 / 8.x / 10” if you are using any of the
windows operation system, otherwise, locate your appropriate
operating system
 Select, “codeblocks-20.03-setup.exe” from the windows file category
and choose the marrow side by the right side, from which to download
i.e FossHUB or Sourceforge.net. This will start your download
automatically.

From the download folder of your system

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;

 Click on “create new project”


 Select Empty project and type the name of the project, type in the
project name, click next and click finish.

Basic C++ Syntax


Syntax refers to the rules and regulations for writing statements in a
programming language. They can also be viewed as the grammatical rules
defining the structure of a programming language.

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:

 Comments: Comments are notes added to the code for documentation.


They begin with // for single-line comments or /* and end with */ for
multi-line comments. Comments are ignored by the compiler and are
only for the human reader.
 #include <iostream>: This line includes the standard C++ input/output
library (iostream). It provides functions like cin and cout for reading
input from the user and displaying output on the console.
 int main() { ... }: The main function is the entry point of a C++
program. It is a required part of every C++ program. The program
execution starts from here. The {} braces denote a block of code.

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.

Assignment operator (=)


The assignment operator assigns a value to a variable.

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

Operations of addition, subtraction, multiplication and division correspond


literally to their respective mathematical operators. The last one, modulo
operator, represented by a percentage sign (%), gives the remainder of a
division of two values. For example:

x = 11 % 3;

Results in variable x containing the value 2, since dividing 11 by 3 results in


3, with a remainder of 2.

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);

Increment and decrement (++, --)


Some expression can be shortened even more: the increase operator (++) and
the decrease operator (--) increase or reduce by one the value stored in a
variable. They are equivalent to +=1 and to -=1, respectively. Thus:

++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.

A peculiarity of this operator is that it can be used both as a prefix and as a


suffix. That means that it can be written either before the variable name (++x)
or after it (x++). Although in simple expressions like x++ or ++x, both have
exactly the same meaning; in other expressions in which the result of the
increment or decrement operation is evaluated, they may have an important
difference in their meaning: In the case that the increase operator is used as a
prefix (++x) of the value, the expression evaluates to the final value of x, once
8
it is already increased. On the other hand, in case that it is used as a suffix
(x++), the value is also increased, but the expression evaluates to the value
that x had before being increased. Notice the difference:

Example 1 Example 2
x = 3; x = 3;
y = ++x; y = x++;
// x contains 4, y contains 4 // x contains 4, y contains 3

In Example 1, the value assigned to y is the value of x after being increased.


While in Example 2, it is the value x had before being increased.

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).

The relational operators in C++ are:

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

You might also like