際際滷

際際滷Share 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

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

Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
VAIBHAVKADAGANCHI
basics of c programming for naiver.pptx
basics of c programming  for naiver.pptxbasics of c programming  for naiver.pptx
basics of c programming for naiver.pptx
ssuser96ba7e1
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statements
Vladislav Hadzhiyski
C programming language:- Introduction to C Programming - Overview and Importa...
C programming language:- Introduction to C Programming - Overview and Importa...C programming language:- Introduction to C Programming - Overview and Importa...
C programming language:- Introduction to C Programming - Overview and Importa...
SebastianFrancis13
1-19 CPP 際際滷s 2022-02-28 18_22_ 05.pdf
1-19 CPP 際際滷s 2022-02-28 18_22_ 05.pdf1-19 CPP 際際滷s 2022-02-28 18_22_ 05.pdf
1-19 CPP 際際滷s 2022-02-28 18_22_ 05.pdf
dhruvjs
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
Mahira Banu
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
Ahmad Bashar Eter
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
ANUSUYA S
basic C PROGRAMMING for first years .pptx
basic C  PROGRAMMING for first years  .pptxbasic C  PROGRAMMING for first years  .pptx
basic C PROGRAMMING for first years .pptx
divyasindhu040
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
Mukund Trivedi
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
raksharao
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
sanatahiratoz0to9
C programming session3
C programming  session3C programming  session3
C programming session3
Keroles karam khalil
C programming session3
C programming  session3C programming  session3
C programming session3
Keroles karam khalil
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
Ralph Weber
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
shashikant pabari
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programming
Nico Ludwig
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
VAIBHAVKADAGANCHI
basics of c programming for naiver.pptx
basics of c programming  for naiver.pptxbasics of c programming  for naiver.pptx
basics of c programming for naiver.pptx
ssuser96ba7e1
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statements
Vladislav Hadzhiyski
C programming language:- Introduction to C Programming - Overview and Importa...
C programming language:- Introduction to C Programming - Overview and Importa...C programming language:- Introduction to C Programming - Overview and Importa...
C programming language:- Introduction to C Programming - Overview and Importa...
SebastianFrancis13
1-19 CPP 際際滷s 2022-02-28 18_22_ 05.pdf
1-19 CPP 際際滷s 2022-02-28 18_22_ 05.pdf1-19 CPP 際際滷s 2022-02-28 18_22_ 05.pdf
1-19 CPP 際際滷s 2022-02-28 18_22_ 05.pdf
dhruvjs
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
Mahira Banu
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
Ahmad Bashar Eter
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
ANUSUYA S
basic C PROGRAMMING for first years .pptx
basic C  PROGRAMMING for first years  .pptxbasic C  PROGRAMMING for first years  .pptx
basic C PROGRAMMING for first years .pptx
divyasindhu040
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
raksharao
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
sanatahiratoz0to9
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programming
Nico Ludwig
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel

Recently uploaded (20)

Why the Engineering Model is Key to Successful Projects
Why the Engineering Model is Key to Successful ProjectsWhy the Engineering Model is Key to Successful Projects
Why the Engineering Model is Key to Successful Projects
Maadhu Creatives-Model Making Company
Machine Elements in Mechanical Design.pdf
Machine Elements in Mechanical Design.pdfMachine Elements in Mechanical Design.pdf
Machine Elements in Mechanical Design.pdf
SLatorreAndrs
applicationof differential equation.pptx
applicationof differential equation.pptxapplicationof differential equation.pptx
applicationof differential equation.pptx
PPSTUDIES
PLANT CELL REACTORS presenation PTC amity
PLANT CELL REACTORS presenation PTC amityPLANT CELL REACTORS presenation PTC amity
PLANT CELL REACTORS presenation PTC amity
UrjaMoon
NFPA 70B & 70E Changes and Additions Webinar Presented By Fluke
NFPA 70B & 70E Changes and Additions Webinar Presented By FlukeNFPA 70B & 70E Changes and Additions Webinar Presented By Fluke
NFPA 70B & 70E Changes and Additions Webinar Presented By Fluke
Transcat
Hackathon-Problem-Statements-Technology-Track-with-Link.pptx
Hackathon-Problem-Statements-Technology-Track-with-Link.pptxHackathon-Problem-Statements-Technology-Track-with-Link.pptx
Hackathon-Problem-Statements-Technology-Track-with-Link.pptx
datahiverecruitment
windrose1.ppt for seminar of civil .pptx
windrose1.ppt for seminar of civil .pptxwindrose1.ppt for seminar of civil .pptx
windrose1.ppt for seminar of civil .pptx
nukeshpandey5678
Mastering Secure Login Mechanisms for React Apps.pdf
Mastering Secure Login Mechanisms for React Apps.pdfMastering Secure Login Mechanisms for React Apps.pdf
Mastering Secure Login Mechanisms for React Apps.pdf
Brion Mario
UHV UNIT-3 HARMONY IN THE FAMILY AND SOCIETY.pptx
UHV UNIT-3 HARMONY IN THE FAMILY AND SOCIETY.pptxUHV UNIT-3 HARMONY IN THE FAMILY AND SOCIETY.pptx
UHV UNIT-3 HARMONY IN THE FAMILY AND SOCIETY.pptx
ariomthermal2031
Artificial intelligence and Machine learning in remote sensing and GIS
Artificial intelligence  and Machine learning in remote sensing and GISArtificial intelligence  and Machine learning in remote sensing and GIS
Artificial intelligence and Machine learning in remote sensing and GIS
amirthamm2083
Self-Compacting Concrete: Composition, Properties, and Applications in Modern...
Self-Compacting Concrete: Composition, Properties, and Applications in Modern...Self-Compacting Concrete: Composition, Properties, and Applications in Modern...
Self-Compacting Concrete: Composition, Properties, and Applications in Modern...
NIT SILCHAR
22PCOAM16 _ML_ Unit 2 Full unit notes.pdf
22PCOAM16 _ML_ Unit 2 Full unit notes.pdf22PCOAM16 _ML_ Unit 2 Full unit notes.pdf
22PCOAM16 _ML_ Unit 2 Full unit notes.pdf
Guru Nanak Technical Institutions
Optimize AI Latency & Response Time with LLumo
Optimize AI Latency & Response Time with LLumoOptimize AI Latency & Response Time with LLumo
Optimize AI Latency & Response Time with LLumo
sgupta86
Intro PPT SY_HONORS.pptx- Teaching scheme
Intro PPT SY_HONORS.pptx- Teaching schemeIntro PPT SY_HONORS.pptx- Teaching scheme
Intro PPT SY_HONORS.pptx- Teaching scheme
Priyanka Dange
Analysis of Daylighting in Interior Spaces using the Daylight Factor - A Manu...
Analysis of Daylighting in Interior Spaces using the Daylight Factor - A Manu...Analysis of Daylighting in Interior Spaces using the Daylight Factor - A Manu...
Analysis of Daylighting in Interior Spaces using the Daylight Factor - A Manu...
Ignacio J. J. Palma Carazo
UHV Unit - 4 HARMONY IN THE NATURE AND EXISTENCE.pptx
UHV Unit - 4 HARMONY IN THE NATURE AND EXISTENCE.pptxUHV Unit - 4 HARMONY IN THE NATURE AND EXISTENCE.pptx
UHV Unit - 4 HARMONY IN THE NATURE AND EXISTENCE.pptx
ariomthermal2031
LA2-64 -bit assemby language program to count number of positive and negative...
LA2-64 -bit assemby language program to count number of positive and negative...LA2-64 -bit assemby language program to count number of positive and negative...
LA2-64 -bit assemby language program to count number of positive and negative...
VidyaAshokNemade
Airport Components Part1 ppt.pptx-Site layout,RUNWAY,TAXIWAY,TAXILANE
Airport Components Part1 ppt.pptx-Site layout,RUNWAY,TAXIWAY,TAXILANEAirport Components Part1 ppt.pptx-Site layout,RUNWAY,TAXIWAY,TAXILANE
Airport Components Part1 ppt.pptx-Site layout,RUNWAY,TAXIWAY,TAXILANE
Priyanka Dange
02.BigDataAnalytics curso de Legsi (1).pdf
02.BigDataAnalytics curso de Legsi (1).pdf02.BigDataAnalytics curso de Legsi (1).pdf
02.BigDataAnalytics curso de Legsi (1).pdf
ruioliveira1921
Knowledge-Based Agents in AI: Principles, Components, and Functionality
Knowledge-Based Agents in AI: Principles, Components, and FunctionalityKnowledge-Based Agents in AI: Principles, Components, and Functionality
Knowledge-Based Agents in AI: Principles, Components, and Functionality
Rashmi Bhat
Machine Elements in Mechanical Design.pdf
Machine Elements in Mechanical Design.pdfMachine Elements in Mechanical Design.pdf
Machine Elements in Mechanical Design.pdf
SLatorreAndrs
applicationof differential equation.pptx
applicationof differential equation.pptxapplicationof differential equation.pptx
applicationof differential equation.pptx
PPSTUDIES
PLANT CELL REACTORS presenation PTC amity
PLANT CELL REACTORS presenation PTC amityPLANT CELL REACTORS presenation PTC amity
PLANT CELL REACTORS presenation PTC amity
UrjaMoon
NFPA 70B & 70E Changes and Additions Webinar Presented By Fluke
NFPA 70B & 70E Changes and Additions Webinar Presented By FlukeNFPA 70B & 70E Changes and Additions Webinar Presented By Fluke
NFPA 70B & 70E Changes and Additions Webinar Presented By Fluke
Transcat
Hackathon-Problem-Statements-Technology-Track-with-Link.pptx
Hackathon-Problem-Statements-Technology-Track-with-Link.pptxHackathon-Problem-Statements-Technology-Track-with-Link.pptx
Hackathon-Problem-Statements-Technology-Track-with-Link.pptx
datahiverecruitment
windrose1.ppt for seminar of civil .pptx
windrose1.ppt for seminar of civil .pptxwindrose1.ppt for seminar of civil .pptx
windrose1.ppt for seminar of civil .pptx
nukeshpandey5678
Mastering Secure Login Mechanisms for React Apps.pdf
Mastering Secure Login Mechanisms for React Apps.pdfMastering Secure Login Mechanisms for React Apps.pdf
Mastering Secure Login Mechanisms for React Apps.pdf
Brion Mario
UHV UNIT-3 HARMONY IN THE FAMILY AND SOCIETY.pptx
UHV UNIT-3 HARMONY IN THE FAMILY AND SOCIETY.pptxUHV UNIT-3 HARMONY IN THE FAMILY AND SOCIETY.pptx
UHV UNIT-3 HARMONY IN THE FAMILY AND SOCIETY.pptx
ariomthermal2031
Artificial intelligence and Machine learning in remote sensing and GIS
Artificial intelligence  and Machine learning in remote sensing and GISArtificial intelligence  and Machine learning in remote sensing and GIS
Artificial intelligence and Machine learning in remote sensing and GIS
amirthamm2083
Self-Compacting Concrete: Composition, Properties, and Applications in Modern...
Self-Compacting Concrete: Composition, Properties, and Applications in Modern...Self-Compacting Concrete: Composition, Properties, and Applications in Modern...
Self-Compacting Concrete: Composition, Properties, and Applications in Modern...
NIT SILCHAR
Optimize AI Latency & Response Time with LLumo
Optimize AI Latency & Response Time with LLumoOptimize AI Latency & Response Time with LLumo
Optimize AI Latency & Response Time with LLumo
sgupta86
Intro PPT SY_HONORS.pptx- Teaching scheme
Intro PPT SY_HONORS.pptx- Teaching schemeIntro PPT SY_HONORS.pptx- Teaching scheme
Intro PPT SY_HONORS.pptx- Teaching scheme
Priyanka Dange
Analysis of Daylighting in Interior Spaces using the Daylight Factor - A Manu...
Analysis of Daylighting in Interior Spaces using the Daylight Factor - A Manu...Analysis of Daylighting in Interior Spaces using the Daylight Factor - A Manu...
Analysis of Daylighting in Interior Spaces using the Daylight Factor - A Manu...
Ignacio J. J. Palma Carazo
UHV Unit - 4 HARMONY IN THE NATURE AND EXISTENCE.pptx
UHV Unit - 4 HARMONY IN THE NATURE AND EXISTENCE.pptxUHV Unit - 4 HARMONY IN THE NATURE AND EXISTENCE.pptx
UHV Unit - 4 HARMONY IN THE NATURE AND EXISTENCE.pptx
ariomthermal2031
LA2-64 -bit assemby language program to count number of positive and negative...
LA2-64 -bit assemby language program to count number of positive and negative...LA2-64 -bit assemby language program to count number of positive and negative...
LA2-64 -bit assemby language program to count number of positive and negative...
VidyaAshokNemade
Airport Components Part1 ppt.pptx-Site layout,RUNWAY,TAXIWAY,TAXILANE
Airport Components Part1 ppt.pptx-Site layout,RUNWAY,TAXIWAY,TAXILANEAirport Components Part1 ppt.pptx-Site layout,RUNWAY,TAXIWAY,TAXILANE
Airport Components Part1 ppt.pptx-Site layout,RUNWAY,TAXIWAY,TAXILANE
Priyanka Dange
02.BigDataAnalytics curso de Legsi (1).pdf
02.BigDataAnalytics curso de Legsi (1).pdf02.BigDataAnalytics curso de Legsi (1).pdf
02.BigDataAnalytics curso de Legsi (1).pdf
ruioliveira1921
Knowledge-Based Agents in AI: Principles, Components, and Functionality
Knowledge-Based Agents in AI: Principles, Components, and FunctionalityKnowledge-Based Agents in AI: Principles, Components, and Functionality
Knowledge-Based Agents in AI: Principles, Components, and Functionality
Rashmi Bhat

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)