Tricks and Tips || C++ Programming Tips

Important Tricks and Tips for Beginners Programmers || C++ Programming Tips 

Topic Datatypes

Tips and Tricks: 
                                          while working on programming languages datatypes hold very important place as they decide what to keep in your variables for instant they are value specifiers and they can only store specific values such as integer hold only integer value ranges -infinity to positive discrete values  same range will hold by the float and double but float holds pointer values and double can hold 0.00 more values after point other data types such as bool can hold only 0 or 1 (true or false) values last but not least char can hold one alphabet at a time.

now some important things:

if we have integer variable for example:

  int a=10;

it holds integer value and if we assign it to float variable it will remain the same but if we have float variable and initialize it as below:

float a=10.0;

and if we assign it to the integer typed variable then we will only have discrete value we will lose after point values such as

int a=0;

float b=10.10

 a=b;

 now a will have only 10.

conditional statements:
                                                           All conditional statements work on the logical statements such as a>b and 10>= 100 they also work well on the digits and single alphabets we can also use only single-digit and alphabet in the conditional box of the conditional statements. below given example and syntax will show you what it means and how we can do this:

from the past Boolean datatype concepts we have assigned some logical statement to the bool variable, it will return us either zero or one which means either it true (1) or false (0). every Boolean statement returns the same such as 0 or 1. So, it's mean our conditional statements are also worked on the same principle they compute the logical statement in their condition box if(condition box) and return 0 or 1 then if-statement decides it should work or not.

syntax:
int a=1;
if(a>10)  // firstly a>10 will compute and return 0 or 1 to the                         condition box then if decide either it true or false 
{
    cout<<"here im"<<endl;
}
else
{
   cout<<"I'm in else"<<endl;
}
Note: we all know 0 means false and 1 means true so condition statement takes it the same as we discussed if(0) then it false, true otherwise.

Hypothesis:
                      if-statement does not work at zero but it works on every negative and positive value.

prove:
               it considers 0 as false and true every value other than zero 
  


while loop:
                   loops repeat instruction multiple times instructions are programming statements written inside the loop's body.

syntax of the while loop:
                          
  while( condition box)
{
   execute programming instructions//  body of the loop;
}

how while executes:

in while loop we have condition box which defines how many times our loop will execute means it holds starting and ending conditions of the loop for this purpose we need a loop variable:

int i=0;
while(i<10)
{
   statments;
   i++;
}

you can see we have me as a loop variable that controls the loop and increment I tell to add one in me every time loop condition satisfy.

how exactly loop works:
  
the loop has 4 parts such as condition part, initialization part, body part, and increment part.

int i=0;    -------------> initialization part
while(i<10)------------>conditional part 
{
   statements; ......................> body part 
   i++;........................................................>increment part 
}


execution of different parts:

  1. * initialization part will execute only once.
  2. * conditional part will execute after initialization part 
  3. * body part only executes if conditional part satisfies if not loop will terminate.
  4. * after all the body part's instructions increment part will execute.
  5. *after increasing part execution conditional part will again execute and if it satisfies then repeats steps 3.


int i=0;    -------------> initialization part  (1)
while(i<10)------------>conditional part (2)
{
   statements; ......................> body part  (3)
   i++;........................................................>increment part (4)
}



Example Program's Dry Run:

#include<iostream>
using namespace std;
int main()
{
   int a=0;
   while(a<10)
{
       cout<<a;
       a++;
}
what will be the output of the program:

for Dry run see the above section "execution of different parts".

Do-while loop:
                   loops repeat instruction multiple times instructions are programming statements written inside the loop's body.

syntax of the while loop:
                          
Do 
{
   execute programming instructions//  body of the loop;
} while( condition box)


While VS Do-while:
                                  while loop condition box always executes first if the condition true loop executes the body if not it simply terminated but on the other hand's do-while loop executes one time even if the condition not true or satisfy.

Important note for loops:
     Every loop can be converted to any other loop that means all loops are the same by their operation but they are different by their syntax.                      


post-increment and post-decrement:

int i=0;
i++;  // post increamnet statement this is also equals to i=i+1;
i--; //post decrement statement this is also equals to i=i-1;

how they actually execute: 
as their name tells us that post-increment and decrement mean first the use it as same then they incremented or decremented for an instant:

Example:
 int i=10;
cout<<i++; //post-increment at this line value of i is 10 but after this line value of i will be 11 because of post-increment  
cout<<i; //value of i is 11 
cout<<i--; //post-decrement at this line value of i is 11 but after this line value of i will be 10 because of post-decrement 
cout<<i; //value of i is 10


For loop:
                   loops repeat instruction multiple times instructions are programming statements written inside the loop's body.

syntax of the For loop:
                          
for (initialization box ; condition box ; increament/decreament box)
{
      loop's body;

}
example:
printing 10 digits from 0-10 

for(int i=0;i<10;i++)
{
  cout<<i;
}

The special thing about for loop:
 any variable that initialized in the loop's initialization box can only be used in the body of the loop you can't access that variable outside the loop's body. 

how for loop executes:
for( 1 ;2; 4)

    3;
}
steps:

  1. part 1 is initialization box that only executes once loop never loop back to the part 1
  2. part 2 is a condition part that executes every time after the initialization box executed for example every time part 2 executes if it true then loop enters into part 3.
  3. part 3 is the body of the loop it executes after part 2 (if it true)
  4. part 4 will execute after part 3.
  5. repeat step 2 until the condition got false. 
    For Loop Execution 
                            

1 Comments

  1. Very informative sir Whole C++ in just one blog....
    Awesome!!!

    ReplyDelete

Post a Comment