Thursday, October 29, 2009

IF-ELSE Looping

Looping is one of the most essential and important programming feature. Let us talk about IF looping. IF is exactly same to the If we use in literature of English. it play an important role in conditional branching. IF basically check the condition whether it is true or false, if it is true it executes the block of itself if not then jump out of loop or skips the if loop. Let us take a real life example of it

If( weather==cloudy)
It will rain.

-------------------------------------------------------------

If (cloudy weather== 1)
{
Printf(“It will rain”);
}

You can identify the similarity between the IF in English and in C language. Now, we will deal with ‘IF’ in more technical way or say let us use it programmatically. ‘IF’ is a strict comparison feature in C. ‘IF’ always look for exact comparison. Let us take the example

Main()
{
Float me=1.1;
Double you=1.1;

If(me==you)
{
Printf(“Waw! We are same”);
}
Else
Printf(“Nope! We are different”);
}

Here u must be wondering with strange word ‘ELSE’, don’t you? We will be coming to that just after the explanation of this program.

Here the values assigned to variable me and you is same, so we easily conclude that comparison will return a true or positive non-zero value and print statement in that block will get executed. One more term is there we should look for before concluding the answer and that is data type of both the variables. Variable me is of float type and you is of double type. And after referring to article 1 on data types we come to know that float and double are two different types of storing data. They differ in the number of precisions after decimal and so they require different amount of memory blocks as float requires 4 bytes while double requires 8 bytes. Hence the values assigned to float me and double you though they are same their representation in memory is different in fact and therefore the program result the answer saying “ No! We are different”.
Now, let us come to the ‘stranger’ i.e. ELSE. ‘ELSE’ is a follower of ‘IF’ but not always. Sometimes we only need ‘IF’ statement, and sometimes both ‘IF’ and ‘ELSE’. ‘ELSE’ again works exactly according to its literal meaning. Again lets take that real time example. If the condition returns zero, sometimes you want the program do something else. In that case, after the if statement, you use an else statement.


If( weather==cloudy)

It will rain.
Else
It will not rain
--------------------------------------------------------------------

If (cloudy weather== 1)
{
Printf(“It will rain”);
}
Else
{
Printf(“It will not rain”);
}

‘ELSE’ always gets executed when the condition in IF statement is false. So, it means that a ELSE block should contain something which we want to get executed if the condition in IF statements results false value or zero value.
One more provision for conditional branching is there in C language that we call ‘ELSEIF’. If we have multiple conditions to check and wish to execute separate block for each condition then it is always a good practice to use elseif than multiple IF statements. Let’s have a look at following example.

#include

int main() {
int a;

printf("Input an integer and push return:\n");
scanf("%d", &a);

if(a%2==0 && a<0) { printf("%d is even and less than zero.\n", a); } else if (a%2!=0 && a<0) { printf("%d is odd and less than zero.\n", a); } else if (a%2==0 && a>0)
{
printf("%d is even and greater than zero.\n", a);
}
else if (a%2!=0 && a>0)
{
printf("%d is odd and greater than zero.\n", a);
}
else
{
printf("You entered zero.\n");
}

return 0;
}

the program output will depend on the value you enter.
The main focus is on the if block, with the solitary if and else statements, as well as the else ifs.
Now, the program evaluates the if condition. If it returns a non zero value, the if branch is executed. Once all the statements in that branch have been executed, THE PROGRAM IGNORES THE REST OF THE IF BLOCK. In other words, the remaining conditions are not evaluated.
If, on the other hand, (a%2==0 && a<0) returns zero, the first else if condition is evaluated and so on.
The main thing to remember is that once the program chooses a branch in the if block, the remaining branches are totally ignored.

One final note: I tend not to end an if block on an else if.

If one wanted to say, "else do nothing", rather than missing the else statement totally, its always better to insert a else block containing nothing.


if(x==0)
{
printf("x is zero\n");
}
else if(x==1)
{
printf("x is 1\n");
}
else
{
/* Do nothing */
}

Wednesday, October 28, 2009

C Language :Data Type

A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called program. A program usually contains different types of data value and hence one must specify the type. A variable is nothing but a name of memory location where we store or keep the data, but prior to this compiler expects declaration of specific form of value to be stored in memory location and for that a concept of data types is used. C language supports pre-declaration strategy i.e. one must specify the (form) type of variable before its' use. The declaration of variable with certain data type actually specifies the value contained in variable. C has different data types for different types of data and can be broadly classified as :
  • Primary data types.
  • Secondary data types
    Primary data types consist following data types.
Data Types in C

Integer types:
Integers are whole numbers with a range of values, range of values are machine dependent. Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to +32768 (that is, -215 to +215-1). A signed integer use one bit for storing sign and rest 15 bits for number. To control the range of numbers and storage space, C has three classes of integer storage namely short int, int and long int. All three data types have signed and unsigned forms. A short int requires half the amount of storage than normal integer. Unlike signed integer, unsigned integers are always positive and use all the bits for the magnitude of the number. Therefore the range of an unsigned integer will be from 0 to 65535. The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space.
Syntax: int ; like int num1; short int num2; long int num3;
Example: 5, 6, 100, 2500.


Integer Data Type Memory Allocation


Floating Point Types:
The float data type is used to store fractional numbers (real numbers) with 6 digits of precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision and takes double space (8 bytes) than float. To extend the precision further we can use long double which occupies 10 bytes of memory space.
Syntax: float ; like float num1; double num2; long double num3;
Example: 9.125, 3.1254.

Floating Point Data Type Memory Allocation

Character Type:
Character type variable can hold a single character. As there are singed and unsigned int (either short or long), in the same way there are signed and unsigned chars; both occupy 1 byte each, but having different ranges. Unsigned characters have values between 0 and 255, signed characters have values from –128 to 127.
Syntax: char ; like char ch = ‘a’;
Example: a, b, g, S, j.


Void Type:
The void type has no values therefore we cannot declare it as variable as we did in case of integer and float. The void data type is usually used with function to specify its type. Like in our first C program we declared “main()” as void type because it does not return any value.


Regards:
Ajit R Pandey,

Tuesday, October 27, 2009

C Theory Learning: Ajit Pandey

Dear Students,
Hi, finally we have started with some extra curricular activities in the form of C : A Practical Approach . It is in fact a good way to exercise your C programming skill in short span of time. And as you all must know that we are trying strongly to start up with value addition classes for all non computer graduates of MCA-I and hopefully we will soon come up with it as well. But when I was thinking of the benefits of the exercise we have already started a thought suddenly came to my mind that why not to create a BLOG wherein 1 article about C language will posted and in comment to that you can online ask questions to me and I will be able to reply it within minutes. And you need not to every time roam around for faculty members for getting your problem solved. And this is something we can continue for upcoming all semesters moreover for upcoming batches.The basic intension behind all this is to add something more to your technical knowledgebase from all perspective. But as I always keep telling to you all, every activity is response dependant, better you respond longer it survive.From tomorrow we can start with this wherein I will be posting one article on any C language topic and in comment just below that you can post your questions. And the answer or solution to that will be sent to you very next day; in this way we will be able to learn C (Theory) online.So guys and gals, healthy response will boost me to do better and more useful stuff for you.Do write comment on this to get your views and suggestions.

Followers