Statements

A statement specifies an action to be performed. Except as indicated, statements are executed in sequence. In other words, we can say that Statement is the text the compiler will attempt to turn into executable instructions. In C, Statements are always end with semicolon (;) character. In Source file multiple statements can share a single line or vice-versa. Language C defines six types of Statements and they are:

1. Label Statement

A label consists of a name followed by a colon (:) on a line by itself. Label names shall be unique within a function. Any statement may be preceded by a prefix that declares an identifier as a label name. Labels in themselves do not alter the flow of control, which continues unimpeded across them.
Label names that are not referenced might be classified as redundant code. However, labels have uses other than as the destination of goto statements. They may also be used as break points when stepping through code using a symbolic debugger. Automatically generated code may also contain labels that are not jumped to. C does not provide any mechanism for declaring labels before they appear as a prefix on a statement. Labels may be referenced before they are declared. That is, it is possible to goto a label that has not yet been seen within the current function. Label Statement have three types: a) Identifier: Statement; b) case constant-expression: Statement; c) default: Statement; case and default label shall appear only in switch statement and for referring see in selection statements.
Small program using Identifier Statement as under:
Without any Identifier Label Statement:
#include<stdio.h>
int main(void){
    int i;
    for(i=0; i<10; i++){
        printf("%d - Executed\n", i);
    }
printf("Ending Program");
return 0;
}

Output of Program:

0 – Executed
1 – Executed
2 – Executed
3 – Executed
4 – Executed
5 – Executed
6 – Executed
7 – Executed
8 – Executed
9 – Executed
Ending Program
With Identifier Label Statement:
#include<stdio.h>
int main(void){
    int i;
    for(i=0; i<10; i++){
        printf("%d - Executed\n", i);
        if(i==5){
            goto ending;</div>
        }
    }
ending:
    printf("Ending Program");
    return 0;</div>
}

Output of Program:

0 – Executed
1 – Executed
2 – Executed
3 – Executed
4 – Executed
5 – Executed
Ending Program

2. Compound Statements

A compound statement contains numerous statements that you enclose within curly brace ({}) punctuation. The statements inside a compound statement can be any kind of statement. The Compounded statements are two types (a) declaration-list (b) statement-list. If there are declarations, they must come before any statements. The scope of each identifier declared at the beginning of a compound statement extends from its declaration point to the end of the block. It is visible throughout the block unless a declaration of the same identifier exists in an inner block.
Storage is not allocated and initialization is not permitted if a variable or function is declared in a compound statement with storage class extern. The declaration refers to an external variable or function defined elsewhere.
Variables declared in a block with the auto or register keyword are reallocated and, if necessary, initialized each time the compound statement is entered. These variables are not defined after the compound statement is exited. If a variable declared inside a block has the static attribute, the variable is initialized when program execution begins and keeps its value throughout the program.
When editing someone else’s code, always use the style used in that code.
#include<stdio.h>
int main(void){                   /* Starting of 1st compound statement */
    int i;
    for(i=0; i<10; i++){       /* Starting of 2nd compound statement */
        printf("%d - Executed\n", i);
    }                             /* Ending of 2nd compound statement */
    printf("Ending Program");
    return 0;
}                                 /* Ending of 1st compound statement */

3. Expression Statements

Expression Statement is the most important part of Language C and if one can’t understand expression statement don’t move forward until you understand expression statement.
An expression consists of combination of operators and operands. (An operand, recall, is what an operator operates on.) The simplest expression is a lone operand, and you can build in complexity from there. Thus, we can say that an expression is anything that evaluates to a numeric value. Treating an expression as a statement simplifies the C syntax. However, this specification is needed to handle the resulting value. If a function call is evaluated as an expression statement for its side effects only, the discarding of its value may be made explicit by converting the expression to a void expression.
#include<stdio.h>
int main(void){
    int a, b, c;
    int X, y, z;
    a = b*3+c;                 /*This is the Expression Statement */
    X=y-z;                     /*This is the Expression Statement */
    return 0;
}
Thus, an expression is nothing but a valid combination of constants, variables and operators. Thus, 3, 3 + 2, c and a + b * c – d all are valid expressions.

4. Selection Statements

A selection statement selects among a set of statements depending on the value of a controlling expression. A selection expression is used to make one of two choices and these may be driven by applications requirements. Selection Statement written in the C language have a well-defined syntactic and semantic meaning. Selection statement are used to choose one of several flows of the control the Selection statement. There are two types of Selection Statement are, if and switch.

4.1 IF – Selection Statements

The controlling expression of an if statement shall have a scalar type.
The value of a if selection expression is used to make one of two choices. Values used in this way are generally considered to have a boolean role. Some languages require the controlling expression to have a boolean type and their translators enforce this requirement. Some coding guideline documents contain recommendations that effectively try to duplicate this boolean type requirement found in other languages.
In both forms, the first sub-statement is executed if the expression compares unequal to 0. In the else form, the second sub-statement is executed if the expression compares equal to 0. If the first sub-statement is reached via a label, the second sub-statement is not executed.
As a general rule in C, anywhere you can use a simple statement, you can use any compound statement, which is just a number of simple or compound ones enclosed in {}. The ability to replace single statements by complex ones at will is one feature that makes C much more pleasant to use than Programming Languages.
#include <stdio.h>
int main(void){
    int i, j;
    for ( i = 0; i &lt; 10; i++ ){
        printf("Outer loop executing. i = %d\n", i );
        for (j=0; j&lt;3; j++ ){
            printf("Inner loop executing. j = %d\n", j );
            if(i==5){
                goto stop;             /* We have used condition statement*/
            }
        }
    }
    printf( "Loop Exited. i = %d\n", i ); /* This message does not print: */
stop:
    printf( "Jumped to stop. i = %d\n", i ); /*Loop will finish when i =5.*/
    return 0;
}

Output of Program:

Outer loop executing. i = 0
Inner loop executing. j = 0
Inner loop executing. j = 1
Inner loop executing. j = 2
Outer loop executing. i = 1
Inner loop executing. j = 0
Inner loop executing. j = 1
Inner loop executing. j = 2
Outer loop executing. i = 2
Inner loop executing. j = 0
Inner loop executing. j = 1
Inner loop executing. j = 2
Outer loop executing. i = 3
Inner loop executing. j = 0
Inner loop executing. j = 1
Inner loop executing. j = 2
Outer loop executing. i = 4
Inner loop executing. j = 0
Inner loop executing. j = 1
Inner loop executing. j = 2
Outer loop executing. i = 5
Inner loop executing. j = 0
Jumped to stop. i = 5

4.2 Switch – Selection Statements

The controlling expression of a switch statement shall have integer type.
A switch statement uses the exact value of its controlling expression and it is not possible to guarantee the exact value of an expression having a floating type. For this reason implementations are not required to support controlling expressions having a floating type.
A controlling expression, in a switch statement, having a boolean role might be thought to be unusual, an if statement being considered more appropriate. However, the designer may be expecting the type of the controlling expression to evolve to a non-boolean role, or the switch statement may have once contained more case labels.
The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion.
Some sequences of case label values might be considered to contain suspicious entries or omissions. For instance, a single value that is significantly larger or smaller than the other values (an island), or a value missing from the middle of a contiguous sequence of values (a hole). While some static analysis tools check for such suspicious values, it is not clear to your author what, if any, guideline recommendation would be worthwhile.
There may be at most one default label in a switch statement.
Some coding guideline documents recommend that all switch statements contain a default label. There does not appear to be an obvious benefit for such a guideline recommendation. To adhere to the guideline developers simply need to supply a default label and an associated null statement. There are a number of situations where adhering to such a guideline recommendation leads to the creation of redundant code.
A switch statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a default label and the values of any case labels on or in the switch body. A case or default label is accessible only within the closest enclosing switch statement.
#include<stdio.h>
int main(void){
    int no;
    printf("\nEnter a number with Digits form 0 to 9(1 digit no): ");
    scanf("%d",&no);
    switch(no){
        case 0:
            printf("zero");
            break;
        case 1:
            printf("one");
            break;
        case 2:
            printf("two");
            break;
        case 3:
            printf("three");
            break;
        case 4:
            printf("four");
            break;
        case 5:
            printf("five");
            break;
        case 6:
            printf("six");
            break;
        case 7:
            printf("seven");
            break;
        case 8:
            printf("eight");
            break;
        case 9:
            printf("nine");
            break;
        default:
           printf("Kindly Enter the Digits Only\t");
           break;
    }
    return 0;
}

5. Iteration Statements

6. Jump Statements

to be continued…..

				
Advertisements
Tagged ,
%d bloggers like this: