You are on page 1of 2

C Instructions

Now that we have written a few programs let us look at the instructions that we used in these programs. There are basically three types of instructions in C: (a)Type Declaration Instruction (b)Arithmetic Instruction Control Instruction The purpose of each of these instructions is given below:

(A) Type declaration instruction - To declare the type of variables used in a C program. (b) Arithmetic instruction - To perform arithmetic operations between con-stants and variables. Control instruction - To control the sequence of execution of various state-ments in a C program. Since, the elementary C programs would usually contain only the type declaration and the arithmetic instructions; we would discuss only these two instructions at this stage. The other types of instructions would be discussed in detail in the subsequent chapters.

The Conditional Operators


The conditional operators ? and : are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is,
expression 1 ? expression 2 : expression 3 What this expression says is: if expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3. Let us understand this with the help of a few examples:

(a) int x, y ; scanf ( "%d", &x ) ; y=(x>5?3:4);

This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y. The equivalent if statement will be,
if ( x > 5 ) y=3; else y=4; (b) char a ; int y ; scanf ( "%c", &a ) ; y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;

Here 1 would be assigned to y if a >=65 && a <=90 evaluates to true, otherwise 0 would be assigned.

The following points may be noted about the conditional operators: Its not necessary that the conditional operators should be used only in arithmetic statements. This is illustrated in the following examples: Ex.: int i ; scanf ( "%d", &i ) ; ( i == 1 ? printf ( "Amit" ) : printf ( "All and sundry" ) ) ;

Summary
There are three ways for taking decisions in a program. First way is to use the if-else statement, second way is to use the conditional operators and third way is to use the switch statement.

You might also like