2 . C++ Programming Basics

Basic Data Type

All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data it can store.The compiler allocates some memory for that variable based on the data-type with which it is declared. Every data type requires a different amount of memory.
Types of Data type:-

Data types in C++ is mainly divided into three types:-

  1.  Primitive data types:-These data types are built-in or predefined data types and can be  used directly by the user to declare variables. example: int, char , float, bool etc.    Primitive data types available in C++ are:-
    • Integer
    • Character
    • Boolean
    • Floating Point
    • Double Floating Point
    • Valueless or Void
  2. Derived Data Types:- The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types.
      These can be of four types namely:-
  • Function
  • Array
  • Pointer
  • Reference
3.Abstract or User-define data type:-These data types are defined by user itself.Likedefining a class in C++ or a structure.
C++ provides the following user-defined datatypes:-
  • Class
  • Structure
  • Union
  • Enumeration
  • Typedef defined DataType
  Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C/C++ language is rich in built-in operators and provides the following types of operators:−

1. Arithmetic Operators

Scope resolution(::):-
The :: (scope resolution) operator is used to get hidden names due to variable scopes so that you can still use them. The scope resolution operator can be used as both unary and binary. You can use the unary scope operator if a namespace scope or global scope name is hidden by a particular declaration of an equivalent name during a block or class. For example, if you have a global variable of name my_var and a local variable of name my_var, to access global my_var, you'll need to use the scope resolution operator.

Example:-
#include <iostream>  
using namespace std;  

int my_var = 0;
int main(void) {
   int my_var = 0;
   ::my_var = 1;  // set global my_var to 1
   my_var = 2;    // set local my_var to 2
   cout << ::my_var << ", " << my_var;
   return 0;
}

Output:-
               1, 2

 CONTROL STATEMENTS



Conditional Statements:-

1). if else statement

    If the test expression is evaluated to true,
  • statements inside the body of if are executed.
  • statements inside the body of else are skipped from execution.

If the test expression is evaluated to false,

  • statements inside the body of else are executed
  • statements inside the body of if are skipped from execution
    Syntax:-
if (test expression) {
    // statements to be executed if the test expression is true
}
else {
    // statements to be executed if the test expression is false
}

2). Nested if else
     It is possible to include an if...else statement inside the body of another     if...else statement.
    
     Syntax:-
if(condition) {
    //Nested if else inside the body of "if"
    if(condition2) {
       //Statements inside the body of nested "if"
    }
    else {
       //Statements inside the body of nested "else"
    }
}
else {
    //Statements inside the body of "else"
}

3). Switch case
The switch statement allows us to execute one code block among many alternatives.
The expression is evaluated once and compared with the values of each case label.
  • If there is a match, the corresponding statements after the matching label are executed. For example, if the value of the expression is equal to constant2, statements after case constant2: are executed until break is encountered.
  • If there is no match, the default statements are executed.
Syntax:-
switch (expression)
​{
    case constant1:
      // statements
      break;

    case constant2:
      // statements
      break;
    .
    .
    .
    default:
      // default statements
}

Loop statements:-
A loop statement allows us to execute a statement or group of statements multiple times.loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

How it Works

As per the above diagram, if the Test Condition is true, then the loop is executed, and if it is false then the execution breaks out of the loop. After the loop is successfully executed the execution again starts from the Loop entry and again checks for the Test condition, and thiskeeps on repeating.

The sequence of statements to be executed is kept inside the curly braces { } known as the Loop body. After every execution of the loop body, condition is verified, and if it is found to be true the loop body is executed again. When the condition check returns false, the loop body is not executed, and execution breaks out of the loop.

Types of Loop:-

1)  for loop



Jumping statement
1) break statement:-The break is a keyword in C/C++ which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement.

Syntax:-
// loop or switch case
 break;

2). Continue statement:-The continue statement in C/C++ language is used to bring the program control to the beginning of the loop. The continue statement skips some lines of code inside the loop and continues with the next iteration. It is mainly used for a condition so that we can skip some code for a particular condition.
Syntax:-
//loop statements  
continue;  
//some lines of the code which is to be skipped  

3).goto statement:-The goto statement is known as jump statement in C/C++. As the name suggests, goto is used to transfer the program control to a predefined label. The goto statment can be used to repeat some part of the code for a particular condition. It can also be used to break the multiple loops which can't be done by using a single break statement. However, using goto is avoided these days since it makes the program less readable and complecated.

Syntax:-
label:   
//some part of the code;   
goto label;  


Post a Comment

0 Comments