learncpponline

Control Statements in C Plus Plus (C++)

Control statements alter the flow of execution of the programs. Control statements can be broadly divided into three categories: Decision-making or Conditional Statements (Branching and Selection) if statement if-else statement switch statement Loop Statements for statement while statement do-while statement Breaking Control Statements break statement continue statement goto statement

Preprocessor Directives in C++

The C++ preprocessor is a collection of special statements, called directives, that are executed at the begenning of the compilation process. The preprocessor is usually an integral part of our compiler. It is a process that is executed by the compiler before our C++ program code is compiled into machine instructions. The job of the preprocessor is to prepare our source code, proper for the compile phase, according to the instructions that we have included in the source files. These instructions are called preprocessor directives. All preprocessor directives begin with the symbol #, so they are easily distinguishable from C++ language statements. Given below is the complete set of preprocessor directives: Directive Function #include Supports header file inclusions #if, #else, #elif, #endif, #if defined (or #ifdef), #if !defined (or #ifndef) Enables conditional compilation #define, #undef Enable symbol and macro substitution #line Allows redefinition of current line and filename #error Produces...

Input and Output in C++

Output in C++: Output in C Plus Plus (C++) is enabled by the object cout (pronounced as “C out”) which is predefined to corresponding to the standard output stream. A stream is an abstraction that refers to a flow of data. The standard output stream normally flows to the screen display, although it can be redirected to other output devices. cout, when combined with insertion or put-to operator << enables the output in C++ programs. << operator redirects the contents of the variable on its (<<‘s) right to the object on its(<<‘s) left. A simple example of output of a phrase is depicted below: cout << “LearnCppOnline.com is the best C++ programming tutorial site”; Above statement causes the phrase in the quotation marks to be displayed on the screen. Input in C++ Contrary to cout, to receive input through the keyboard what is used is object cin (pronounced as “C...

Type Conversion in C++

Type Conversion means conversion of one data-type to another data-type. In C Plus Plus (C++), type conversion can be done by two ways: Automatic Conversion Type Casting 1) Automatic Conversion: In C++, if we have mixed mode expression (i.e., different types of data in one expression) then the lower data-type variable is automatically converted to the data-type of the higher data-type variable. Let us illustrate this with am example as follows: int c = 7; float a = 155.5; double t = c * a; In the above example, in expression: double t = c * a; firstly, variable c of the type int is converted to type float and is stored in a temporary variable before being multiplied by variable a of type float. Also, the result of multiplication c * a which comes in type float is then converted to type double and then is assigned to variable...

Conditional Operator in C++

Simple conditional operations can be carried out with the conditional operator (? :). An expression that makes use of the conditional operator is called a conditional expression. Such an expression can be written in place of the more traditional if – else statement. A conditional expression is written in the form: expression 1 ? expression 2 : expression 3 While evaluating a conditional expression, expression 1 is evaluated first. If expression 1 is true (i.e., if its value is non-zero), then expression 2 is evaluated and this becomes the value of the conditional expression. However, if the expression 1 is false (i.e., if its value is zero) then expression 3 is evaluated and this becomes the value of the conditional expression. Note that only one of the embedded expressions (either expression 2 or expression 3) is evaluated determining the value of a conditional expression. Example: In the conditional expression below....

Assignment Operators in C++

There are several different assignment operators in C Plus Plus (C++). All of them are used to form assignment expressions, which assign the value of an expression to an identifier. The most commonly used assignment operator is =. Assignment expressions that make use of this operator are written in the form: identifier = expression where, identifier generally represents a variable, and expression represents a constant, a variable or a more complex expression. Example: a = 3; x = y; sum = a + b; In the above statements, the first assignment expression causes the integer value 3 to assigned to the variable a, and the second assignment causes the value of variable y to be assigned to x. In the third assignment, result in the value of the arithmetic expression is assigned to the variable sum i.e., the value of a + b is assigned to sum. Remember that the...