0% found this document useful (0 votes)
433 views13 pages

STD 8 Notes C++ 2015-16

The document provides an introduction to the C++ programming language. It discusses that computer studies involve hardware and software, and software includes operating systems, packages, and programming languages. It then defines a program and programming language. The rest of the document discusses C++ in more detail, including how to load and run a basic "Hello World" C++ program, basic syntax rules, data types in C++, mathematical and assignment operators, and how to take user input using the cin command.

Uploaded by

yekomor876
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)
433 views13 pages

STD 8 Notes C++ 2015-16

The document provides an introduction to the C++ programming language. It discusses that computer studies involve hardware and software, and software includes operating systems, packages, and programming languages. It then defines a program and programming language. The rest of the document discusses C++ in more detail, including how to load and run a basic "Hello World" C++ program, basic syntax rules, data types in C++, mathematical and assignment operators, and how to take user input using the cin command.

Uploaded by

yekomor876
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/ 13

SMT.SULOCHANADEVI SINGHANIA SCHOOL, THANE.

ICT

INTRODUCTION TO C++

The studies of computers are divided into two categories:


Hardware
Software

Further, the software studies are sub-divided into:


Operating System
Packages
Programming Languages

So far, you have learnt a package like MS – Office containing Word, Excel,
PowerPoint, etc. You have also learnt an Operating system i.e. Windows.

What is a program?
List of commands given to the computer to perform a specific task is known as a
program.

What is a programming language?


The language in which you write programs is known as a programming language.
There are many programming languages like:
BASIC : Beginners All purpose Symbolic Instruction Code
COBOL : Common Business Oriented Language
FORTRAN : Formula Translation
PASCAL : named after the French scientist Blaise Pascal
C++ : Originated from a programming language called C
C++ was developed by "Dr. Bjarne Stroustrup” from Bell Laboratories, USA in
1981.

Loading C++
You can start C++ by clicking on the short cut icon “Shortcut to Turbo C++” .
C++ automatically opens one program file. It is named as noname00.cpp - cpp
stands for C plus plus.

Your C++ screen indicates the cursor position by displaying the row number and
column number at the left-hand bottom corner of the screen. It also contains the
horizontal and vertical scroll bars and the function key bar.

Developing a C++ program


The program is first developed or written and then executed to get the desired
output. The C++ program is written in the editor screen or C++ editor (blue
screen). The output is shown on a blank screen called as the terminal window.

What are syntax rules?


As you have grammatical rules in English language, there are certain rules in
programming language. These rules have to be followed while using every
command in the programming language. These rules are known as syntax rules. If
these syntax rules are violated, you will get a syntax error on compilation of the
program.
Your Program works in 5 steps –
1. Typing your Program – Type the program using the proper syntax rules.
2. Saving the Program – Once the program has been typed, it needs to be
saved. This can be done by clicking on File – Save command. C++
automatically saves the file as a .cpp file. cpp is the extension given to c++
files and stands for c plus plus. The saved file in C++ is called as the source
code.
3. Compilation – The source code has to be compiled in order to remove the
mistakes (syntax errors) that may be present in the program. In this process
the source code gets converted into an .obj file. We do this by clicking on
COMPILE (alt+F9) and letting the computer do the rest.
4. Linking and Building – The compiled file (.obj) cannot be executed (run)
until you convert it to an executable file (.exe). This can be done by clicking
on Compile – Make command.
5. Run – Once the process of making the file is completed your file is ready to
be executed (run). On execution it shows the output of your program on the
screen.

The above steps can be avoided and the output can be directly seen by
pressing ctrl+F9 keys together also. Even if you choose to use the shortcut
method for execution of your program but the process of Compilation and
Linking takes place in the background. Remember without these processes
the program will not execute.

Rules for writing C++ program

To begin your C++ program, you require two header files. They are:
iostream.h - input output stream.header file
conio.h - console input output.header file

When you want to write a C++ program, you should begin it with the
following commands:
#include<iostream.h>
#include<conio.h>

These commands are a must for writing any C++ program. Make sure that you
follow the correct syntax.
Next, you need a heading for your C++ program. Every C++ program has a
common heading, which is as follows:

void main( )

C++ is a case sensitive language. Therefore all commands in C++ are written
in lower case.

The body of the C++ program i.e. the commands of a program also called as a
block, is always enclosed in { } (brace or curly brackets).
Few commands of C++

Command Meaning Usage


cout<< console out for displaying values/message on the screen
cin>> console in for inputting values through the keyboard
clrscr( ) clear screen for clearing the screen
getch( ) get character for keeping the output screen open till any
key is pressed
Now, let us write a sample program to display a message “Hello World!” on the
computer.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
cout<< “Hello World!”;
}

Observe the program carefully. The Body of the program consists of 2 lines. Every
line in the body ends with the semi-colon. As you end every sentence in English
with a full-stop, you must end every command in C++ with a semi-colon.
Whenever, a message has to be displayed on the screen, it should always be
written within quotation marks, i.e. “ ”.

After you complete the program, you need to execute or run the program.
You can do so by pressing Ctrl + F9 keys on the keyboard.
This program will clear your screen and display:
Hello World!

Why do we end our program with the command getch( ) ?


When you execute the above program, you will notice that the program output is
not visible to you. This is because it gets displayed on the screen and it
immediately vanishes from the screen. In order to solve this problem, we need a
command called getch( ). This command will keep the output on the screen till
we press any key on the keyboard. This command should be written at the
end of the C++ program before closing the brace brackets.

Few shortcut keys of C++ (useful when you type C++ programs)
End key - to reach the cursor at the end of the line
Home key - to reach the cursor at the beginning of the line
Pgup - to move the cursor several lines up
Pgdn - to move the cursor several line down
F5 - to maximize the window
[↑ ↨] - to maximize or restore the window
[ ▌] - to close the file
Escape Sequences for display of output

You can use some options while printing the text on the screen. The options are as
follows:
\n or endl - prints the text on a new line
\t - prints the text with a space or gap of 5 columns
\a - prints the text with a beep sound

For eg:

cout<< “fun”;
cout<< “time”;
will give the output as:
funtime

cout<< “fun\n”; OR cout<< “fun”<<endl;


cout<< “time”; cout<< “time”;
will give the output as: will give the output as:
fun fun
time time

cout<< “fun\t”;
cout<< “time”;
will give the output as:
fun time

Data types in C++


Following are the data types in C++:

int - For storing integers


float - For storing decimal numbers
char - For storing a single character (a character can be an alphabet, a digit, a
special character or even a blank space)

Assigning values to the variables

Whenever you use a value in a program, you need to assign it to a variable. Your
command will be as follows:

int a=5;
float x = 12.5;
char ch= ‘a’; note: characters are to be enclosed in single quotation marks.

Assignment of values can also be done as follows,


int a;
a=5;
i.e. The value 5 is assigned to the variable a.

Here the ‘=’ sign is called as the assignment operator in C++.


Remember – assignment always is from right to left.
Hence, 5 = a;
cannot be written and will give a syntax error on compilation of the program.

Sample program for assigning two values and print them on two different
lines:
OR
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main( ) void main( )
{ {
clrscr( ); clrscr( );
int a=5; int a,b;
int b=10; a=5;
cout<< “first number is=”<<a<<endl; b=10;
cout<< “second number =”<<b; cout<< “first number is=”<<a<<endl;
getch( ); cout<< “second number =”<<b;
} getch( );
}

Mathematical operators used with numeric data type

+ for addition * for multiplication


- for subtraction / for division
% for finding the remainder(called modulo)

cin command

So far you have been assigning values to the variables within the C++ program.
You will now learn to give values to the variables during program execution.

Here, the user is allowed to give the values to the variables used in the program
through the keyboard.

The syntax of the cin command is as follows:

Syntax: cin>>variable name;


Example: cin>>a;

In case of more than one variable, the cin command will be written as:

Syntax: cin>>variable 1>>variable 2>>variable 3;


Example: cin>>a>>b>>c;

OR
cin>>variable 1;
cin>>variable 2;
cin>>variable 3;

Example: cin>>a;
cin>>b;
cin>>c;
A sample program with explanation using cin command:

W.A.P to accept two float values from the user and find the sum and the average.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
float a,b,s,avg;
cout<< “Enter values for a and b”<<endl; (message prompt to enter values)
cin>>a>>b; (values entered from keyboard will be accepted by the variables)
s=a+b;
avg=s/2;
cout<< “sum=”<<s<<endl;
cout<< “average=”<<avg;
getch( );
}

Program output:

Enter values for a and b


14.5 15.6
sum=30.1
average=15.05

cin with character and strings

In C++, a character can be either an alphabet or a number or a special symbol.


Character is declared using the following syntax char ch.
In the above declaration ‘ch’ is the name of the variable. ‘char’ is the data type
used to declare a character.
Here’s a sample program to input a character and print it 4 times.

Program

#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr( );
char ch;
cout<< “Enter any character”<<endl;
cin>>ch;
cout<<ch<<endl;
cout<<ch<<endl;
cout<<ch<<endl;
cout<<ch<<endl;
getch( );
}
Output of the program

Enter any character


*

*
*
*
*

A group of characters is called as a string. If you want to store a string, i.e. a word
in a variable, you need to declare the approximate size of that string. Such a string
whose length or size is declared or defined is called as character array.

Here’s a sample program using character array with cin command.

Program

#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr( );
char nm[15];
cout<< “Enter your name”<<endl;
cin>>nm;
cout<< nm<<endl;
cout<< “Welcome to the world of computers”;
getch( );
}

Output of the program

Enter your name


Manju

Manju
Welcome to the world of computers

gets( ) command & puts( ) command


If you want to store strings, i.e. words or sentences with blank spaces in variables,
you cannot use the cin command. cin command is not capable of accepting strings
with blank spaces. For this, we need another command called gets( ).
The full form of gets( ) is get-string. In order to use this command we need to
use a header file called stdio.h (standard input output.header file).
To print or display the output, we can use puts( ) command instead of cout.
Here’s a sample program using gets( ) & puts( ) command.

Program

#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main( )
{
clrscr( );
char nm[25];
int ag;
cout<< “Enter your full name”<<endl;
gets(nm);
cout<< “Enter your age”<<endl;
cin>>ag;
puts(nm);
cout<< “You are”<<ag<< “years old”;
getch( );
}

Output of the program

Enter your full name


John Francis Rebello
Enter your age
29

John Francis Rebello


You are 29 years old

DECISION MAKING C++

In order to make decisions in the program, we make use of the if statement in


C++. if else, if else if are all known as condition statements in C++. In order to
frame a condition, we need operators along with the if statement. There are 3
different types of operators in C++.

1. Mathematical / Arithmetic Operators

Operator What it signifies Example


+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus (for remainder) a%b

2. Relational Operators

These operators establish a relation between two values and decide whether it is
true or false.
Operator What it signifies Example
== Equal to a= =b
!= Not equal to a!=b
> Greater than a>b
< Less than a<b
>= Greater than equal to a>=b
<= Less than equal to a <=b

Syntax: if(condition)
{
action to be taken if the condition is true;
}
else
{
action to be taken if the condition is false;
}

Example: Write a program to input your name and age and check eligibility for
voting.
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main( )
{
clrscr( );
char nm[25];
int ag;
cout<< “Enter your full name”<<endl;
gets(nm);
cout<< “Enter your age”<<endl;
cin>>ag;
if(ag>=18)
{
cout<<nm<< “You are eligible to vote”;
}
else
{
cout<<nm<< “Sorry you cannot vote”;
}

3. Logical operators

Example 1
“If you have money and if your mother gives you permission then you can go and
have an ice cream”.

In the above statement, we understand that unless both the conditions are satisfied,
you cannot eat ice cream.

Example 2
If x is greater than y and x is greater than z, then x is the largest number amongst
the three numbers x, y and z.
In the above statement, we compare x with the other two numbers y and z and then
arrive at a conclusion that x is the largest.

In order to solve the above program, we require Logical operators, which help us
to combine two or more conditions together.

Logical operator Symbol Syntax


and && x > y && x > z
or || a= =b || b= =c

Logical operator and (&&)


This operator is used to compare and check if all the given conditions are true. If
yes, then action to be taken is performed.

Logical operator or ( | | )
This operator is used to compare and check any one of the given conditions is true.
If yes, then action to be taken is performed.

Sample program using logical operators

A program to find if you are allowed to eat in a restaurant.

Input your age and the amount of money you have. If you are above 15 and you
have Rs.300 or more with you, you can eat out.

Program

#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr( );
int ag,rs;
cout<< “Enter your age”<<endl;
cin>>ag;
cout<< “Enter the amount of money”<<endl;
cin>>rs;
if (ag>15 && rs>=300)
{
cout<< “You can eat out”;
}
else
{
cout<< “sorry, you have to eat at home”;
}
getch( );
}
Output of the program
Enter your age
13
Enter the amount of money
350

sorry, you have to eat at home

In the above program, only one condition is satisfied or true. Hence, the above
output.

IF ELSE-IF STATEMENT

When you have to check for multiple conditions, then we use the if else-if
statement in C++.

Syntax

if (condition 1)
{
action to be taken if condition 1 is true;
}
else if (condition 2)
{
action to be taken if condition 2 is true;
}
else
{
action to be taken if both conditions 1 and 2 are false;
}

Sample program
A program to input 3 numbers and find out the largest among them.

#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr( );
int a,b,c;
cout<< “Enter the 3 numbers”<<endl;
cin>>a>>b>>c;
if (a>b && a>c)
{
cout<<a<< “is the largest”;
}
else if (b>a && b>c)
{
cout<<b<< “is the largest”;
}
else
{
cout<< c<< “is the largest”;
}
getch( );
}

Output of the program


Enter three numbers
15 25 10

25 is the largest

FOR LOOP IN C++

Loops are used in a program to repeat a statement for a given number of times. For
eg: loops can be used for displaying a string many times, for counting numbers,
calculating the sum of the numbers, etc.

Loops in C++ are of three types:


1) ‘for’ loop
2) ‘while’ loop
3) ‘do while’ loop

We will be concentrating on ‘for’ loop only.

The syntax of the ‘for’ loop is:

for (initial value;condition;increment or decrement)

To further explain the above syntax, we will take an example:

Example: To print numbers from 1 to 5 we will write the ‘for’ loop as follows:

for(i=0;i<=5;i++) OR for(i=0;i<=5;i=i+1)

initial condition initial condition increment


value value
increment

A loop contains the following parts:


1. Control variable: A variable which starts with an initial value and
decides the number of repetitions is called as control variable. In the
above example, i is the control variable.

2. Body of the loop: A set of statements which are executed within the loop.

3. Test condition: Each loop has a test condition. Depending upon the test
condition, the loop will be repeated or terminated. If the test condition is true,
the control of the program enters the body of the loop for executing the
statements within the loop. If the condition is false, the loop terminates. In the
above example, i<=5, is the test condition.
4. Step value (increment or decrement): The step value in a loop determines the
increment (increase) or decrement (decrease) of the control variable. In the above
example, i++ or i=i+1, is the step value or increment.
Now, we will see the working of the for loop with an example:

This program will print ‘Hello’ 5 times on the screen.

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i;
for(i=1;i<=5;i++)
{
cout<< “Hello”<<endl; (Body of the loop)
}
getch( );
}
1. The variable i is initialized to 1.
2. Control checks for the condition, i<=5. If it is true, then it enters the body of
the loop and executes the statement.
3. After executing the statement, it returns back to the for statement and
increases the value of i by 1. The value of i, now becomes 2. The condition,
i<=5 is checked again. If it is true, then it enters the body of the loop and
executes the statement again.
4. Thus, this process is repeated, till the test condition becomes false or value
of i exceeds 5.

**************************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy