Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views36 pages

01B Syntax I (Final)

Uploaded by

sgacct85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views36 pages

01B Syntax I (Final)

Uploaded by

sgacct85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Syntax I Built-in Types I/O Code Structure Operators

Syntax I
Syntax I Built-in Types I/O Code Structure Operators

Introduction to C++
C++ is a programming language. It is in fact a…
● General-purpose,
● Object-oriented,
● And compiled
language.
While Python is probably the more popular language today (and much
easier to read, it is slower than C++.
That’s why we use C++ for IO problems – because it is pretty darn
fast…
Syntax I Built-in Types I/O Code Structure Operators

Brief Compiler Theory

C++ code that u Gets compiled Into computer-readable


write by a compiler machine code

Which your CPU can


execute
Syntax I Built-in Types I/O Code Structure Operators

(Fundamental) Types
In C++, a type represents a class of values with the same representation. Some common types
include:

Type Description Syntax

Booleans Holds values of either True or False bool

Characters Holds single characters char

Integers Holds integral values (that can be positive int, long long, unsigned int,
or negative) unsigned long long

Floats / floating Holds real values: float, double, long double


points

Strings Holds a “string” of multiple characters std>:string, char*

Voids Types with no values (rarely seen, mostly void


for functions)
Syntax I Built-in Types I/O Code Structure Operators

(Fundamental) Types
Built-in types have standard representations in C++ Code.

Type Description Example

Booleans Holds values of either True or False true, false

Characters Holds single characters ‘X’

Integers Holds integral values (that can be positive 5, 0x5A, 077, 0b1011, 5LL
or negative)

Floats / floating Holds real values 0.5f, 0.5


points

Strings Holds a “string” of multiple characters “Hello World”


Syntax I Built-in Types I/O Code Structure Operators

Integer Types

Different integer types can store different ranges of values. In general, types
are defined by two characteristics:
1. Width: the number of bits (the “size” of the number). Typically a power
of 2: ranges from 8 to 64

2. Signedness: signed types can store +ve and -ve numbers. Unsigned
types can store only nonnegative numbers.
Syntax I Built-in Types I/O Code Structure Operators

Integer Types

The minimum and maximum values for each data type are:

Width Signedness Min Value Max Value C++ Type Name

32 bits Signed -231 ≈ -2B 231-1 ≈ 2B int

32 bits Unsigned 0 232-1 ≈ 4B unsigned int

64 bits Signed -263 ≈ -9×1018 263-1 ≈ 9×1018 long long

64 bits Unsigned 0 264-1 ≈ 1.8×1019 unsigned long long


Syntax I Built-in Types I/O Code Structure Operators

Integer Types

The minimum and maximum values for each data type are:

Width Signedness Min Value Max Value Example

32 bits Signed -231 ≈ -2B 231-1 ≈ 2B 5, -5

32 bits Unsigned 0 232-1 ≈ 4B 5U

64 bits Signed -263 ≈ -9×1018 263-1 ≈ 9×1018 10LL, -10LL

64 bits Unsigned 0 264-1 ≈ 1.8×1019 10ULL


Syntax I Built-in Types I/O Code Structure Operators

Declaring Variables

A variable stores a value of a pre-selected data type. As the name implies -


it can be modified.

To declare a variable, use the following syntax:


data_type variable_name = initial_value;

Variables can also be declared without an initial value. However, you should
not use uninitialised variables until they are assigned a value.
data_type variable_name;
Syntax I Built-in Types I/O Code Structure Operators

Using Variables

To assign a value to a variable, use the following syntax:


variable_name = new_value;

Having declared a variable, you will then be able to use


data_type variable_name;
Syntax I Built-in Types I/O Code Structure Operators

Pairs

Pairs are a template type, where you can configure the types of constituent
values:

You can change <int, int> to any other types, including other template types!

pair<int, int> p = {5, 6};


std::cout << p.first << "\n"; // 5
std::cout << p.second << "\n"; // 6
p.first = 7;
std::cout << p.first << "\n"; // 7
Syntax I Built-in Types I/O Code Structure Operators

Pairs

To use pairs, you will need the <utility> library - we will explain how to
include this library later.

You can change <int, int> to any other types, including other template types!

pair<int, int> p = {5, 6};


std::cout << p.first << "\n"; // 5
std::cout << p.second << "\n"; // 6
p.first = 7;
std::cout << p.first << "\n"; // 7
Syntax I Built-in Types I/O Code Structure Operators

Using Variables

To assign a value to a variable, use the following syntax:


variable_name = new_value;

Having declared a variable, you will then be able to use


data_type variable_name;
Syntax I Built-in Types I/O Code Structure Operators

Introduction to I/O

Programming languages allow user input and interaction with the program
using Input/Output (I/O) utilities.
- Think of standard I/O as taking input from the keyboard and printing out
onto the screen
- Alternatively, file I/O allows a program to read/write to a file
- Note: standard I/O also reads/writes to a special set of files known as device
files to achieve the behaviour as described above
Syntax I Built-in Types I/O Code Structure Operators

#include preprocessor command

To use I/O functions, we must use libraries provided by the C++ Standard
Template Library (STL). To do this, we use the #include preprocessor
command. There are two variants of the command, as follows:

#include <library> // will check the STL for the file

#include “file” // will check the local file path, then STL for the file
Syntax I Built-in Types I/O Code Structure Operators

Standard I/O

To read and write to standard I/O, we use the iostream library:


#include <iostream>

However, you can also do the following to include most of the C++ STL:
#include <bits/stdc++.h>
Note: not advisable for most purposes, as it adversely affects compilation time. You should
only use this for competitive programming.
Syntax I Built-in Types I/O Code Structure Operators

Standard I/O

Standard input and output is achieved using the std::cin and std::cout
streams. See the code below:

int a;

std::cin >> a; // remember the semicolon. Reads an integer into `a`.

std::cout << a; // Print the integer stored in `a`.


Syntax I Built-in Types I/O Code Structure Operators

Standard I/O: New Lines

To print a newline, you can use either std::endl or the newline character
‘\n’.

std::cout << a << std::endl;


std::cout << a << '\n';
Syntax I Built-in Types I/O Code Structure Operators

Standard I/O: New Lines

endl and ‘\n’ do not produce the same result:

endl forces all output preceding it to be printed immediately.


‘\n’ does not do this - it might wait for more output before printing.

Consequently, it is generally accepted that ‘\n’ is faster - and hence more strongly
recommended for competitive programming - than endl.

However, this property of guaranteeing all prior output will be printed may not be a
bad thing
- When debugging a program which may crash, debugging output should use endl as a
crashing program will not print the output that is “waiting” for more output.
Syntax I Built-in Types I/O Code Structure Operators

The using keyword

In competitive programming, it is undesirable to continually have to retype


the std:: namespace. Hence, we use the using keyword as follows:

using std::cout; // you can use cout instead of std::cout


using namespace std; // you can omit std:: for the rest of the program

In competitive programming, you should include using namespace std; at


the top of your program - however, for most purposes it is not advisable as
it harms code readability.
Syntax I Built-in Types I/O Code Structure Operators

Basic Code Structure

In C++, the entry point of a program is not the top of the file - instead, it is
a function named main with an int return type.

Note: sometimes, you might not be able to use int as the type to return
due to other code tricks we will teach later. Alternatives include signed and
int32_t.
Syntax I Built-in Types I/O Code Structure Operators

First Program: Hello World

Following good programming practice, we can print “Hello World” with the
following code:

#include <iostream>

int main() {
std::cout << "Hello World" << std::endl;
}
Syntax I Built-in Types I/O Code Structure Operators

First Program: Hello World

However, we can also write the code as follows, using all the “tricks” we
have covered:

#include <bits/stdc++.h>
using namespace std; Practice Problem(s):
[cbr] helloworld
int main() {
cout << "Hello World" << '\n';
}
Syntax I Built-in Types I/O Code Structure Operators

Template

You can use this code as a template going forward.

#include <bits/stdc++.h>
using namespace std;

int main() {

}
Syntax I Built-in Types I/O Code Structure Operators

Introduction to Operators

Operators are classified based on their number of inputs


- Unary operators accept one input, binary two, ternary three, etc

We can also classify operators based on their utility:


- Arithmetic
- Comparison
- Logical
- Binary
- Assignment
Syntax I Built-in Types I/O Code Structure Operators

Arithmetic Operators

The binary arithmetic operators are:

Type Operator Symbol Example

Binary Addition + 5 + 5 >/ 10

Subtraction - 6 - 3 >/ 3

Multiplication * 3 * 3 >/ 9

Division / 6 / 3 >/ 2

Modulo / Integer Remainder % 7 % 3 >/ 1


Syntax I Built-in Types I/O Code Structure Operators

Arithmetic Operators

int x = 2, y = 9;
cout << (x+y) << '\n'; //outputs 11
cout << (y/x) << '\n'; //outputs 4
cout << (y%x) << '\n'; //outputs 1

double a = 2, b = 9;
cout << (a/b) << '\n'; //outputs 4.5

C++ integer division [int/int] will yield int values, truncated towards 0. Therefore, 4.5
becomes 4, -2.6 becomes -2. To obtain floating-point values, at least one operand in your
division operation should be a floating-point type [double/int, int/double or double/double]
Syntax I Built-in Types I/O Code Structure Operators

Arithmetic Operators

The unary arithmetic operators are

Type Operator Symbol Example

Unary Negation - -x

Prefix Increment: Set x to x+1 >+ >+x

Postfix Increment: Set x to x+1 >+ x>+

Prefix Decrement: Set x to x-1 >- >-x

Postfix Decrement: Set x to x-1 >- x>-


Syntax I Built-in Types I/O Code Structure Operators

Prefix vs Postfix Operators

Prefix and postfix describe the position of the operator relative to the
argument: prefix operators are before the operator, postfix operators after.
In C++, prefix and postfix increment/decrement have different behaviour, as
seen below:

int x = 3, y = 3;
cout << (x++) << '\n'; // outputs 3
cout << (++y) << '\n'; // outputs 4
cout << x << “ “ << y << '\n'; // outputs 4 4
Syntax I Built-in Types I/O Code Structure Operators

Comparison Operators

Comparison operators are all binary, as there must be two values to


compare between. Returns boolean values (true/false).
Type Operator Symbol

Binary Equality ==

Inequality !=

Greater than >

Greater than or equal to >=

Less than <

Less than or equal to <=


Syntax I Built-in Types I/O Code Structure Operators

Logical Operators

Operates on boolean values (true / false).

Type Operator Example

Unary Logical Negation !a

Binary Logical AND a && b

Logical OR a || b
Syntax I Built-in Types I/O Code Structure Operators

Assignment Operators

For most binary non-comparison operators, there is a corresponding


assignment operator. For example:

int x = 3, y = 3;
x += y; // equivalent to x = x + y, x will have a value of 6 now

The operator symbol is achieved by appending an equals sign to the back of


the base operator.
Syntax I Built-in Types I/O Code Structure Operators

Binary Operators

In binary math, we write the operands in base 2 instead of base 10.

Example:
5 = 22 + 20 = 1012
15 = 23 + 22 + 21 + 20 = 11112

Extra Reading: Negative numbers in binary with 2’s complement


(https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html)
Syntax I Built-in Types I/O Code Structure Operators

Binary Operators

Binary operators act on individual bits in the binary representation.

Example: the binary negation (~) operator flips every bit in the number
(typically 32 or 64).

Therefore, if a = 10 (using a 16-bit example):


~a : 0000 0000 0000 1010
~a : 1111 1111 1111 0101
Syntax I Built-in Types I/O Code Structure Operators

Binary Operators

The bitwise operators are as follows. Each binary operator also has a
corresponding assignment operator (a &= b, a |= b, etc.)
Type Action on bits Symbol Example

Unary Negation: flip bits from 0 to 1 and 1 to 0 ~ ~a

Binary Bitwise AND: bit is only 1 if both inputs are 1 & a & b

Bitwise OR: bit is 1 if either input is 1 | a | b

Bitwise XOR: bit is 1 if exactly one bit is 1 ^ a ^ b

Left shift: moves every bit in a to the left by b bits >< a >< b

Right shift: moves every bit in a to the right by b bits >> a >> b
Syntax I Built-in Types I/O Code Structure Operators

Binary Operators

int x = 3, y = 6;
cout << (x&y) << '\n'; //011&110=010, outputs 2
cout << (x|y) << '\n'; //011|110=111, outputs 7
cout << (x^y) << '\n'; //011^110=101, outputs 5
cout << (x<<1) << '\n'; //011 shifted by 1 is 110, outputs 6
cout << (x>>1) << '\n'; //011 shifted by 1 is 001, outputs 1

// Observe that shifting left b bits is equivalent to multiplying by 2^b


and shifting right is equivalent to dividing by 2^b and rounding down

You might also like