Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Friday 26 June 2020

Loop concepts

Understanding Loop concepts

In programming, some statements are required to be executed several number of times. Suppose programmer has to execute some statements (like print statement) huge number of the times , but writing the statement huge number of times is a way more time consuming and considerably increases the lines of code.  To make this handy, loop comes into play.

Loop is one of the most powerful and basic building blocks of computer programming. Loop let you write code which needs to be repeated several times in a very efficient way. Just write the code once with some loop conditions and run the code any given number of times.

Loop consists of body and iterating conditions:

Loop body holds the code which needs to execute many times,                                                                       Iterating conditions allows you to write start, stop and iteration  conditions for the code.

Here is the general syntax for the loop:

Loop(start; stop; iteration) {

//  statements

//  statements

}

How loop works

First starts with the start value followed by checking stop condition, if the condition holds true then enters to the loop body and start executing statements inside body and after executing the last statement inside loop body , again the executing pointer moves to the top of the loop statement and perform the iteration condition followed by checking the stop condition and if the condition holds true same steps will be performed until the stop condition becomes false and after getting the false stop condition,  the executing pointer moves to the next statement after loop statement.

Example explaining the working of the loop using for loop:

for( start=0; start<5; start++ ) {

   printf(“loop concepts”);     // prints the string five times

}                                                                                                                                        

Steps:

  •  start=0 and check if start < 5; true
  •  enter inside the loop body and execute print statement
  •  move to the top of for loop statement and increment the start by 1 and check if start < 5; true.
       Repeat the 2nd and 3rd steps until start < 5 becomes false, after that jump out of the loop.

  Output:

loop concepts

loop concepts

loop concepts

loop concepts

loop concepts

Monday 22 June 2020

Why indexing in array starts with 0 ?


Array Index starts with zero

In most common programming languages, the indexing in array starts with zero (0). The reasons behind this are:

In programming languages like C and C++, the array name refers to the location in the memory which means the array name simply points to first element of the array and array[n] refers to the location of n-elements away from the first element. Here, n is used as an offset from the array’s first element. So the first element is at zero (0) distance away from the array. That’s why array index starts with zero.


Performance overhead:

Zero (0)-based index allows array[n] to be implemented as *(array + n). If index were one (1)-based, compiler would need to generate *(array + n - 1), and this "-1" has some performance overhead. As this extra subtraction instruction lowers the performance to low extent but this adds up to a lot when one has to write for the Compiler.  

And the common programming languages developed after C and C++ follows the same design logic for the indexing of the array.

Here is the proof:

#include <stdio.h>

int main()
{
    int array[5] = {1, 2, 3, 4, 5};
   
    printf( "Array location: %p\n", array );

   printf( "Array location of first element: %p\n", array[0] );


}

 

Output:

Array location: 0x7ffde4bf25b0
Array location of first element: 0x7ffde4bf25b0

 

See the output above, both array and array[0] refers to the same memory location.

Saturday 20 June 2020

Python Dictionaries


Understanding Dictionaries

Dictionary is the most handy data type offered by Python and frequently used when the items are needed to be accessed by special or user defined keys.

Dictionaries are quite similar to the lists in the following ways:

Mutable: Like lists, dictionaries are also mutable,

Can contain any type of data types like lists.


But differ from the lists in the following ways:

Dictionaries do not support numeral indexing like lists do. However, you can access the items with their respective keys.

Keys in dictionaries are of immutable type unlike lists, where keys simply mean the positions of the items.

 

Dictionary as map

Dictionary consists of collection of the key-value pairs. It works as a map data structures in other programming languages. Dictionary maps the value to its associated key.

Restriction with keys

Like list where positions of the items are unique, dictionary keys are also unique and of immutable type.

Creating Dictionary

Dictionary can be created by enclosing the key-value pairs inside curly braces ( { } ) like:

dict = { key:value, key:value, ….}

Creating name of the books with respective author

books_details = {

“Let us C” : “Yashavant Kanetkar”,

“TCR JAVA” : “Herbert Schildt”,

“Design and Analysis of Algorithms” : “Thomas Cormen”,

“Operating Systems” : “Peter Galvin” }

Dictionary can also be created using built-in dict( ) function:

books_details = dict( [

(“Let us C”, “Yashavant Kanetkar”),

(“TCR JAVA”, “Herbert Schildt”),

(“Design and Analysis of Algorithms”,“Thomas Cormen”),

(“Operating Systems”, “Peter Galvin”),

] ) 

Accessing Value with key

books_details[ “let us C”]

'Yashavant Kanetkar'

Referring to a key which is not present in the dictionary, Python raises an exception:

books_details[“ abc”]

 



If you try to use key as mutable type like list, Python raises exception

dict = { [“Python”, “JAVA”, “C”] : “ Programming Languages” }


 

Updating the existing value of the key

books_details[“Operating Systems”] = “Avi Silberschatz”


books_details 

{'Let us C': 'Yashavant Kanetkar', 'TCR JAVA': 'Herbert Schildt', ‘Design and Analysis of Algorithms’ : ‘Thomas Cormen’,‘Operating Systems’ : ‘Avi Silberschatz’ }

Updating is possible because dictionary values are mutable type.