Compound Statements :
- A compound statement (also called a “block”) typically appears as the body of another statement, such as the if statement, for statement, while statement, etc
- It consists of several individual statements enclosed within a pair of braces { }. The individual statements may themselves be expression statements, compound statements or control statements. Unlike expression statements, a compound statements does not end with a semicolon. A typical Compound statements is given below.
{ pi=3.14; area=pi*radius*radius; } |
The particular compound statement consists of two assignment-type expression statements.
Accessibility of variables inside and outside of compound statement :
- A variable which is declared above (outside) the compound statements, it is accessible both inside and outside the compound statements.
- A variable which is declared inside the compound statements it is not accessible outside the compound statements.
- It is possible to declare a variable with the same name both inside and outside the compound statements.
C – COMPOUND STATEMENT EXAMPLE PROGRAM :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> #include<conio.h> int main() { float pi, area, radius; clrscr(); printf(“Enter radius of Circle”); scanf(“%f”,&radius); { pi = 3.14; area = pi*radius*radius; } printf(“Area of circle= %f”, area); getch(); return 0; } |
Output :
Enter radius of circle
3 Area of the circle = 28.26 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> #include<conio.h> void main() { int a=100; clrscr(); printf("Outside Compound Statement\n"); printf("a=%d\n\n",a); { int a=200; printf("Inside Compound Statement\n"); printf("a=%d\n\n",a); } printf("Outside Compound Statement\n"); printf("a=%d\n",a); getch(); } |