Statements in C++
An executable instruction is known as a statement. They can classified into two main categories:- Simple statements or expression statements.
- Complex statements(Control statements: next chapter).
1) Expression Statements:
Any expression ending with a semi-colon (;) is known as an expression statement. For e.g.:- a=b;
- A=5;
- a=b+c;
- a++;
Input statement:
'cin' is an object(variable) declared in 'iostream.h' header file that represents the standard input device i.e. keyboard. This variable together with >> (extraction operator here) can be used for reading the value of a variable from the keyboard.Syntax:
cin>>variable_name; --> this is an expression.
cin>>variable_name; --> this is a statement known as input statement.
e.g.
int a;
cin>>a; //to read value of a
General syntax:
cin>>variable1>> variable2>>...>> variable.
Constants:
In C++ constants are classified into two categories:
1) Numeric constants:
2, 5, -45, 1.1245, 1.542e+4, etc.
2) Non-numeric constants:
- Character constants --> Any character within ' ' is a character constant. E.g.: 'A', 'b', '6', '+'.
- String constants --> Any sequence of characters within " " is known as a string. E.g.: "Hello world", "12+152".
Output statement:
'cout' is an object that represents the standard output device i.e. monitor. Using cout together with << (insertion operator here), we can display the value of a variable or a constant or an expression.Syntax:
cout<
cout<
- cout<<"hello";
output --> hello
- cout<<"Hello";
cout<<"Hello";
output --> HelloHello
- cout <<"Hello\n";
cout<<"Hello"
output-->Hello
Hello
Note -->'\n' is a special character constant known as new line character that brings the cursor to the next line.
- Let a=5, b=6
cout << a;
cout << b;
output --> 56
- cout<<"Value of a= " << a;
cout<<"Value of b= " << b;
output --> Value of a= 5Value of b= 6
CACKLE comment system