DSA FOR ALL
LECTURE : 2
Basics of Programming C++.
TABLE OF CONTENTS
01 First Program in C++
02 Data Types And Variables and Strings
03 Condition Statements
04 Loops
05 Operators
06 Arrays and Problems Solving
FIRST PROGRAM IN C++
#include<bits/stdc++.h>
using namespace std;
Header Files
int main() {
cout<<“Hello World”;
return 0; Main() Function
}
Output : Hello World
Data-Types And Variable
Important Data-Types Are:
1.) int
2.) long long int
3.) float
4.) double
5.) bool (Boolean)
6.) char
Variable are used with datatypes, it is generally
used to store values.
Syntax of a Variable :
DataType varname;
Ex : int data;
String String str = “Hello”;
String are defined as group of characters.
It is used to represent text in a program and change it.
If-else Statement
Syntax :
If(condition)
{
statement;
}
else if(condition)
{
}
else
{
}
If else are two conditional statements, which are generally used when we want to
run the code based on some condition.
For Loop
Syntax :
for(initialization;condition;updation){
Ex:
for(int i=0;i<10;i++){
cout<<“hello”;
}
For loops are used to repeat the execution of the code, instead of repeating the code
again and again.
While Loop
Syntax :
while(condition){
Body;
}
Ex:
While(num<10)
{
cout<<num;
num+=1;
}
While loops are used when we don’t know the exact number of times the loop
should repeat.it repeats the statement or body till the condition is true.
Operators
And : &
or : |
xor : ^
Not : !
Operators are defined to make some conditions .
Arrays
#include<bits/stdc++.h>
using namespace std;
int main() {
cout<<“Hello World”;
return 0;
Arrays can be defined
} as group or collection of elements of the same type.
Array elements are stored at contiguous memory locations.
Syntax for Array Declaration
Datatype arr_name[no. of elements];
Functions
Syntax :
Return-type name(arguments){
body;
}
Functions is a group or collection of statements that are designed to perform
specific tasks.