Srivaths P
C++ Basics
(Part 1)
Why you should prefer C++
(For Competitive Programming)
• Efficiency and Speed
• Most popular language for CP
• In-built Data Structures and Algorithms (STL)
Goal
To understand:
• Constants and datatypes in C++
• Input/Output in C++
• Various C++ operators
• Conditional statements
• Loops
Be able to write simple programs at the end, such as a
prime number checker.
Simplest C++ program
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!" << endl;
}
Constants in C++
• Integer constants: 4 | 62 | -90
• Decimal constants: 3.14 | 12.0 | 0.33333
• Character constants: 'f' | '5' | '~' | 'n'
• String literal: “Hello :D” | “MyP@ssw0rd123!”
Output in C++
To output a value, we use the cout operator as
follows: cout << value;
To print multiple values in the same line:
cout << value1 << value2 << value3;
To start printing in a new line: endl or ‘n’
Arithmetic operators in C++
Arithmetic Operators:
1) + Addition
2) - Subtraction
3) * Multiplication
4) / Division (Quotient)
5) % Modulo (Remainder)
NOTE: C++ follows the BODMAS rule
Variables
Variables are containers that stores specific types
of data. They can be modified with the
assignment operator “=”
Syntax: datatype variable_name = value;
Variables
Variable names cannot:
• Have spaces (use underscore instead)
• Start with a digit
• Be reserved by the compiler
• Already taken by another variable (in the same scope)
NOTE: Keywords/Variables are case sensitive
Datatypes
Datatypes are used to set the “type” of a
variable. For example, int is used to declare
integer variables.
Two types of datatypes:
• Primitive datatypes
• Derived datatypes
Common Primitive datatypes
1. int (long long int, unsigned int, etc.)
2. char
3. bool
4. float (double, long double)
5. Special type: void
Common Derived datatypes
1. string
2. vector
3. map
4. set
5. priority_queue
Arithmetic Assignment Operators
1. +=
2. -=
3. *=
4. /=
5. %=
Unary Operators
Operators that only need one value/operand
are called unary operators.
1. +
2. -
3. ++
4. --
Input in C++
To output a value, we use the cin operator as
follows: cin >> value;
To print multiple values in the same line:
cin >> value1 >> value2 >> value3;
NOTE: Each value must be separated by a space
or a new line when taking input.
Check your understanding - 1
1. How will you declare a character equal to
exclamatory mark?
2. Take an integer input, and output the value
multiplied by 7.
3. Take two values a, b as input, and output three
values: a+b and a*b and a/b
a/b should be a decimal, not an integer
Conditions and
Relational Operators
Conditions return a boolean value depending on whether
the expression is true or false.
Conditional operators:
==, !=
Relational operators:
<, >, <=, >=
Logical operators
Logical operators perform operations on boolean values or
expressions that result in Boolean values.
1. “(expr1) && (expr2)” checks whether BOTH are true.
2. “(expr1) || (expr2)” checks whether EITHER one is true.
3. “!(expr)” returns the OPPOSITE of the result of “expr”
The operators are called AND, OR, NOT operators respectively
Conditional statements
Conditional statements execute a different block of code
depending on the boolean value of a condition.
Syntax: if (condition) {
// something
} else if (another_condition) {
// something
} else {
// something
}
Check Your Understanding 2
1. Take input of 3 numbers x, y, z and output the maximum
using if statements
2. Given marks of a student, grade them from A to D
1. Between 0 and 30 -> D
2. Between 30 and 65 -> C
3. Between 65 and 90 -> B
4. Between 90 and 100 -> A
5. Output “Error” if less than 0 or greater than 100.
Loop
Loops are used to repeat a block of code until some
condition is satisfied.
There are three types of loops in C++:
1. for loop
2. while loop
3. do-while loop
Loop (Miscellaneous)
• An iteration is defined as one time the loop gets
executed. For example, 3rd iteration is the 3rd time the
loop is run.
• “break” statement exits the current/innermost loop
when executed.
• “continue” statement skips to the next iteration of the
current/innermost loop when executed.
“for” loop
Syntax:
statement1: Executed once before start of loop.
statement2: Condition of the loop. Loop exits if false.
statement3: Executed after each iteration.
for (statement1; statement2; statement3) {
// Code here
}
“while” loop
Syntax:
Check if the condition is true and then execute
the block of code. Repeat.
while (condition) {
// Code here
}
“do-while” loop
Syntax:
Execute the block of code and then check if
the condition is true. Repeat.
do {
// Code here
} while (condition);
Scope
A scope is a region of the program.
Every pair of curly braces creates a new
scope.
The variables inside the scope cannot be
used outside the scope.
Miscellaneous
• A loop inside another loop is called nested loops.
Syntax:
• Infinite loops are loops that run forever and never end
(when the condition is always true)
for (s1; s2; s3) {
for (s4; s5; s6) {
// Code here
}
}
goto statements
Goto/Jump statements are used to skip to another part of the
code.
Considered as bad practice to use goto statements except if it
used to exit from a nested loop.
Syntax:
label: // creates the label to skip to
goto label; // skips to the specific label
Check Your Understanding 3
1. Find the sum of the first N natural numbers (Using loops)
2. For the first N natural numbers:
If number is divisible by 3 and 5, print FizzBuzz
If number is divisible by 3, print Fizz
If number is divisible by 5, print Buzz
3. Print a N x M grid similar to the following:
1 2 3 4
5 6 7 8
9 10 11 12
Exercise
Write a program to take a number N as an input, and
output whether it is a prime number or not.
(Do not worry about efficiency)
Resources
• https://www.programiz.com/cpp-programming (learning C++ in general)
• https://www.programiz.com/cpp-programming#flow-control (if-else and loops)
• https://www.programiz.com/cpp-programming/nested-loops (nested loops)
• https://www.programiz.com/cpp-programming/goto (goto statements)
Thank you!

C_BASICS FOR C PROGRAMMER WITH SRIVATHS P

  • 1.
  • 2.
    Why you shouldprefer C++ (For Competitive Programming) • Efficiency and Speed • Most popular language for CP • In-built Data Structures and Algorithms (STL)
  • 3.
    Goal To understand: • Constantsand datatypes in C++ • Input/Output in C++ • Various C++ operators • Conditional statements • Loops Be able to write simple programs at the end, such as a prime number checker.
  • 4.
    Simplest C++ program #include<iostream> using namespace std; int main() { cout << "Hello world!" << endl; }
  • 5.
    Constants in C++ •Integer constants: 4 | 62 | -90 • Decimal constants: 3.14 | 12.0 | 0.33333 • Character constants: 'f' | '5' | '~' | 'n' • String literal: “Hello :D” | “MyP@ssw0rd123!”
  • 6.
    Output in C++ Tooutput a value, we use the cout operator as follows: cout << value; To print multiple values in the same line: cout << value1 << value2 << value3; To start printing in a new line: endl or ‘n’
  • 7.
    Arithmetic operators inC++ Arithmetic Operators: 1) + Addition 2) - Subtraction 3) * Multiplication 4) / Division (Quotient) 5) % Modulo (Remainder) NOTE: C++ follows the BODMAS rule
  • 8.
    Variables Variables are containersthat stores specific types of data. They can be modified with the assignment operator “=” Syntax: datatype variable_name = value;
  • 9.
    Variables Variable names cannot: •Have spaces (use underscore instead) • Start with a digit • Be reserved by the compiler • Already taken by another variable (in the same scope) NOTE: Keywords/Variables are case sensitive
  • 10.
    Datatypes Datatypes are usedto set the “type” of a variable. For example, int is used to declare integer variables. Two types of datatypes: • Primitive datatypes • Derived datatypes
  • 11.
    Common Primitive datatypes 1.int (long long int, unsigned int, etc.) 2. char 3. bool 4. float (double, long double) 5. Special type: void
  • 12.
    Common Derived datatypes 1.string 2. vector 3. map 4. set 5. priority_queue
  • 13.
    Arithmetic Assignment Operators 1.+= 2. -= 3. *= 4. /= 5. %=
  • 14.
    Unary Operators Operators thatonly need one value/operand are called unary operators. 1. + 2. - 3. ++ 4. --
  • 15.
    Input in C++ Tooutput a value, we use the cin operator as follows: cin >> value; To print multiple values in the same line: cin >> value1 >> value2 >> value3; NOTE: Each value must be separated by a space or a new line when taking input.
  • 16.
    Check your understanding- 1 1. How will you declare a character equal to exclamatory mark? 2. Take an integer input, and output the value multiplied by 7. 3. Take two values a, b as input, and output three values: a+b and a*b and a/b a/b should be a decimal, not an integer
  • 17.
    Conditions and Relational Operators Conditionsreturn a boolean value depending on whether the expression is true or false. Conditional operators: ==, != Relational operators: <, >, <=, >=
  • 18.
    Logical operators Logical operatorsperform operations on boolean values or expressions that result in Boolean values. 1. “(expr1) && (expr2)” checks whether BOTH are true. 2. “(expr1) || (expr2)” checks whether EITHER one is true. 3. “!(expr)” returns the OPPOSITE of the result of “expr” The operators are called AND, OR, NOT operators respectively
  • 19.
    Conditional statements Conditional statementsexecute a different block of code depending on the boolean value of a condition. Syntax: if (condition) { // something } else if (another_condition) { // something } else { // something }
  • 20.
    Check Your Understanding2 1. Take input of 3 numbers x, y, z and output the maximum using if statements 2. Given marks of a student, grade them from A to D 1. Between 0 and 30 -> D 2. Between 30 and 65 -> C 3. Between 65 and 90 -> B 4. Between 90 and 100 -> A 5. Output “Error” if less than 0 or greater than 100.
  • 21.
    Loop Loops are usedto repeat a block of code until some condition is satisfied. There are three types of loops in C++: 1. for loop 2. while loop 3. do-while loop
  • 22.
    Loop (Miscellaneous) • Aniteration is defined as one time the loop gets executed. For example, 3rd iteration is the 3rd time the loop is run. • “break” statement exits the current/innermost loop when executed. • “continue” statement skips to the next iteration of the current/innermost loop when executed.
  • 23.
    “for” loop Syntax: statement1: Executedonce before start of loop. statement2: Condition of the loop. Loop exits if false. statement3: Executed after each iteration. for (statement1; statement2; statement3) { // Code here }
  • 24.
    “while” loop Syntax: Check ifthe condition is true and then execute the block of code. Repeat. while (condition) { // Code here }
  • 25.
    “do-while” loop Syntax: Execute theblock of code and then check if the condition is true. Repeat. do { // Code here } while (condition);
  • 26.
    Scope A scope isa region of the program. Every pair of curly braces creates a new scope. The variables inside the scope cannot be used outside the scope.
  • 27.
    Miscellaneous • A loopinside another loop is called nested loops. Syntax: • Infinite loops are loops that run forever and never end (when the condition is always true) for (s1; s2; s3) { for (s4; s5; s6) { // Code here } }
  • 28.
    goto statements Goto/Jump statementsare used to skip to another part of the code. Considered as bad practice to use goto statements except if it used to exit from a nested loop. Syntax: label: // creates the label to skip to goto label; // skips to the specific label
  • 29.
    Check Your Understanding3 1. Find the sum of the first N natural numbers (Using loops) 2. For the first N natural numbers: If number is divisible by 3 and 5, print FizzBuzz If number is divisible by 3, print Fizz If number is divisible by 5, print Buzz 3. Print a N x M grid similar to the following: 1 2 3 4 5 6 7 8 9 10 11 12
  • 30.
    Exercise Write a programto take a number N as an input, and output whether it is a prime number or not. (Do not worry about efficiency)
  • 31.
    Resources • https://www.programiz.com/cpp-programming (learningC++ in general) • https://www.programiz.com/cpp-programming#flow-control (if-else and loops) • https://www.programiz.com/cpp-programming/nested-loops (nested loops) • https://www.programiz.com/cpp-programming/goto (goto statements)
  • 32.