Tuesday, 5 March 2013

NESTED-IF- Statement:



NESTED-IF- Statement:
It's also possible to nest one if statement inside another. (For that matter, it's in general possible to nest any kind of statement or control flow construct within another.) For example, here is a little piece of code which decides roughly which quadrant of the compass you're walking into, based on an x value which is positive if you're walking east, and a y value which is positive if you're walking north:
                    if(x > 0)
                               {
                               if(y > 0)
                                              printf("Northeast.\n");
                               else         printf("Southeast.\n");
                               }
               else         {
                               if(y > 0)
                                              printf("Northwest.\n");
                               else         printf("Southwest.\n");
                               }




/* Illustates nested if else and multiple arguments to the scanf function.  */
               #include <stdio.h>
 
                    main()
               {
                               int    invalid_operator = 0;
                               char   operator;
                               float  number1, number2, result;
 
                               printf("Enter two numbers and an operator in the format\n");
                               printf(" number1 operator number2\n");
                               scanf("%f %c %f", &number1, &operator, &number2);
 
                               if(operator == '*')
                                              result = number1 * number2;
                               else if(operator == '/')
                                              result = number1 / number2;
                               else if(operator == '+')
                                              result = number1 + number2;
                               else if(operator == '-')
                                              result = number1 - number2;
                               else
                                              invalid _ operator = 1;
 
                               if( invalid _ operator != 1 )
                               printf("%f %c %f is %f\n", number1, operator, number2, result );
                               else
                                              printf("Invalid operator.\n");
               }
 
 
               Sample Program Output
               Enter two numbers and an operator in the format
               number1 operator number2
               23.2 + 12
               23.2 + 12 is 35.2

IF- Statement: and IF-ELSE Statement:



IF- Statement:

It is the basic form where the if statement evaluate a test condition and direct program execution depending on the result of that evaluation.

Syntax:

                If (Expression)
                {
                Statement 1;
                Statement 2;
                }
Where a statement may consist of a single statement, a compound statement or nothing as an empty statement. The Expression also referred so as test condition must be enclosed in parenthesis, which cause the expression to be evaluated first, if it evaluate to true (a non zero value), then the statement associated with it will be executed otherwise ignored and the control will pass to the next statement.
 
Example:
                if (a>b)
                {
                printf (“a is larger than b”);
                }








IF-ELSE Statement:

An if statement may also optionally contain a second statement, the ``else clause,'' which is to be executed if the condition is not met. Here is an example:
                    if(n > 0)
                               average = sum / n;
               else         {
                               printf("can't compute average\n");
                               average = 0;
                                         }

Type conversions in expressions



Type conversions in expressions

Implicit type conversion

C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic type conversion is know as implicit type conversion .

During evaluation it adheres to very strict rules and type conversion. If the operands are of different types the lower type is automatically converted to the higher type before the operation proceeds. The result is of higher type.

The following rules apply during evaluating expressions
All short and char are automatically converted to int then
1. If one operand is long double, the other will be converted to long double and result
.....will be long double.
.
2. If one operand is double, the other will be converted to double and result will be double.
.
3. If one operand is float, the other will be converted to float and result will be float.
.
4. If one of the operand is unsigned long int, the other will be converted into unsigned
.....long int and result will be unsigned long int.
.
5. If one operand is long int and other is unsigned int then .
.....a. If unsigned int can be converted to long int, then unsigned int operand will be
..........converted as such and the result will be long int.
.....b. Else Both operands will be converted to unsigned long int and the result will be
..........unsigned long int.
.
6. If one of the operand is long int, the other will be converted to long int and the result will be long int. .
7. If one operand is unsigned int the other will be converted to unsigned int and the
.....result will be unsigned int.

Explicit Conversion

Many times there may arise a situation where we want to force a type conversion in a way that is different from automatic conversion.

Consider for example the calculation of number of female and male students in a class

                                         female_students
Ratio =        -------------------
                  male_students


Since if female_students and male_students are declared as integers, the decimal part will be rounded off and its ratio will represent a wrong figure. This problem can be solved by converting locally one of the variables to the floating point as shown below.

Ratio = (float) female_students / male_students

The operator float converts the female_students to floating point for the purpose of evaluation of the expression. Then using the rule of automatic conversion, the division is performed by floating point mode, thus retaining the fractional part of the result. The process of such a local conversion is known as explicit conversion or casting a value. The general form is

(type_name) expression
Specifier Meaning
%c – Print a character
%d – Print a Integer
%i – Print a Integer
%e – Print float value in exponential form.
%f – Print float value
%g – Print using %e or %f whichever is smaller
%o – Print actual value
%s – Print a string
%x – Print a hexadecimal integer (Unsigned) using lower case a – F
%X – Print a hexadecimal integer (Unsigned) using upper case A – F
%a – Print a unsigned integer.
%p – Print a pointer value
%hx – hex short
%lo – octal long
%ld – long unsigned integer.

The size of Operator



The size of Operator

The operator size of gives the size of the data type or variable in terms of bytes occupied in the memory. The operand may be a variable, a constant or a data type qualifier.

Example

m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);


The size of operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables during the execution of the program.

Example program that employs different kinds of operators. The results of their evaluation are also shown in comparision .

.main() //start of program
{
int a, b, c, d; //declaration of variables
a = 15; b = 10; c = ++a-b; //assign values to variables
printf (“a = %d, b = %d, c = %d\n”, a,b,c); //print the values
d=b++ + a;
printf (“a = %d, b = %d, d = %d\n, a,b,d);
printf (“a / b = %d\n, a / b);
printf (“a %% b = %d\n, a % b);
printf (“a *= b = %d\n, a *= b);
printf (“%d\n, (c > d) ? 1 : 0 );
printf (“%d\n, (c < d) ? 1 : 0 );
}


Notice the way the increment operator ++ works when used in an expression. In the statement c = ++a – b; new value a = 16 is used thus giving value 6 to C. That is a is incremented by 1 before using in expression.
However in the statement d = b++ + a; The old value b = 10 is used in the expression. Here b is incremented after it is used in the expression.

We can print the character % by placing it immediately after another % character in the control string. This is illustrated by the statement.

printf(“a %% b = %d\n”, a%b);

This program also illustrates that the expression

c > d ? 1 : 0

Assumes the value 0 when c is less than d and 1 when c is greater than d.