Skip to main content

Posts

The While Loop

The While Loop While programming we often need to do something a fixed number of times. In such cases the While loop is used. Lets study it with the help of an example below; /* Calculation of simple interest for 3 sets of p, n and r */ main( ) { int p, n, count ; float r, si ; count = 1 ; while ( count <= 3 ) { printf ( "\nEnter values of p, n and r " ) ; scanf ( "%d %d %f", &p, &n, &r ) ; si = p * n * r / 100 ; printf ( "Simple interest = Rs. %f", si ) ; count = count + 1 ; } } The program above executes all statements after while three times.The logic of calculating simple interest is written in braces after the while word and is called as the body of the while loop.The brackets after the while contains the condition.All the statements within the loop are executed till the condition is true. Another program which uses while loop,to print the average of the numbers input . The Program

loops2

The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control instruction. There are three methods by way of which we can repeat a part of a program; (1) Using a for statement (2) Using a while statement (3) Using a do-while statement

Loops

Till now we have studied the use of Decision or Sequential Control statements. When executed thesestatements always perform the same series of actions, in the same way, exactly once.But in order to perform programs that can do an action over and over,we need LOOPS. Thus in the next post we are going to discuss about the Loops.

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