Decision Making Statement in C++

In the C++ language decision making statement executes if the given condition is  true otherwise conditional block will never execute.

C++ language assumes non-zero and non-null values as true, and zero or null is assumed as false value.

There are following types of decision making statements in C++ programming language.

if statement

Syntax of an ‘if’ statement −

if(Condition) {
/* statement will execute if the condition is true */
}

In a ‘if’ statement if the Condition is true, then the block of statement of the ‘if’ statement will be executed.

If the Condition is false, then the block of statement of the ‘if’ statement will not executed and control sent to the next line of if block.

Example: Write a program to demonstrate the ‘if’ statement.

Output

In the above program if reverse the ‘if’ condition of ‘if’ statement then the condition will be false and ‘if’ block will never execute.

Output

if-else  statement

Syntax of an ‘if-else’ statement −

if(Condition) {
/* if-statement will execute if the Condition is true */
}
else
{ if the condition is false then else block will execute
}

In a ‘if-else’ statement if the Condition is true, then the block of statements of the ‘if’ statement will be executed.

If the Condition is false, then the block of code of the ‘else’ statement will executed.

Example: Write a program to  take two numbers from user and find the grater between them using ‘if-else’ statement.

Output

Nested -if statement
In a C language we can use if statement inside another if statement(s).
Syntax of an ‘nested -if’ statement −


if(condition 1) {
/* statement will execute if the condition 1 is true */
If (condition 2) { /* statement will execute if the condition 2 is true */
}
}

Example: Write a program to take two numbers from user if both number are between 1 to 9 then print “Good” using nested-if statement.

Output

Nested if-else statements

In a C language we can use  if or if-else statement inside another if or  if-else statement(s).

Syntax of an ‘if-else’ statement −

if(condition 1) { /* statement will execute if the condition 1 is true */
If (condition 2) {
/*if- statement will execute if the condition 1 & 2 both are true */
}
else {
/* else-statement will execute if the condition1 is true Condition2 is false */
}
}
else {
If (condition 3) {
/* statement will execute if the condition 3 is true */
}
}

Example: Write a program to  take three numbers from user and find the grater among them using ‘nested if-else’ statement.

Output

Categories C++

Operators in C++

An operator is a symbol.

It is used to tells the compiler to perform specific mathematical or logical functions.

C++ language have a following types of built-in operators:−

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators

 

Arithmetic Operators

C++ language have a following arithmetic operators.

Assume variable i holds 100 and variable j holds 200 then –

Operator Description Example
+ Adds two operands. i + j = 300
Subtracts second operand from the first. i − j = -100
* Multiplies two operands. i * j = 20000
/ Divide. j / i = 2
% Modulus Operator returns the remainder of integer division. j % i = 0
++ Increment operator increases the value of variable by one. i++ = 101
Decrement operator decreases the value of variable by one. i– = 99

Example : Write a C++ program to demonstrate the various arithmetic operators.

Output

Increment and Decrement Operator

  1. Increment operator      ++
  2. Decrement operator    —

In a C++ language, Increment operator (++)  increases the value of variable by one and Decrement operator  (–) decreases the value of variable by one.

There are two form of increment  and decrement operator:

  1. Prefix form
  2. Postfix form

Prefix Form : In the prefix expression operator appears in the expression before the operands.

Example : ++A 

In the prefix form first the value of operand is increment or decrement than the value of operand is used in expression.

For Example: int  i = 10;
      j = ++i ;    // j=11 , i=11

Postfix Form: In the postfix expression operator appears in the expression after the operands.

Example : A++

In the postfix form first the value of operand is used in expression than value of operand is incremented or decremented.

For Example: int  i = 10;
    j = i++ ;    // j=10, i=11

Example: Write a C++ program to demonstrate the increment and decrement operator. 

Output

Relational Operators

C++ Language has a following relational operators.

For example: Assume variable A holds 100 and variable B holds 200 then −

Operator Description Example
== Equals to (A == B) is not true.
!= Not equal (A != B) is true.
Greater than (A > B) is not true.
Less than (A < B) is true.
>= Greater than or equals to (A >= B) is not true.
<= Less than or equals to (A <= B) is true.

Example:  Write a C++ Program to find the largest of two numbers.

Output

Logical Operators

There are three basic logical operators available in C++ language.

Assume variable A and B holds 1 and 0 respectively then –

Operator Description Example
&&  AND operator (the condition becomes true if both the operands are non-zero) (A && B) is false.
||  OR Operator (the condition becomes true if any of the two operands is non-zero) (A || B) is true.
!  NOT Operator (this operator reverse the logical state of operand. For example: If condition is true, then NOT operator will make it false) !(A && B) is true.

Example:  Write a C++ Program to find the largest among three numbers using AND logical operator.

Output

Bitwise Operators

In a C++ programming language Bitwise operator works on bits and perform bit-by-bit operation.

There are following bitwise operators in C++:-

Operator Description
& Binary AND Operator
| Binary OR Operator
^ Binary XOR Operator
~ Binary One’s Complement Operator is unary
<<  Binary Left Shift Operator
>>  Binary Right Shift Operator

The truth tables for &, |, and ^ is as follows –

a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume A = 60 and B = 13 in binary format, they will be as follows −

               A in binary     = 0011 1100         (60)                     

              B in binary     = 0000 1101          (13)

                               —————–

Bitwise AND (&)

                      A      = 0011 1100         (60)                

          &         B      = 0000 1101          (13)

———————————————————

                                0000 1100           (12)

Bitwise OR (|)

                      A      = 0011 1100         (60)                

          |          B      = 0000 1101          (13)

———————————————————

                                0011 1101           (61)

Bitwise XOR (^)

                      A      = 0011 1100         (60)                

          ^         B      = 0000 1101          (13)

———————————————————

                                0011 0001           (49)

Binary Left Shift Operator

Left shift operator shift all the bits in a left direction to specified number of times.

Exapmle:

Output:

Output

Exapmle:  Write a program to demonstrate Bitwise operator.

Output

Assignment Operators

In a C++  language there are following assignment operators  −

Operator Description
= assignment operator (it assigns values of right side operands to left side operand )
+= Add assignment operator
-= Subtract assignment operator
*= Multiply  assignment operator
/= Divide assignment operator
%= Modulus assignment operator
<<= Left shift assignment operator
>>= Right shift assignment operator
&= Bitwise AND assignment operator
^= Bitwise exclusive OR assignment operator
|= Bitwise inclusive OR assignment operator

Add Assignment Operator

Example:

Output

Subtract Assignment Operator

Example:

Output

Multiply  assignment operator

Example:

Output

The ?  Operator

This operator also known as conditional operator . It is similar to if-else statement.

Syntax of ? operator

 Expression1 ? Expression 2 : Expression 3;

There are three expressions in ? operator, Expression1, Expression2, and Expression3 .

The value of a ? expression is determined like this −

  • If Exp1 is true, then Exp2 is evaluated.
  • If Exp1 is false, then Exp3 is evaluated.

Example: write a program to find the grater between two number using ? operator.

Output:

Example: write a program to find the grater among three number using ? operator.

Output

Categories C++

Basics of C++

Tokens in C++

In a C++ language, token is either a keyword, a symbol ,an identifier, a constant or a string literal.

A C++ program consists of various tokens for example, the following C++ program consists of four tokens

The individual tokens are −

Comments

Comments are the text in C++  program language and they are ignored by the compiler during compilation and execution.

They start with /* and terminate with the characters */ as shown below −

/* first C ++ program  */

Anything written within Comment will never execute. You can’t write comments within comments.

This is a multi line comment.

We can also use single line comment. 

Single line comment start with // and continue until the end of the line.

Identifiers

In a C++ language, identifier is a name given to any variable, function, or any other user-defined item.

In C++  language identifier starts with a letter A to Z, a to z, digits (0 to 9) or an underscore ‘_’ followed by zero.

C++ language does not allow punctuation characters such as @, $, and % within identifiers.

Here are some examples of acceptable identifiers −

Keywords

In a C++ language keywords are the reserved\predefined words whose meaning already explained in a compiler.

These reserved words can’t be used as a variables or identifier names.

There are 60 key words currently defined for C++.

C++ Keywords
Asm auto bool break case
catch char class const_cast continue
default delete do double else
enum dynamic_cast extern false float
for union unsigned using friend
goto if inline int long
mutable virtual namespace new operator
private protected public register void
reinterpret_cast return short signed sizeof
static static_cast volatile struct switch
template this throw true try
typedef typeid unsigned wchar_t while
Categories C++

Variables in C++

Variables is a name given to memory location. 

            int x =10;                    

here, x is a integer variable.                                                    

Integer variable x is a name given to memory location and where we stored integer value10.

Initialize variable

we can also initialize variables as below.

int  a,b,c;

a=10;
b=20;
c=30;

Here, we have initialized three integer variable a,b & c. variable  ‘a’  stores 10, variable ‘b’ stores 20 and variable ‘c’ stores 30.

Initialize multiple variable

we can also initialize multiple by comma separated list.

int a;             
char b;       
float c;
a=10;
b=’S’;
c=11.23

How to take input from user in C++

In C++ we can take a input from user by using “cin”.

In C++ , “cin” is an  object of class istream. 

“cin” object  is used to take the input from the standard input device i.e. keyboard.

In “cin” ,   “c” refers to “character” and ‘in’ means “input”, hence cin refers “character input”.

“cin” is used with the extraction operator (>>) for receive a stream of characters from user.

Syntax  of cin is:

Example

How to take multiple input from user in C++

In C++ we can also take a multiple input at a time by using cin object.

Syntax to take multiple input at time by using cin.

cin >> Variable1 >> Variable2 >> … >> VariableN;

Example:

Output:

Categories C++

Data Type in C++

There are four types of data type in C language.  They are as follows:

TypesData Types
Basic data typesint, char, float, double, boolean
User Defined data type structure, union, enum
Derived data type Pointer, array, function

Basic Data Types In C Language

Integer Data Type

Integer data ( example 1,2,3,4,5,6, ….. etc. ) is stored in int ,short and long data type.

Type Storage size Value range
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Example: Addition of integer numbers

OUTPUT

Character Data Type

Character data ( example ‘a’ , ‘b’ , ‘f’, ….. etc. ) is stored in “char” data type.

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127

Example: Declare and initialize integer variable.

OUTPUT

Float Data Type

Floating point value (example  11.23, 333.3330 etc. ) can be stored in data type float and double. Small floating value can be stored in float and large floating stored in double. 

Type Storage size Value range Precision
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places

Example: Declare and initialize float & double variable.

OUTPUT

Categories C++

Simple Program in C++

A simple C++ program basically consists of the following parts −

  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

Basic structure of C++ program

Above shown the basic structure of C++ program.

For every program we have to writeabove mention basic structure.

How to write or print

We can write or print in C++ language by using “cout” object.

It is used to display the output to the standard output device i.e. monitor.

In C++, cout is an object of class ostream.  for example: code to write “hello” is as follows:

cout << “hello”;

In “cout” ,  “c”  refers to “character” and ‘out’ means “output”,  means cout refers “character output”.

cout object is used along with the insertion operator (<<)  for display a stream of characters.

A simple C program to print “Hello”

OUTPUT

Description

  • First line  #include <iostream.h> is a preprocessor, which instruct to C++ compiler to include iostream.h file. iostream ( input output stream ) header file contains definitions of objects like cin, cout etc.
  •  int main() is the main function from where the program execution begins.
  •  /*………..*/  is a “comments” in the program. This comments will be ignored by the compiler during execution of program
  • cout is an object of ostream in C++ which is used to print message.
  •  line return 0; terminates the main() function and returns the value 0.return 0 tells the compiler that everything is ok and the execution of main() function ends. Because 0 is the code for successful execution, while values greater than 0 (integers) is an indication that something went wrong. 
Categories C++

Introduction of C++

C++ was developed by Bjarne Stroustrup in 1979.

C++ is both procedural  and object oriented programming language.

C++ is a programming language that contains the feature of C programming language as well as includes a concept of object Oriented language. C++ programming  introduced the concept of Class and Objects.

C++  is a  high-level language which can be easily read and written by any one.

C++ is a case-sensitive programming language. Thus, “HELLO” and “Hello” two different identifiers in C.

Differences between C and C++

CC++
C language was developed by Dennis Ritchie in 1972 at AT&T Bell Labs.C++ was developed by Bjarne Stroustrup in 1979.
C is procedural programming language.C++ is both procedural and object oriented programming language.
C is a function-driven language.C++ is an object-driven language
The file extension of a C program is .cThe file extension of a c+ + program language is.cpp
C programming does no support polymorphism, encapsulation, and inheritance because C language does not support object oriented programming. C++ programming supports polymorphism,encapsulation, and inheritance because it is an object oriented programming language.
C language contains 32 keywords. C++ language contains 52 keywords.
C does not support information hiding.C++ support data hiding by the Encapsulation .
C language does not support function and operator overloading. C++ language support function and operator overloading.
Header file used by C is stdio.h.Header file used by C++ is iostream.h.
Virtual and friend functions are not supported by C.Virtual and friend functions are supported by C++.
C does not support inheritance.C++ supports inheritance.
C language uses malloc() and calloc()functions for dynamic memory allocation, and free() for memory de-allocation.C++ language uses new operator for memory allocation and delete operator for memory de-allocation.
In C language, variable should be defined at the beginning of the program. In C++ we can declare variables anywhere in the function.
for input/output in C language uses a scanf() and printf() functions .cin and cout are used for input/output in C++ language.
Categories C++