MY mENU


Wednesday, 15 February 2012

Loops in C programming


Loop's in C are three kinds:
 those are 1. For Loop
                 2. While Loop
                   3.do-while Loop

For Loop:
 For loop in C is the most general looping construct. The loop header contains three parts: an initialization, a continuation condition, and step.
Syntax:
               for (initialization, condition, step)
                        {
                        Statement 1;
                        Statement 2; 
                        …………...
                        Statement n;
                       }

               For statement contain three parts separated by two semicolons. The first part is known as initialization. The variable called loop control variable or index variable is initialized in this part.

              The second part is known as the condition. The condition should be a value one. The condition check can be a compound expression made up of relational expression connected by logical AND, OR.

                 The third part is known as step. It should be an arithmetic expression. The initialization need not be contained to a single variable. If more than one variable is used for initialization they are separated by commas. The step can also applied to more than one variable separated by commas.

               When for statement is encountered at first index variable is get initialized. This process is done only once during the execution of for statement. When condition is evaluated, if it is true the body of the loop will be executed. If more than one statement has to be executed it should end with a pair of braces. The condition of for loop is executed each time at the beginning of the loop. After executing the body of the loop the step is executed, again the condition is executed. If the condition become false it exit from the loop and control transferred to the statement followed by the loop.

The following example executes 10 times by counting 0..9.
for (i = 0; i < 10; i++)
        ;
While LOOP:
  The while loop evaluates the test expression before every loop, so it can execute zero times if the condition is initially false. It has the following syntax.

while (expression)
        {
        Statement 1;
        Statement 2; 
        …………...
        Statement n;
       }

               Here the initialization of a loop control variable is generally done before the loop separately. The testing of the condition is done in while by evaluating the expression within the parenthesis. If the evaluation result is true value, the block of statement within calibrates is executed. If the evaluation result is false value the block of statements within the body of loop is skipped and the loop execution get terminated with the control passing to the statement immediately following the while construct. The increment or decrement of the loop control variable is generally done within the body of the loop.

DO-While Loop:
 In this case the loop condition is tested at the end of the body of the loop. Hence the loop is executed at least once.
Syntax of do while loop is
do
     {
     Statement 1;
     Statement 2; 
     …………...
     Statement n;
    }
while(expression);     

                 Here the block of statement following the do is executed without any condition check. After this expression is evaluated and if it is true the block of statement in the body of the loop is executed again. Thus the block of statement is repeatedly executed till the expression is evaluated to false.

               

Switch Statement in C



switch statement:
 Switch statements simulate the use of multiple if statement.The switch statement is probably the single most syntactically awkward and error-prone feature of the C language. Syntax of c switch statement is
switch(expression)
          {
          case constant1:
                       statements 1;
           break;
           case constant2:
                       statements 2;
           break;
           …………………..
          default:
                       statements n;
           break;
          }


                  When the switch statement is executed, the expression in the switch statement is evaluated and the control is transferred directly to the group of statements whose case label value matches the value of the expression. Each group of statement ends with a break statement. The execution of break statement causes immediate exit from the switch statement. If break statement is not present, then the execution will falls trough all the statement in the selected case.  If none of the case-label value matches the value of expression, then none of the group within the switch statement will be selected and the control is transferred directly to the statement that follows the switch statement c.


                  The expression that forms the argument of switch is evaluated to either char or integer. Similarly the constant expression follows the keyword case should be a value int or char. That is, names of variable can also be used. Switch can test for only equality.



When case constants are evaluated from the top to down, and the first case constant that matches the switch's expression is the execution entry point. In other words, once a case constant is matched, C will execute the associated code block, and all subsequent code blocks .

Example:




goto statement:

The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of the program unconditionally. In its general form, the goto statement is written as


goto label;
               where the label is an identifier that is used to label the target statement to which the control is transferred. Control may be transferred to anywhere within the current function. The target statement must be labeled, and a colon must follow the label. Thus the target statement will appear as
label:statement;


Each labeled statement within the function must have a unique label, i.e., no two statement can have the same label.




If Statement in C


if statements:
If statement is a conditional branching statement. In conditional branching statement a condition is evaluated, if it is evaluate true a group of statement is executed. The simple format of an if statement is as follows:
if (expression) 
        statement;
  If the expression is evaluated and found to be true, the single statement following the "if" is executed. If false, the following statement is skipped. Here a compound statement composed of several statements bounded by braces can replace the single statement.

if else statement:   This feature permits the programmer to write a single comparison, and then execute one of the two statements depending upon whether the test expression is true or false. The general form of the if-else statement is


if(expression)
          statement1
else
          statement2

                    Here also expression in parentheses must evaluate to (a boolean) true or false.
You can set up an if-else statement to test for multiple conditions. if-else construct, the  if, else if, else. This construct is useful where two or more alternatives are available for selection.


The syntax is
if(condition)
          statement 1;
else if (condition)
          statement 2;
          .....................
          .....................
else if(condition)
         statement n-1;
else
          statemens n ;