Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Basics of C++ for DSA

● User input / output


● Data types
● If else statement
● Switch statement
● For Loops
● While Loops
● Function pass by reference and value

By - Himanshu Rawat
Skeleton of C++

#include <iostream>
Int main (){
std::cout << “hello” << std::endl;
return 0;
}
Some other libraries

#inlcude <iostream> //for input and output functions

#include <math.h> //for mathematical functions

#include <strings> //strings

#include <bits/stdc++.h> //to include all the libraries of c++


Input and output

#include <iostream>
using namespace std;
int main (){
int a;
cin >> a;
cout << "the given input is: " << a;
return 0;
}
Data Type - 1 int
#include <iostream>
using namespace std;
int main (){
//integer
int a=10;

//long
long b=15;

long long c= 565674632326563;

return 0;
}
2 Float and double

#include <iostream>
using namespace std;
int main (){
//float and double
float a = 10.6;
Float b = 5;
double c= 6.564646;
return 0;
}
3 string

#include <iostream>
using namespace std;
int main (){
//string
string s;
cin >> s;
cout << s
return 0;
}
4 getline

#include <iostream>
using namespace std;
int main (){
//getline
string str;
getline (cin, str);
cout << str;
return 0;
}
5 char

#include <iostream>
using namespace std;
int main (){
//char
char ch;
cin >> ch;
cout << ch;
return 0;
}

//char is declared with ‘g’ and string is declared with “helo”


Easy way to remember

Int = -10^9 to 10^9

Long= - 10^ to 10^12

longlong= - 10^18 to 10^18


If else statement
#include <bits/stdc++.h>
using namespace std;
int main(){
int age;
cin >> age;
if (age>=18){
cout << "adult";
}
else{
cout << "not a adult";
}
return 0;
}
// else is not mandatory to have in code
Else if
#include <bits/stdc++.h>
using namespace std;
int main(){
int age;
cin >> age;
if (age>=18){
cout << "you are an adult";
}
else if (age<10){
cout << "you are not an adult";
}
else {
cout << "you are not a adult but teen";
}
return 0;
}
WAP to check the grade for the marks.
Nested if
Switch statement
For Loop
While loop
Do While
Basics of Array

Operation on array
2d Array

It will give the garbage value for the location


where the values are not assigned
String
Array Using loops
Functions (Pass by reference and values)
Take two num and print its sum using functions
Print the min of two num using function
Function pass by value
Function pass by reference
Array is always passed by reference

You might also like