Module 3. Control statements

 

Lesson 9

SIMPLE AND COMPOUND STATEMENTS

9.1  Statement

A typical ‘C’ programme comprises of various statements. A statement is a part of the programme that can be executed by the compiler. Every statement in the programme alone or in combination specifies an action to be performed by the programme. ‘C’ provides a variety of statements to attain any function with maximum efficiency. Statements fall into three general types:

·         An expression statement

·         Compound statement 

·         Control statement

9.2  Expression Statement

An expression statement consists of an expression followed by a semicolon. The execution of an expression statement causes the expression to be evaluated.

Examples:

a = 34;

c = a + b;

++i;

The first two statements are assignment statements. Each causes the value of expression on the RHS of the ‘equal to’ sign to be assigned to the variable on the LHS. The third statement is increment statement that increments the value of variable ‘i’ by 1.

9.3  Compound Statement

A compound statement consists of several individual statements enclosed within a pair of braces, ({and}). Thus, the compound statement provides capability for embedding statements within other statement(s). A compound statement being a group of statements, which are bound together to form a programming logic, are also called a block statement. As you have already seen, a block statement begins with a left curly bracket, {, and ends with a right curly bracket, }. This is illustrated below with the help of an example code fragment:

                   {

          a = 3; b = 4;

          perimeter  = 2*(a+b);

          area = a*b;

        }

Example 1: Programme to convert length given in feet to equivalent centimetres. Given that one foot contains 30.48 cm. (See Example-1 of Lesson-2 for pseudo code and flowchart for this problem).

#include <stdio.h>

int main()

{

   float len_ft, len_cm;

   printf(“Enter length in feet: “);

   scanf(“%f”, &len_ft);

   len_cm=len_ft*30.48;

   printf(“Length in ft and in cm is: %5.2f, %5.2f,  
           respectively”, len_ft, len_cm);

   return 0;

}

Output:

Enter length in feet: 2.4

Length in ft and in cm is:  2.40 and 73.15, respectively.

Example 2: Programme to calculate the real roots of a quadratic equation. (See Example-2 of Lesson-2 for pseudo code and flowchart for this problem).

#include <stdio.h>

#include <math.h>

int main()

{

   float a,b,c,d,x_1, x_2;

   printf(“Enter a, b and c: “);

   scanf(“%f %f %f”, &a, &b, &c);

   d=sqrt(b*b-4×a×c);

   x_1=(-b+d)/(2*a); 

   x_2=(-b-d)/(2*a);

   printf(“a, b, c, x_1, x_2: %5.2f, %5.2f, %5.2f,  
          %5.2f,%5.2f",a, b, c, x_1, x_2);

   return 0;

}

Output:

Enter a, b and c: 2 3 1

a, b, c, x_1, x_2:  2.00,  3.00,  1.00,-0.50, -1.00

Example 3: Programme to calculate area and perimeter of a circle when its radius is known.

#include <stdio.h>

int main()

{

    float area, pi, perimeter, radius;

    printf(“Enter radius of circle: ”);

    scanf(“%f”, &radius);

    {

       pi=3.14;

       area = pi*radius*radius;

       perimeter = 2.0*pi*radius;

    }

    printf(“\nArea of circle is: %f“, area);

    printf(“\nPerimeter of circle is: %f“,
           perimeter);

    getch();

    return 0;

}

Output:

Enter radius of circle: 3

Area of circle is: 28.260000

Perimeter of circle is: 18.840000

9.4  Control Statements

Control statements are used to control the flow of execution of the instructions written in a programme. Normally, the instructions are executed sequentially, i.e., one after the other or in the same order as they appear in the programme. This type of control structure is known as sequence control structure and there is neither any decision making process involved nor do they require repeated execution of group of statements. However in normal routine, it is often required to change the order of execution of statements. Hence, besides sequence control statements, several other control structures are required to accomplish a real-life problem-solving task. These structures are:

·         Selection control structure (decision or branching or conditional statements) – These structures decide the flow of statements based upon the results of evaluation of pre-define conditions, e.g., if…then…else and switch…case statements (discussed in Lesson 10) come under this category.

·         Repetition control structure (looping or iteration statements) – These structures are used to run a particular block of statements repeatedly (i.e., in a loop), e.g., for, while…do and repeat…until statements (discussed in Lesson 11) come under this category.

·         Jumping statements (goto statement)

o    Jump Statements – These are used to make the flow of your statements from one point to another, e.g., break, continue, goto and return statements (discussed in Lesson 11) belong to this category.

o    Label Statements – These are used as targets/way-points for jump and selection statements. case (discussed with switch in Lesson 11) and label (discussed with goto) come under this category

All types of control statement are discussed in detail in the following lessons 10 and 11 of this Module III.

9.5  Comments Statement

Besides various executable (that are compiled by compiler) as well as non-executable statements (i.e., processor directive statements such as the define statement) as discussed earlier, there is another non-executable statement supported by ‘C’, i.e., comments statement that is ignored by the compiler while compiling the source code to generate object code leading to the executable code. Comments provide the programmers with information about the code. Good commenting generally increases the productivity of your team as you do not have to go back every time to figure out what a particular function/segment is doing; you can read the comments instead. Remember that while your source code will be compiled and optimised by a machine; it would always be read and modified by a human being! Hence, comments statement is important and is discussed below.

Any text or statement in a ‘C’ source code, which is embedded within the symbols, ‘/* and ‘*/is treated as a comment statement, which is non-executable and just ignored by the compiler while compiling the code. The comment statement can occur within expressions, and can span multiple lines. Proper care must be taken by the programmer to mark the beginning and end of the comment using appropriate terminators meant for the purpose. Consider the following code segment and note the associated side effects arising due to improper use of comment statement terminators:

1. /* This line will be ignored.
2. /*
3. These lines will also be ignored. Note that the comment opening token above did not start a new comment, and that the comment closing token below will close the comment begun on line-1.
4. */
5. Hence, this line and the line below it will not be ignored. Both may produce compile-time errors!
6. */

C++ style line-comments start with // and extend to the end of the line. This style of comment originated in Basic Combined Programming Language (BCPL) and became valid ‘C’ syntax in C99; however, it is neither available in the original ‘C’ nor in the ANSI ‘C’. This is illustrated in following code snippet:

// this line will be ignored by the compiler

/* these lines

will be ignored

by the compiler */

x = *p/*q;  /* note: this comment starts after the ‘p’ */