0% found this document useful (0 votes)
10 views6 pages

C

The document provides an introduction to C++ programming including preprocessor directives, header files, functions, input/output operators, data types, conditional statements like if/else, loops like for, while and do-while along with code examples for each.

Uploaded by

mihika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

C

The document provides an introduction to C++ programming including preprocessor directives, header files, functions, input/output operators, data types, conditional statements like if/else, loops like for, while and do-while along with code examples for each.

Uploaded by

mihika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

c++ 101

#include <iostream> #include <iostream>


using namespace std;
int main()
{ int main()
std::cout<<"hello world\ {
return 0; cout<<"hello world\n";
} return 0;
}

#include = preprocessor directive - used to include files


iostream = header file for taking input and printing output
main = program starts execution from here
cout = display output in quotation marks
\n = line break

; = end of sentence
{ } = start and end of function
>> = insertion operator
<< = extraction operator

//code for sum of 2 integers

#include <iostream>
using namespace std;

int main()
{
int a;

c++ 101 1
cin>>a;
int b;
cin>>b;
int sum=a+b;
cout<<"sum=", sum;
}

if else ladder

//code for if else statement //code for nested if else st


#include <iostream> #include <iostream>
using namespace std; using namespace std;

int main() int main()


{ {
int savings; int savings;
cout<<"please enter your cout<<"please enter your
cin>>savings; cin>>savings;

if(savings>5000) if(savings>5000)
cout<<"laptop \n"; {
else if(savings>10000)
cout<<"nothing \n"; cout<<"macbook \
} else
cout<<"windows \
}
else if(savings>2000)
//code for if,else if, else cout<<"ipod \n";
#include <iostream> else
using namespace std; cout<<"nothing \n";
}
int main()
{
int savings;
cout<<"please enter your

c++ 101 2
cin>>savings;
//code for largest of 3 numb

if(savings>5000) #include <iostream>


cout<<"laptop \n"; using namespace std;
else if(savings>2000)
cout<<"ipod \n"; int main()
else {
cout<<"nothing \n"; int a, b, c;
cout<<"enter 3 numbers:
}
cin>>a>>b>>c;

if(a>b)
{
//code for odd or even
if(a>c)
#include <iostream>
{
using namespace std;
cout<<"largest n
}
int main()
else
{
{
int a;
cout<<"largest n
cout<<"enter a number: "
}
cin>>a;
}
else
if(a%2==0)
{
{
cout<<"number is eve if(b>c)
{
}
cout<<"largest n
else
}
{
cout<<"number is odd else
} {
cout<<"largest n
}
}
}
}

for loop

c++ 101 3
syntax :
for(initialisation; condition; update)

//body

//code for sum of n natural numbers


#include <iostream>
using namespace std;

int main()
{
int n;
cout<<"enter n: ";
cin>>n;

int i;
int sum=0;
for(i=1; i<=n; i++)
{
sum = sum + i;
}
cout<<"sum of n numbers is: "<<sum<<endl;

number of iterations is counted here

while loop

syntax :

while(condition)
{

//body

c++ 101 4
//code for accepting only positive numbers as input
#include <iostream>
using namespace std;

int main()
{
int n;
cin>>n;
while(n>0)
{
cout<<n<<endl;
cin>>n;
}
}

loop runs once condition is checked

different from for as number of iterations is not counted


here - loop will run as long as condition is satisfied

do while loop

syntax :

do {
//body

} while(condition);

//code for accepting only positive numbers as input


#include <iostream>
using namespace std;

int main()
{
int n;
cin>>n;
do{
cout<<n<<endl;

c++ 101 5
cin>>n;
}while(n>0);
}

loop runs once as body is outside while loop - condition is


checked after the loop runs once

c++ 101 6

You might also like