You are on page 1of 49

Introduction to Programming

Lecture 3
PROGRAMMING 1

Engr. Jennelyn P. Cabale


Tasks
Pre-task Dev C++

While-task Lecture Demonstration

Post-task Exercises

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 2


Objectives

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 3


Compilers,
Machine
Program interpreters, or C++
language
assemblers

a sequence of
designed to be a
instructions for a
compiled language
computer to execute the only one language programs built into
computers understand various programming
and that language applications that
consists of sets of translates high level
instructions made of languages to machine a set of tools are
allows the programmer ones and zeros level languages needed, known as the
to write efficient,
development toolchain,
structured, object-
whose core are a
oriented programs
compiler and its linker.

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 4


Console programs
easy to interact with, and
programs that use text to
generally have a
communicate with the
predictable behaviour
user and the
that is identical across all
environment
platforms

Integrated Development
simple to implement and
Environment (IDE)
are very useful to learn
generally integrates
the basics of a
several development
programming language
tools

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 5


• Here you have instructions on how to compile and run console programs
using different free Integrated Development Interfaces (IDEs):

IDE Platform Console programs


Compile console programs
Code::blocks Windows/Linux/MacOS
using Code::blocks
Compile console programs
Visual Studio Express Windows
using VS Express 2013
Compile console programs
Dev-C++ Windows
using Dev-C++

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 6


Structure of a Program
• Example # 1: SIMPLE PROGRAM

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 7


Example # 2 : THE SHORTEST C++ PROGRAMS

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 8


<< Insertion operator Output operator

The inserts objects into


the output stream
named on its left
stream

Output
Operator
Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 9
Example # 3 : THE HELLO WORLD PROGRAM AGAIN

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 10


Comments
Messages in the program that will be ignored by the
compiler but intended only to be read by humans.

Standard C comment
• Begins with the combination slash-star symbol /* and ends with the star-slash symbol */.
• Anything written between these symbol will be ignored by the compiler.
• /* This is a C style comment */

Standard C++ comment


• Begins with a double-slash / / and extends to the end of the line.
• // This is a C++ style comment

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 11


Example # 4 : USING THE TWO TYPES OF COMMENTS

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 12


Example # 5 : USING ONLY DOUBLE-SLASH COMMENTS

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 13


using all the elements in the standard

namespace C++ library are declared within


what is called a namespace: the
namespace std.

std
a program shall either qualify
each and every use of
elements of the, or introduce
visibility of its components.

using namespace std;

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 14


Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 15
Variable

A symbol that represents a storage location in the


computer’s memory.

The information that is stored in that location is called the


value of that variable.

The most common way that a variable obtains a value is by


means of an assignment. This has the syntax
variable = expression ;

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 16


Identifiers alignas, alignof, and, and_eq, asm, auto,
bitand, bitor, bool, break, case, catch,
char, char16_t, char32_t, class, compl,
const, constexpr, const_cast, continue,
A valid identifier is a Identifiers shall always
sequence of one or begin with a letter or an decltype, default, delete, do, double,
more letters, digits, or underline character (_) dynamic_cast, else, enum, explicit,
underscore characters
(_)
export, extern, false, float, for, friend,
goto, if, inline, int, long, mutable,
namespace, new, noexcept, not,
Spaces, punctuation
not_eq, nullptr, operator, or, or_eq,
marks, and symbols private, protected, public, register,
cannot be part of an reinterpret_cast, return, short, signed,
identifier
reserved for compiler-
sizeof, static, static_assert, static_cast,
specific keywords or struct, switch, template, this,
external identifiers
thread_local, throw, true, try, typedef,
In no case can they typeid, typename, union, unsigned,
begin with a digit.
using, virtual, void, volatile, wchar_t,
while, xor, xor_eq

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 17


Fundamental Data Types
Character types Numerical integer types Floating-point types Boolean type

• They can represent a • They can store a whole • They can represent real • The Boolean type,
single character, such number value, such values, such as 3.14 or known in C++ as bool,
as 'A' or '$'. as 7 or 1024. 0.01, with different can only represent one
levels of precision, of two states, true or
• The most basic type • They exist in a variety depending on which of false.
is char, which is a one- of sizes, and can either the three floating-point
byte character. be signed or unsigned, types is used.
depending on whether
• Other types are also they support negative
provided for wider values or not.
characters.

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 18


Declaration of a Variable

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 19


Initializing the Declaration
C-like Constructor Uniform
initialization initialization initialization
consists of appending an equal
introduced by the C++ language using curly braces ({})
sign followed by the value to
which the variable is initialized
encloses the initial value introduced by the revision of
between parentheses (()): the C++ standard in 2011
type identifier = initial_value;
type identifier (initial_value); type identifier {initial_value};

Example: int x = 0;
Example: int x (0); Example: int x{0};

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 20


Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 21
Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 22
Types of Variable

Local variables Formal parameters Global variables

•Known throughout
Located inside the entire program
Located inside and may be used by
the function
the main ( ) any piece of code
parameters
function
•Hold their values
during the entire
execution of the
program

•Declared outside
any function

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 23


Introduction to strings
Store sequences of
Compound type characters

Initialized with string mystring = "This is a string";


any valid string string mystring ("This is a string");
literal string mystring {"This is a string"};

Perform all the other


basic operations that
fundamental data
types can

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 24


Literals
• used to express particular values
Expressions with a within the source code of a program
fixed value
• Example:
•a=5; //5 is the literal constant

Literal constants can be


classified into: integer,

Constants floating-point, characters,


strings, Boolean, pointers,
and user-defined literals.

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 25


Not enclosed in or
any quotes other
Numerical constants special character
that identify integer
values.

Integer Simple succession of


digits representing a
Numerals whole number

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 26


Express real values with Can include either a decimal point, an e
decimals and/or character (that expresses "by ten at the Xth
exponents. height", where X is an integer value that
follows the e character), or both a decimal
point and an e character:

Floating
Point Default type for
floating-point
literals is double.
Numerals • 3.14159
• 6.02e23
// 3.14159
// 6.02 x 10^23
• 1.6e-19 // 1.6 x 10^-19
• 3.0 // 3.0

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 27


Character and string literals
Escape code Description
\n newline
Example:
Single character \r carriage return
• 'z'
• Enclosed between single
• 'p' \t tab
quotes (')
• "Hello world" \v vertical tab
• "How do you do?“
\b backspace
can also represent special characters form feed (page
that are difficult or impossible to \f
feed)
express otherwise in the source code
of a program, like newline (\n) or tab \a alert (beep)
String (\t). These special characters are all of
\' single quote (')
them preceded by a backslash
• Enclosed double character (\) \" double quote (")
quotes (")
\? question mark (?)
\\ backslash (\)

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 28


Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 29
Typed constant expressions

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 30


Preprocessor definitions (#define)

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 31


Assignment Operator

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 32


Chained Assignments

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 33


Simple Arithmetic Operators
• A symbol that operates on one or more expressions, producing a value
that can be assigned to a variable.
Integer Arithmetic Operators
Operator Description Example
+ Add m+n
- Subtract m-n
- Negate -n
* Multiply m* n
/ Divide m/n
% Remainder m%n

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 34


Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 35
Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 36
Compound Assignment Expressions

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

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 37


Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 38
Can be used
both as a prefix
The Increment
and suffix
and Decrement
Operators
Example 1 Example 2

x = 3; x = 3;
y = ++x; y = x++;

// x contains 4, y contains 4 // x contains 4, y contains 3

x++ or ++x both


have exactly the
same meaning but
different in
meaning

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 39


Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 40
Relational and Comparison Operators
operator description (7 == 5) // evaluates to false
(5 > 4) // evaluates to true
== Equal to
(3 != 2) // evaluates to true
!= Not equal to (6 >= 6) // evaluates to true
< Less than (5 < 5) // evaluates to false
> Greater than
Suppose a=2, b=3 and c=6
<= Less than or equal to (a == 5)
>= Greater than or equal to (a*b >= c)
(b+4 > a*c)
((b=2) == a)

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 41


Logical Operators

! - NOT. && - AND || - OR


Yields true if either of its operands
Only one operand, to its right, and Yields true if both its operands are true,
is true, thus being false only when both
inverts it and false otherwise.
operands are false.

True && True = TRUE True || True = TRUE


Returns the opposite Boolean value of True && False = FALSE True || False = TRUE
evaluating its operand. False && True = FALSE False || True = TRUE
False && False = FALSE False || False = FALSE

Example: Example: Example:(


!(5 == 5) (5==5) && (6==6) (5==5) || (3>6)
!(6 <= 4) (3<5) && (5>7) (7<=11) || (2<=5)
!true (6>=9) && (9<=4) (1>=3) || (4<5)

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 42


condition ?
Evaluates an expression, result1 : result2
returning one value if
that expression
evaluates to true,
otherwise to false

Conditional
Ternary 7==5 ? 4 : 3
Operator (?) 7==5+2 ? 4 : 35
>3 ? a : b
a>b ? a : b

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 43


Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 44
Comma
Operator (,)

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 45


Bitwise Operators (&, |, ^, ~, <<, >> )
• Bitwise operators modify variables considering the bit patterns that
represent the values they store.
operator asm equivalent description
& AND Bitwise AND
| OR Bitwise inclusive OR
^ XOR Bitwise exclusive OR
Unary complement (bit
~ NOT
inversion)
<< SHL Shift bits left
>> SHR Shift bits right

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 46


Explicit type casting Precedes the expression to be
operator converted by the new type enclosed
between parentheses (()):

Allows to convert a
value of a given
type to another
type

int i;
float f = 3.14;
i = (int) f;

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 47


Operator Precedence and Associativity
Operator Description Associativity Arity Example
- Negate Right Unary -n
* Multiply Left Binary m*n
/ Divide Left Binary m/n
% Remainder, Left Binary m%n
modulo
+ Add Left Binary m+n
- Subtract Left Binary m–n
Bit shift left,
<< Left Binary cout << n
output
Simple
= Right Binary m=n
assignment

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 48


THE END

Engr. Jen Perez-Cabale Lecture 3 - INTRODUCTION TO PROGRAMMING 51

You might also like