Skip to main content

Posts

Forms of IF statement

Forms of IF Statement: The if statement can take any of the given forms: (1) if ( condition ) do this ; (2) if ( condition ) { do this ; and this ; } (3) if ( condition ) do this ; else do this ; (4) if ( condition ) { do this ; and this ; } else { do this ; and this ; } (5) if ( condition ) do this ; else { if ( condition ) do this ; else { do this ; and this ; } } (6) if ( condition ) { if ( condition ) do this ; else { do this ; and this ; } } else do this ;

The if-else Statement

The if-else Statement As described in earlier post,the if statement will execute a single statement, or a group of statements, when the expression following if evaluates to true.But it does nothing when the expression evaluates to false. In case, we want to execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false,we need to use the If-else statement. The syntax of If-else statement is as follows; if(condition) { statement; } else { statement2; } If the condition 1 is evaluated true,then statement1 will execute else statement2 will be executed. A simple example is; if(a =2500 ) { hra = 1200; da = basic * 0.5 ;pt=150; } else { hra = basic*0.1 ; da = basic * 25.01 / 100 ; } gs = basic + hra + da ; net=gs-pt; printf ( "Net salary = Rs. %f", net ) ; }

The IF statement

The IF statement The C language uses the keyword if to implement the decision control instruction. The general form of IF statement is as follows; if ( condition ) execute this statement ; The condition following the keyword if is always enclosed within a pair of parentheses. If the condition is true then the given statement is executed.If the condition is not true then the statement is not executed; instead the program skips past it. To express conditions in C we use conditional operators. Expression Meaning x == y x is equal to y x != y x is not equal to y x x is less than y x > y x is greater than y x x is less than or equal to y x >= y x is greater than or equal to y Lets consider a sample program; /* The IF statement */ main( ) { int num ; printf ( "Enter a number less than 50 " ) ; scanf ( "%d", &num ) ; if ( num printf ( "You are fail" ) ; } On execution,if you enter a number less than equal to 50 you will get the message on the screen but...

Decision Control Structure

The Decision Control Structure Till now we have used sequence control structure in the programs,in which the various steps are executed sequentially i.e.in the same order in which they appear in the program. In C programing the instructions are executed sequentially,by default. At times,we need a set of instructions to be executed in one situation and an another set of instructions to be executed in another situation. In such cases we have to use decision control instructions.This can be acheived in C using; (a)The if statement (b)The if-else statement (c)The conditional operators

Hierarchy of Operations

The preference in which arithematic operations are performed in an arithematic expression is called as Hierarchy of operations. Its very important in C Programming to predefine the Hierarchy of operations to the C compiler in order to solve the given arithematic expression correctly. e.g.: If a programmer wishes to perform ,z=a+b/c;it may be interpretted as z=(a+b)/c or z=a+(b/c). and if a=3,b=6,c=2 then using the same expression we will obtain two different answwers as z=4.5 or z=6. To avoid this condition we must be aware of hierarchy of operations used in C programming. In arithematic expressions scanning is always done from left to right. The priority of operations is as shown below, Priority Operators First Paranthesis os brackets() Second Multiplication & Divison Third Addition & Substraction Fourth Assignment i.e.= If we consider the logical operators used in C language,the operator precedence will be like; Operators Type ! Logical ...

Increment-Decrement Operators

PRE/POST INCREMENT/DECREMENT OPERATORS(Unary operators) In programming we often require to increment value of certain variable in step of one or may require to decrement a value. This is usually written as i=i+1 for incrementing the value of variable i or i=i-1 to decrement the value of i. To accomplish this C language provides unary operators called as increment/decrement operators.Using these operators the above variable can be written as i++ or ++i. If ++ is after the variable then it is called as post increment and if it is before the variable then it is called as predecrement. In case of post increment(i++) the old value of the variable is used first and then the increment in the value of variable is done. In preincrement the value of variable is incremented first and then this value is used in the expression. same is in the case of decrement,i.e.pre decremant and post decrement, denoted as --i and i--.

Using symbolic Constants(#define)

Using Symbolic constants(#define) While developing a program in C language,we often use certain unique constants in a program.These const. may appear repeatedly in a C program,at number of places. Foe example,if we want to calculate the area of n number of circles,we require a constant 3.14 which is the value of'pi'. Now we may like to change the value of pi from 3.14 to 3.14159.To achieve this we will have to search through-out the program and explicitly change the value of pi whereever it has been used.If any value is left unchanged the program will produce wrong results.When we use variable ,value may change accidently,which results to wrong output. To avoid these type of mistakes w use symbolic constants in our C program. the syntax of a symbolic constant is; #define symbolic name value e.g. #define pi 3.14159 #define total 100