Mail us : hexainclude@gmail.com
Hexainclude

Menu

Skip to content
  • HOME
  • C/C++
    • C
    • C++
  • OS
  • RDBMS
  • JAVA
  • PHP
  • WORDPRESS
  • DOWNLOAD
    • eBOOKS

CPP

01Jul/16

Structure in C++

July 1, 2016CPPc_structure, structure_in_c++DreamMaker

Structure in C++: Structure is a user-defined data type. The format of the structure is known as template and we can create multiple structure variables from template. Difference between C Structure andRead More…

10Jun/16

Structure of C++ program

June 10, 2016CPPDreamMaker

The general structure of C++ program can be shown in following diagram. In C++ program, first section is the include section It contains pre-processor statements to include various header (library) files. AfterRead More…

10Jun/16

Introduction to C++

June 10, 2016CPPi/o_statements, introduction_to_c++DreamMaker

C++ is an object-oriented programming language developed by Bjarne Stroustrup at AT & T Bell Laboratories in New Jersey, USA, in 1980. It is developed from Simula67 and C programming language. TheRead More…

10Jun/16

Features of OOP

June 10, 2016CPPencapsulation, features_of_oop, inheritance, message_passing, polymorphism, types_of_languageDreamMaker

Data abstraction: Abstraction refers to give the general view. We represent thing without going into the background details. Class contains abstract data members such as size, weight, height and function to operateRead More…

10Jun/16

Object-Oriented Programming Language (OOP)

June 10, 2016CPPcharacteristics_of_oopDreamMaker

In object-oriented programming we divide program into multiple object. “An Object is a thing in real world which has certain properties and method.” It may be any place, person, bank account, billRead More…

10Jun/16

Procedure-Oriented Programming Language(POP)

June 10, 2016CPPprocedure_oriented_programming_structure, Structor_of_popDreamMaker

Programming in the high-level languages such as COBOL, FORTRAN, C, etc. is known as procedure-oriented programming. Procedure-oriented programming basically contains group of instructions known as function. There are multiple functions into theRead More…

Posts navigation

  • « Previous
  • 1
  • 2
  • 3
Download Android App

Recent Posts

  • PHP Functions
  • PHP Arrays
  • Safe Working-Practice
  • COPA Trade Introduction
  • NESTED STRUCTURE
  • In the previous post, we discussed about different file operations. In this post, we will try to understand what the file system is.

Files system provides the mechanism for storing and accessing both data and programs of o/s.

  • The file system consists of three parts:

1)  Files

2)  Directory Structure

3)  Partition

Files

  • A collection of files each storing related data.

Directory Structure

  • It organizes and provide information about all the files in the system.
  • The computer file system can be very expensive.
  • Generally system stores all the files on the disk.
  • To manage all these files and data we need to organize them.
  • We can organize data by two ways:  1) Partition 2) Directories

Partition

  • A partition can be created to split large disk area into sub-partitions either physically or logically.
  • Disks are split into one or more partition also known as minidisk in the IBM system or volumes on the pc.
  • Each disk on a system contains at list one partition which is a low level structure in which files and directories are stored.
  • Sometimes partitions are used to provide several separate areas within one disk that can be treated as separate storage device.
  • Partition can be considered as virtual disk.
  • It can also store multiple o/s allowing a system to boot and run more than one o/s.

Directories

  • Each partition contains information about files within it.
  • This information is kept in a device directory or in a volume.
  • Each partition contains a directory structure that holds information about the files stored on that partition.
  • A directory can be viewed as a symbol tables that translate file name into their directory and disk.

File System

  • There are different schemes for defining logical structure of the directory system are available.
  • While defining any particular directory structure we need to keep in mind the operations that are to be perform on a directory.
  • There are following operations that can be perform on a directory.

Search for a File

  • Searching a directory structure to find for a specific file entry.

Creating a File

  • Whenever a new file is created, its entry should be recorded into the directory.

Deleting a File

  • Whenever a file is no longer needed its associative entry should be remove form a directory.

List a Directory

  • The directory structure should allow to list out all files in a directory and the content of directory entry for each file in the list.

Rename a File

  • Name of file represent it content to its users. The name must be changeable when the content or users of file changes.

Traverse a File

  • Traverse is an operation to access every directory and every files within a directory.
  • We may be able to access every directory and every file within a directory structure.

 

August 17, 2017
  • The basic definition of a recursive function can be given as below:

A function that calls itself is known as recursive function and the process in which a function calls itself is known as recursion.

Example of Recursion in C programming

  • To understand the concept of recursion consider the following program calculating sum of first n natural numbers using recursion.
  • Note: Positive integers are known as natural number i.e. 1, 2, 3….n
/* C program to calculate sum of first n natural numbrs.*/
 
#include "stdio.h"
int sum(int n);      // function prototype
void main()
{
    int num,add;
    printf("Enter a positive integer:\n");
    scanf("%d",&num);

    add=sum(num);       // function call
    
    printf("sum=%d",add);
}

/* Recursive function definition */
int sum(int n)    
{
    if(n==0)      /* condition to break recursion */
       return n;
    else
       return n + sum(n-1);    /* recursive call to function sum() */
}

Output:

Enter a positive integer:
5
15
  • In, the above program, sum() function is invoked from within the same function.
  • If n is not equal to 0 then, the function calls itself passing argument 1 less than the argument it was previously called with.
  • Suppose, n is 5 initially. Then, during next function calls, 4 is passed to function and the value of argument decreases by 1 in each recursive call.
  • When, n becomes equal to 0, the condition for breaking recursion evaluates to true and hence the value of n is returned which is the sum numbers from 5 to 1.
  • The below is the complete recursion calls for better visualization of recursion process:
sum(5)        // initial call to sum() function

=5+sum(4)

=5+4+sum(3)

=5+4+3+sum(2)

=5+4+3+2+sum(1)

=5+4+3+2+1+sum(0)

=5+4+3+2+1+0

=5+4+3+2+1

=5+4+3+3

=5+4+6

=5+10

=15
  • Note that there must be some way to end the recursion. In this example when, n is equal to 0, there is no recursive call and recursion ends.

Advantages

  • Recursion is more powerful and requires few variables which make program clean.
  • It can be used to replace complex nesting code by dividing the problem into same problem of its sub-type.
  • Recursion   can replace large program logic with a few line of code and thus, helps to reduce program code.

 Disadvantages

  • The major difficulty with recursion is that it is hard to think the logic of a recursive function.
  • It is also difficult to debug the code containing recursion.

Comparison of Recursion and iteration

Recursion Iteration
Recursion is the term given to the mechanism of defining a set or procedure in terms of itself. Iteration is the block of statement executed
repeatedly using loops.
A conditional statement is required in the body
of the function for stopping the function execution
The iteration control statement itself contains statement for stopping the iteration. At every execution, the condition is checked.
At some places, use of recursion generates extra overhead. so its better to skip when easy solution is available with iteration. All problems can be solved with iteration.
Recursion is expensive in terms of speed and
memory.
Iteration does not create any overhead. All the
programming languages support iteration.
  • While using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go in infinite loop.
  • Recursive function are very useful to solve many mathematical problems like calculating factorial of a number, generating Fibonacci series, etc
August 12, 2017

Break:

  • A break statement can be used to terminate or to come out from the loop or conditional statement unconditionally.
  • It can be used in switch statement to break and come out from the switch statement after each case expression.
  • Whenever, break statement is encounter within the program then it will break the current loop or block.
  • A break statement is normally used with if statement.
  • When certain condition becomes true to terminate the loop then break statement can be used.

Break

  • The following program demonstrates the use of break statement. Loop will be terminated as soon as the counter value becomes greater than 5.
<?php
           
   for( $i = 1; $i <= 10 ; $i++ )
   {
        if ($i > 5)
            break;    // terminate loop 
        echo "$i"."</br>" ;
   }

?>
  • break can also be used with an optional numeric argument to specify how many nested enclosing structures are to be broken out of.
  • The default value is 1, means only the immediate enclosing structure is broken out of.
<?php
        /* Using optional argument. */

$i = 0;
while ($i++)
{
    switch ($i)
    {
        case 5:
            echo "case 5 \n" ;
            break 1;     /* Exit only the switch. */
        case 10:
            echo "case 10; quitting \n" ;
            break 2;  /* Exit the switch and the while. */
        default:
            break;
    }
 }
?>

Continue:

  • A continue statement can be used into the loop when we want to skip some statement to be executed and continue the execution of above statement based on some specific condition.
  • Similar to break statement, continue is also used with if statement.
  • When compiler encounters continue, statements after continue are skipped and control transfers to the statement above continue.

Continue Statement

  • The following example uses the continue statement to print upper and lower a to z alphabets
<?php
   /* program to print upper and lower a to z alphabets using continue */

   for ( $i = 65 ; $i <= 122 ; $i++ )   // loop through ASCII value for a to z
   {
       if($i >= 91 && $i <= 96)
              continue ;       // skip unnecessary special characters.
       
          echo "| $i "."</br>" ;   // print character equivalent for ASCII value. 
   }
   
?>
  • Similar to break, continue also accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip.
  • The default value is 1, which will skip to the end of the current loop.
<?php
     $i = 0 ;
     while ($i++ < 5)
     {
        echo "Outer while\n" ;
        
        while (1)
        {
            echo "Middle while\n" ;
            
            while (1)
            {
                echo "Inner while\n" ;
                continue 3;
            }
            echo "This will never output.\n" ;
        }
        echo "Not even this.\n" ;
      }
?>

Exit:

  • An exit statement is used to terminate the current execution flow.
  • As soon as exit statement is found, it will terminate the program.
  • It can be used to output a message and terminate the current script: for example exit(“Good Bye!”);
  • It can also be used with error code. For example: exit(1), exit(0376).
  • the following program demonstrates the use of exit statements.
<?php

   $filename = 'sample.txt' ;
   $file = fopen($filename, 'r')  // open file for reading
        or exit("unable to open file ($filename)");
?>
August 7, 2017
  • An Exit Control Loop checks the condition for exit and if given condition for exit evaluate to true, control will exit from the loop body else control will enter again into the loop.
  • Such type of loop controls exit of the loop that’s why it is called exit control loop.

Exit Control loop

Do…While loop:

  • Do …while loop is an exit control loop.
  • It is a variation of while loop.
  • When we want to execute or perform some task at least for once, we can use do…while loop structure.
  • In a while loop if the expression becomes false at very first try then loop will never be executed.
  • Since, do…while is of type exit control loop it checks the condition at last so, first time the loop will be execute unconditionally.
  • If the condition for the exit becomes true then loop will be terminate otherwise it will be executed for the next time.
  • It has the following form:

Syntax:

do
{
    //loop body
} while (expression);
  • The main difference between while loop and do… while is as follow.
While loop Do…while loop
It is an entry control loop. It is an exit control loop.
In while loop expression/condition is checked at the time of entry. In do… while loop condition is checked at the time of exit.
It does not have semicolon (;) at the end of the expression. In do while loop semicolon (;) at the end of the expression is compulsory.
Syntax:
while(condition)
{
// loop body
}
do
{
// loop body
} while(condition);
Example:
while($i<=5)
{
echo “Value of i is $i <br/>” ;
$i++ ;
}
do
{
echo “Value of i is $i <br/> ” ;
$i++ ;
} while($i<=5) ;

The following program demonstrates use of do…while loop to to print value of i.

<?php
      $i = 0 ;
      do
      {
          echo 'Value of i is ' .  $i . '<br/>' ;
             
      } while($i > 0 ) ;
?>
   
  • The above loop will run only once. The first time it will run unconditionally and second time the loop will be terminated as the given expression evaluates to false.
July 9, 2017
  • An entry control loop checks the condition at the time of entry and if condition or expression becomes true then control transfers into the body of the loop.
  • Such type of loop controls entry to the loop that’s why it is called entry control loop.

Entry Control Loop

  • The following are the main looping statements available in this category:

While loop

  • The while loop is the simplest looping structure
  • It is of entry control looping structure
  • If we have to execute some statements repeatedly as long as certain condition become true we can use the while loop.
  • In the while loop the given expression will be check at the time of the loop entry, if given expression becomes true then control will transfer into the loop body.
  • The statement inside the loop body will be executed.
  • The counter value will be modified and again given expression will be checked to enter in to the loop.
  • This process will continue until the given expression become false.
  • The while loop contains only expression part ,initialization will be done before the while loop

Syntax:

while(condition)
{
    //loop body
}

Example:

while( i <= 10)
{
    echo "Value of I is $i <br/>" ;
    $i++ ;      // modify counter value
}
  • The following program demonstrates use of while loop to print multiplication table.
/* program to print multiplication table using while loop. */


<?php

    $n = 5, $ans = 0 ;

    

    $i = 1 ;     // set counter value to 1
    
    while( $i <= 10)
    {
       $ans = $n * $i;
      echo "$n * $i = $ans <br/>" ;
       
       $i++ ;      // modify counter value
    }
    
?>

For loop

  • For loop is the most powerful and flexible looping structure.
  • We can perform any complex task using for loop.
  • It has the following from:

Syntax:

 for ( initialization ; condition ; inc/dec)
 {
       // loop body
 }

Example:

PHP for loop

  • For loop contains three different parts: initialization, condition and inc/dec part.
  • When the loop executes for the first time, initialization of the counter variable will be done after that expression given into the condition part is tested, if test expression evaluate to true, control will enter into the loop body.
  • The statements inside the loop will be executed.

Flow of For loop

  • After that control will transfer to the third part where counter value will be either incremented or decremented.
  • With the modified counter value test expression will be check again to enter into the loop.
  • This process will be repeated until condition becomes false.
  • Once the condition becomes false, the loop will be skipped and statement following by for loop will be executed.
  • The initialization will be performed only once when loop encounters first time.
  • For loop also allows specify multiple condition as well as complex expression to be tested.
  • We can also skip any of the part of the loop as per our requirement.
  • The following example provides multiple initialization and expression to be tested.
  • for loop can also be executed either in straight forward or in reverse manner similar to while loop.
  • The following is the same program to print multiplication table using for loop.
/* program to print multiplication table using for loop. */

<?php
    $n = 5, $ans = 0;
   
    for( $i = 1; $i <=10 ; $i++ )
    {
       $ans = $n * $i;
       echo "$n * $i = $ans <br/> " ;
    }
   
?>

Foreach loop

  • Another powerful looping structure offered by the PHP is the foreach loop.
  • It is a variation of for loop.
  • It is more powerful and flexible for looping over arrays and objects.
  • It provides two different versions of foreach loop.
  • It has the following from:

Syntax:

 foreach ( array as value )
 {
       // loop body
 }

 foreach ( array as key => $value )
 {
      // loop body
 }
  • The foreach loop takes an array as argument and the current array element is assigned to the $value and the array pointer moves forward and points to the next array element for processing.
  • The body of the loop is executed once for each array elements.
  • Array elements are processed according to their internal order. Looping continues until the foreach loop reaches the last element.
  • The foreach loop is terminated when there is no element available to process in the array.
  • The foreach operate on a copy of array. During each loop, the value of the variable $value can be manipulated but the original value of the array remains the same.
  • The following example demonstrates the use of foreach loop to print array elements.

Example:

<html>
  <body>
        <?php $array = array (10, 20, 30, 40, 50 ) ;
               foreach( $array as $value )
               {
                    eacho "Value is $value <br/>" ;
               }
        ?> 
  </body>
</html>

Output:

Value is 10
Value is 20
Value is 30
Value is 40
Value is 50
  • Another alternative form of foreach loop is useful for accessing the current key as below:
<html>
   <body>
      <?php  $employee = array ( 'name' => 'Same',
                                 'dept' => 'Sales',
                                 'address' => '72, S.G. road, Ahemdabad'
                               ) ;

        foreach ( $employee as $key => $value )
        {
             echo $key . " is " . $value . "<br/>" ;
        }  
      ?>

   </body>
</html>
July 4, 2017
  • PHP Programming language provides one of the most powerful features known as looping structure
  • When we want to execute some statements repeatedly based on some specific condition, looping structure can be helpful.
  • Looping structure can be divide into the following two categories:
  1. Entry control loop
  2. Exit control loop
July 4, 2017
  • Similar to C-language, PHP also provides goto statement to transfer control unconditionally to other part of the program.
  • Although use of goto statement is not advisable but sometime it is desirable to use go to statement.
  • It has the following form:

goto Statement

  • goto statement requires a label to determine where to transfer the control.
  • A label must end with colon (:)
  • We can use any valid name as a label similar to variable name.
  • When compiler encounters goto statement with a label name then, it transfers the control to the location where label has been defined in the program.
  • When we use goto statement either some statement are execute repeatedly or skipped.
  • When goto statement is placed after the label, control jumps backward direction and some statement are repeated. Such type of go to jump is called backward jump.
  • When goto statement is placed before the label in the program, control transfers to the label and some statements are skipped. Such type of jump is called forward jump.
  • In highly structure programming language such as c, it is not advisable to use go to statement.
  • We should avoid using go to statement as far as possible because it affects performance of the program.
  • The following program demonstrates the use of goto statement.
<?php 
       /* PHP program to calculate sum using goto statement.
   $x = 10;
   $y = 20 ;
    
           
   if( x > 0 && y > = 0)
      goto input ;
   echo "</br>."This statement will be skipped because of goto" ;     
 
   input:
      $sum = $x + $y ;
   
   echo "\n Answer is $sum" ;
   
?>
June 20, 2017
  • When multiple decisions involved in the program at that time we can use else…if ladder but some time it is difficult to read and understand.
  • In addition to this when the numbers of alternative are more than the complexity of the program will also get increase.
  • In such situation PHP-Language provides another multi-way decision making statement called switch statement.
  • The structure of switch statement is more standard and readable then the else if ladder.
  • Switch statement uses different cases to be matched
  • It will match the expression value with different cases available in switch statement from the top to down ward.
  • Each case has its associated block of statement.
  • Matching of the case value will perform from the first case. As soon as it founds the matching case, statements associated with that case block is executed and remaining cases are skipped.
  • If no matching case is found then the default block will be executed if present.
  • The default is optional if present then it will be executed otherwise no action will be taken.
  • The following are the rules for the switch statement:

Rules for Switch Statement:

  • Switch expression must be of type integer or character.
  • Each case value must be unique that means no two case value should be same.
  • Default is optional it can be place anywhere in switch statement but normally it is place at the end.
  • Break statement is also optional if not present then similar cases will be executed.
  • It has the following form:

switch-statement

  • Matching of case expression will be start from top to the down when it found matching case the block associated with that case expression executes then after control transfers to the statement-x.
  • If case expression doesn’t match next case expression will be tested in sequential manner up to the last case and if no matching case is found, default block will be executed if present.
  • the following program demonstrates the use of switch statement:
/* Program to demonstrate the use of switch statement for basic calculation

<?php 
   $x=10;
   $y=20;
   $ans = 0 ;
   $ch = 3 ;
   switch($ch)
   {
      case 1: 
              ans = x + y;
              break;
      case 2:
              ans = x - y;
              break;
      case 3:
              ans = x * y;
              break;
      case 4:
              ans = x / y;
              break;
      default:
              echo "Invalid choice! Please try again!" ;
  }
   echo "\n Answer is $ans" ;
   
?>
June 17, 2017
  • In the previous post we looked upon else…if ladder and in this post we will focus on another form of if statement that is nested if statement.
  • When one if statement contains another if statement then such type of structure is known as nested if.
  • Nested if structure also helps in multi-way decision making where one condition depends on other.
  • It has the following form:
if (condition 1)
{
   if (condition 2)      / * nested if */
   {
       Statement(s);
   }
   else
   {
       Statement(s);
   }
}
else
{
   Statement(s);
}

Example:

<?php
       if (category==”sport”)
       {
           if (gender==’m’)
           {
               mark=mark+bonus_mark;
           }
       }
       else
       {
           mark=mark+5;
       }
?>
June 16, 2017
  • When we have multiple options available or we need to take multiple decisions based on available condition, we can use another form of if statement called else…if ladder.
  • In else…if ladder each else is associated with another if statement.
  • Evaluation of condition starts from top to down.
  • If condition becomes true then the associated block with if statement is executed and rest of conditions are skipped.
  • If the condition becomes false then it will check for next condition in a sequential manner.
  • It repeats until all conditions are cheeked or a true condition is found.
  • If all condition available in else…if ladder evaluated to false then default else block will be executed.
  • It has the following syntax:

Syntax:

if (condition 1)
{
    // block 1
}
elseif (codition 2)
{
    // block 2
}
elseif(codition 3)
{
     // block 3
}
else
{                   
    // default block
}

Else If Ladder

  • else…if ladder provides multi way decision making when any one alternative is to be select among the available option.
  • The following block demonstrates the use of else if ladder.
<?php
   $x=10, $y=20, $ch = 3, $z = 0 ;
    
   if ($ch == 1) {
      $z = $x + $y;
   }
   elseif ($ch == 2){
      $z = $x - $y;
   }
   elseif($ch == 3){
      $z = $x * $y; 
   }
   elseif($ch == 4){                   
      $z = $x/$y ;
   }
   else{
      echo "Invalid choice! Please try again!" ;
   }
      echo "Answer is $z" ;
?>  
June 13, 2017

All Posts

  • Procedure-Oriented Programming Language(POP)
  • Object-Oriented Programming Language (OOP)
  • Features of OOP
  • Introduction to C++
  • Structure of C++ program
  • Structure in C++
  • Class in C++
  • Functions in C++
  • Default Argument
  • Object As Function Argument
  • Static Data Member and Member Function
  • Constructor
  • Destructor
  • Inline Function
  • Function Overloading
  • Constructor Overloading
  • Friend Function
  • Operator Overloading
  • Overloading Unary Operator
  • Overloading Binary Operator
  • Overloading Insertion and Extraction Operators
  • Overloading Operators For String
  • Type Conversion
  • Basic to Class Type Conversion
  • Class Type to Basic Conversion
  • Class to Class Conversion
Copyright © Hexainclude
Developed by Hexainclude
  • TIPS &TRICKS
  • QUIZ
  • CONTACT US
  • COPA