SlideShare a Scribd company logo
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!

More Related Content

PPTX
C Programming with oops Concept and Pointer
PPTX
Cs1123 4 variables_constants
PPTX
Chapter 2: Elementary Programming
PPT
Lab 4 reading material formatted io and arithmetic expressions
PPTX
POLITEKNIK MALAYSIA
PPTX
Loop Statements TanCollege Programming course.pptx
PPTX
Review of C programming language.pptx...
PPTX
Presentation 2nd
C Programming with oops Concept and Pointer
Cs1123 4 variables_constants
Chapter 2: Elementary Programming
Lab 4 reading material formatted io and arithmetic expressions
POLITEKNIK MALAYSIA
Loop Statements TanCollege Programming course.pptx
Review of C programming language.pptx...
Presentation 2nd

Similar to C_BASICS FOR C PROGRAMMER WITH SRIVATHS P (20)

PPTX
Introduction to C ++.pptx
PPTX
basics of c programming for naiver.pptx
PPTX
Operators loops conditional and statements
PPTX
C programming language:- Introduction to C Programming - Overview and Importa...
PDF
1-19 CPP Slides 2022-02-28 18_22_ 05.pdf
PDF
C programing Tutorial
PDF
Acm aleppo cpc training second session
PPTX
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
PPTX
basic C PROGRAMMING for first years .pptx
PPT
85ec7 session2 c++
PPTX
lecture 2.pptx
PPT
Data types and Operators
PPTX
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
PDF
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
PPTX
All type looping information and clearly
PDF
C programming session3
PDF
C programming session3
PPTX
C_plus_plus
PPT
Basic concept of c++
ODP
(2) cpp imperative programming
Introduction to C ++.pptx
basics of c programming for naiver.pptx
Operators loops conditional and statements
C programming language:- Introduction to C Programming - Overview and Importa...
1-19 CPP Slides 2022-02-28 18_22_ 05.pdf
C programing Tutorial
Acm aleppo cpc training second session
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
basic C PROGRAMMING for first years .pptx
85ec7 session2 c++
lecture 2.pptx
Data types and Operators
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
All type looping information and clearly
C programming session3
C programming session3
C_plus_plus
Basic concept of c++
(2) cpp imperative programming
Ad

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT
Project quality management in manufacturing
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
composite construction of structures.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
Welding lecture in detail for understanding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Lecture Notes Electrical Wiring System Components
CH1 Production IntroductoryConcepts.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Project quality management in manufacturing
Embodied AI: Ushering in the Next Era of Intelligent Systems
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Structs to JSON How Go Powers REST APIs.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
composite construction of structures.pdf
573137875-Attendance-Management-System-original
Welding lecture in detail for understanding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Geodesy 1.pptx...............................................
Foundation to blockchain - A guide to Blockchain Tech
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Digital Logic Computer Design lecture notes
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Ad

C_BASICS FOR C PROGRAMMER WITH SRIVATHS P

  • 2. Why you should prefer C++ (For Competitive Programming) • Efficiency and Speed • Most popular language for CP • In-built Data Structures and Algorithms (STL)
  • 3. 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.
  • 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++ 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’
  • 7. Arithmetic operators in C++ Arithmetic Operators: 1) + Addition 2) - Subtraction 3) * Multiplication 4) / Division (Quotient) 5) % Modulo (Remainder) NOTE: C++ follows the BODMAS rule
  • 8. Variables Variables are containers that 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 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
  • 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 that only need one value/operand are called unary operators. 1. + 2. - 3. ++ 4. --
  • 15. 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.
  • 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 Conditions return a boolean value depending on whether the expression is true or false. Conditional operators: ==, != Relational operators: <, >, <=, >=
  • 18. 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
  • 19. 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 }
  • 20. 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.
  • 21. 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
  • 22. 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.
  • 23. “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 }
  • 24. “while” loop Syntax: Check if the condition is true and then execute the block of code. Repeat. while (condition) { // Code here }
  • 25. “do-while” loop Syntax: Execute the block of code and then check if the condition is true. Repeat. do { // Code here } while (condition);
  • 26. 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.
  • 27. 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 } }
  • 28. 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
  • 29. 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
  • 30. 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)
  • 31. 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)