Академический Документы
Профессиональный Документы
Культура Документы
Provides simple access using namespace std; int main() { cout << "Hello world!" << endl; return 0; } Ends executions of main() which ends program Insertion statement
Function
Greeting Output
Comments in C++
Escape sequences
Escape Sequence \n \t \r \a \\ \ \
Description
New Line Tab space Carriage return Alert. Sound system bell Print back slash character Print single quote character Print double quote character
Output:
Hello Welcome to C++
Tab space \t
#include<iostream> using namespace std; void main (void) { cout << "Hello \n"; cout<<Hello! \t Welcome to C++; } Output:
Hello Hello!
Welcome to C++
Activity 1
Data Types
A computer program operates on data and produces an output. In C++, each data must be of specific data type. The data type determines how the data is represented in the computer and kind of processing the computer can perform on it.
Types of data
10
Integers (int)
11
Float
12
Character
13
boolean
Boolean or Flag type is a type which can represent only two values: 0 and 1, usually identified with false and true respectively. This type can be stored in memory using a single bit. e.g.
14
Variables
A storage box, its type defines the kind of stuff you store in it
15
Variable
16
Memory in Computer
First you ask computer to give you a box and you also have to specify what type of box you want
The first character must be letter or underscore ( _ ). You can use upper and lowercase letters and digits from 1 to 9. Identifier can be as long as you like but only the first 250 character are recognizable in C++ Compiler.
//define var1 //define var2, var3 //assign value to var1 //assign value to var2 //displaying the sum of var1 + 10 Output: Result=30
Area is 12.56
Summary
Constant
values used in a program that are not changed during the course of program. e.g.
Circumference=2*pi*r. area=pi*r*r
23
Defining constant
main() { const int i = 5; i = 10; //error, can not modify constant i++; // error, can not modify constant }
cin with insertion operators >>. cin>> tells computer to wait for input from user (keyboard). When a value is entered, it needs to be stored/boxed in the appropriate variable.
Input example
26
Activity 2
Input three integer values from keyboard into variables a, b and c.
Activity 3
Assign the product of variables b and c to a as entered by the user.
Arithmetic operators
29
Operator precedence
() * / % + -
30
Activity 4
What prints when each of the following c++ statement is executed? Assume x=2 and y=3.
1.
2.
3.
4.
5.
Activity 5
Write a program that calculates the square of a number entered by the user and displays output as below.
Value Square
16
32
Increment Operators
You often need to add 1 to the value of an existing variable. Normal way to do this is count = count + 1; Or using an arithmetic assignment operator
count += 1;
Another approach is
++count; The ++ operator increments its arguments.
Increment Operators
Prefix and Postfix The increment operator can be used in two ways
Prefix: meaning that the operator precedes the variable Postfix: meaning that operator follows the variable.
2.
Activity 6
What would be the output int i=6, j=2; cout<<i<<endl; cout<<i++<<endl; cout<<++i<<endl; cout<<j++<<endl; cout<<++i-j<<endl; cout<<i-j--<<endl;
Output
6 6 8 2 6 6
rules that are used for implicit conversion of floating point and integer values in C++ are;
An arithmetic operation between an integer and integer always yields an integer result. Operation between a real and real always yields a real result
In some Cases, it may happen that the type of the expression on the right hand side and the type of the variable on the left hand side of the assignment operator may not be same.
In that case the value of the expression is promoted or demoted depending on the type of the variable on left hand side of assignment operator.
As 35.9 is of float type it cannot be stored in i of int type. In this case float is demoted to an int and then value is stored in i. So the value of i will be 35. Same will happen in y i.e. 10 will be promoted to 10.00 and then will be stored in y.
In the above example, some of the operands are of type int and some of them are of type float. As we know during evaluation of the expression int will be promoted to float and result would be of type float. But when this float value is assigned to s, it is again demoted to an int and then stored in s.
Thank You