CH 2
CH 2
CH 2
#include<iostream.h>
int main()
{
int workdays;
float workhours, payrate, weeklypay;
workdays = 5;
workhours = 7.5;
payrate = 38.55;
weeklypay = workdays * workhours * payrate;
cout<< "weekly Pay= ";
cout<<weeklypay<<endl;
}
OUTPUT:
weekly Pay= 1445.62
CONT….
Simple Input/output
The most common way in which a program
communicates with the outside world is through
simple, character-oriented Input/Output (IO)
operations. C++ provides two useful operators for
this purpose: >> for input and << for output.
CONT….
#include<iostream.h>
int main()
{
int a;
cin>>a;
cout<<a;
}
CONT….
• A comment is a piece of descriptive text which
explains some aspect of a program. Program
comments are totally ignored by the compiler and
are only intended for human readers. C++ provides
two types of comment delimiters:
• Anything after // (until the end of the line on which
it appears) is considered a comment.
• Anything enclosed by the pair /* and */ is
considered a comment.
CONT….
Integer Numbers
An integer variable may be defined to be of type
short, int, or long. The only difference is that an int
uses more or at least the same number of bytes as a
short, and a long uses more or at least the same
number of bytes as an int.
• short age = 20;
• int salary = 65000;
• long price = 4500000;
CONT….
• Real Numbers
A real variable may be defined to be of type float or
double. The latter uses more bytes and therefore
offers a greater range and accuracy for representing
real numbers. For example, on the author’s PC, a
float uses 4 and a double uses 8 bytes.
• float interestRate = 0.06;
• double pi = 3.141592654;
• CHARACTERS
A character variable is defined to be of type char.
A character variable occupies a single byte which
contains the code for the character. This code is a
numeric value and depends on the character coding
system being used (i.e., is machine-dependent).
char ch = 'A';
CONT….
• Strings
A string is a consecutive sequence (i.e., array) of
characters which are terminated by a null
character. A string variable is defined to be of
type char* (i.e., a pointer to character). A
pointer is simply the address of a memory
location.
char *str = "HELLO";
CONT….
• Names
Programming languages use names to refer to
the various entities that make up a program. We
have already seen examples of an important
category of such names (i.e., variable names).
CONT….
• C++ imposes the following rules for creating
valid names (also called identifiers). A name
should consist of one or more characters, each
of which may be a letter (i.e., 'A'-'Z' and 'a'-'z'),
a digit (i.e., '0'-'9'), or an underscore character
('_'), except that the first character may not be
a digit. Upper and lower case letters are
distinct.
CONT….
• salary // valid identifier
• salary2 // valid identifier
• 2salary // invalid identifier (begins
with a digit)
• _salary // valid identifier
• Salary // valid but distinct from
salary
++ keywords
as continue float new signed try
% Remainder 13 % 3 // gives 1
• Relational Operators
C++ provides six relational operators for comparing
numeric quantities.
Operator Name Example
== Equality 5 == 5 // gives 1
!= Inequality 5! = 5 // gives 0
< Less Than 5 < 5.5 // gives 1
<= Less Than or 5 <= 5 // gives 1
Equal
> Greater Than 5 > 5.5 // gives 0
>= Greater Than or 6.3 >= 5 // gives 1
Equal
• Logical Operators
C++ provides three logical operators for combining
logical expression.
Operator Name Example