0% found this document useful (0 votes)
31 views19 pages

C++ Basics: Variables, Assignments Input

This document discusses basic C++ concepts such as variables, data types, input/output, and functions. It explains that every C++ program must have a main function, variables are used to store and represent data, and common data types include int, double, float, and char. It also covers variable declaration and assignment, input using cin, output using cout, and basic program structure.

Uploaded by

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

C++ Basics: Variables, Assignments Input

This document discusses basic C++ concepts such as variables, data types, input/output, and functions. It explains that every C++ program must have a main function, variables are used to store and represent data, and common data types include int, double, float, and char. It also covers variable declaration and assignment, input using cin, output using cout, and basic program structure.

Uploaded by

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

C++ BASICS

Variables,
Assignments
Input
Main function
2

 void main( ) - main function header

• Every C++ program has at least one function, called main.

• Program execution begins with the first statement in main.

Beginning C++ Programming


Statement terminator
3

  ; statement terminator

 Every C++ statement must end with a semicolon.

 << stream insertion operator

 Expression to the right of the operator is inserted (sent) to the cout object

(the display screen or console window).

Beginning C++ Programming


1001
Variables y 12.5 1002
1003
4
1004
Temperature 32 1005
1006
Letter 'c' 1007
1008
Number 5 1009

 Variable can hold a number or a data of other types, it


always holds something. A variable has a name

 Variables are implemented as memory locations and


assigned a certain memory address. The exact address
depends on the computer and compiler.

 A variable definition means to tell the compiler where and


how much storage to create for the variable.
Rules for naming a Variable
5

 Variable name must start with a letter or underscore


symbol (_), the rest of the characters should be
letters, digits or underscores
 The following are legal variable names:
x x1 x_1 _abc sum RateAveragE
 The following are illegal variable names.
13 3X %change data-1 my.identifier a(3)
 C++ is case sensitive:
MyVar and myvar are different variable names
Variable Declarations
6

 Every variable in C++ program needs to be declared


 Declaration tells the compiler (and eventually the
computer) what kind of data is going to be stored in the
variable
 The kind of data stored in a variable is called it’s type
 A variable declaration specifies
 Data type

 Name

 Declaration syntax:
Data type id, id, ..., id
Data Types
7

 Commonly used numeric types are:


 int - whole positive or negative numbers:

1,2, -1,0,-288, etc.


 double - positive or negative numbers with fractional part:

1.75, -0.55
 float - positive or negative numbers with fractional part:
1.75, -0.55
 char- alphabets
‘a’, ‘h’,’i’, etc
 Example declarations:
int number;
double weight, totalWeight;
sizeof
8

 cout <<sizeof (int ) ;


 cout <<sizeof (float ) ;
 cout <<sizeof (char ) ;
Keywords
9

 Keywords / reserved words are reserved as


part of the language
int, return, float, double
 cannot be used by the programmer to name
things
 consist of lowercase letters only
 have special meaning to the compiler
Where to Declare
11

 A variable should be declared before its use

 if the variable will be used in several unrelated


locations, declare it at the beginning of the program:
void main() {
 right here
Assignment
12

var = expression;
 Assignment statement is an order to the computer to set the
value of the variable on the left hand side of the equation
to what is written on the right hand side
 Example:
number = 37;
totalWeight = oneWeight;
totalWeight = oneWeight * number;
number = number + 3;
Initialization
13

Variable is assigned a value at the time of


declaration int a=10;

Here, variable is allocated a memory location


and at the same time , a value is assigned to it
Simple Program
14

//adding two numbers


#include<iostream.h>
#include<conio.h>
void main()
{
int num1,num2,ans; //declaration

num=10;
num=20;
ans=num1+num2;
cout<<ans;
getch();
}
Named Constants
15

 Permanent data that never changes through out the


execution of the code.
 Example
 const double PI = 3.1415 ;
 #define PI 3.1415
 You don’t have to repeatedly type the same value.
 Change the constant value at a single location.
 Program readability increases.
Output
16

 To do input/output, at the beginning of your program insert


#include <iostream> C++ uses streams for input an output
 stream - is a sequence of data to be read (input stream) or a sequence of data
generated by the program to be output (output stream)
 variable values as well as strings of text can be output to the screen using cout
(console output):
cout << ans
cout << ”candy bars”;
cout << endl;
 << is called insertion operator, it inserts data into the output stream, anything
within double quotes will be output literally (without changes) - ”candy
bars taste good”
 note the space before letter “ c” - the computer does not insert space on its
own

keyword endl tells the computer to start the output from the next line
More Output
17

 Data in the output can be stacked together:


cout << number << ”candy bars\n”;
 Symbol \n at the end of the string serves the same
purpose as endl
 Arithmetic expressions can be used with the output
statement:
cout << ”The answer is”<< (num1 + num2);
Escape sequences
18

 The backslash is an escape character. The character


following it takes on a different meaning, e.g.:
  Sequence
Escape Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

Beginning C++ Programming


Input
19

 cin - (stands for Console Input) - is used to fill the variables with
the input from the user.
 When the program reaches the input statement it just pauses until
the user types something and presses <Enter> key
cin >> num1;
cin >> num2;
 >> is extraction operator
 The values typed are inserted into variables when <Enter> is
pressed, if more values needed - program waits, if extra typed - they
are used in next input statements if needed
Simple Program
20

//adding two numbers


#include<iostream.h>
#include<conio.h>
int main()
{
int num1=0,num2=0; //initialization
int ans=0; //initialization

cin>>num1; //ask the user to enter the value

cin>>num2; //ask the user to enter the value

ans=num1+num2;
cout<<“The answer is”<<ans;//display statement
}

You might also like