Control Statements and
Functions in C
V.V.Subrahmanyam,
Sr. Lecturer,
SOCIS, IGNOU
Date: 04-02-07
Time: 1-00 to 1-45
Control Statements
Branching:
 The if statement
 If – Else statement
Looping:
 While Statement
 Do-while statement
 For Statement
Switch statement
If statement
If (expr) statement
Example: if (x<0)
printf (“ %d”, x);
If – else statement
If (expr) statement1;
else
statement2;
Example:
if (status == ‘s”)
tax = 0.20 * pay;
else
tax = 0.14 * pay;
Nested if - else
if e1 if e2 s1
else s2
else if e3 s3
else s4
While statement
while (expr)
{ statement(s); }
Example: int digit=9;
while(digit <= 9)
{
printf(“%dn”, digit);
++digit;
}
Do – while statement
Do statement while (expr);
Example:
{
int digit = 10;
do
printf(“%dn”, digit);
while (digit <= 9);
}
For loop
For (expr1; expr2; expr3) statement
Example:
int I;
for (i=0;i<=10; i++) {
printf(“%d”, i);
}
Break statement
It can be used within a for, while , do-
while, or a switch statement.
It is used to terminate a loop or to exit
from a switch statement in a convenient
way when a condition is met, if any
error or irregular condition is detected.
Continue statement
It is used to bypass the remainder of the
current pass through a loop. The loop
does not terminate when a continue
statements is encountered. Rather, the
remaining loop statements are skipped
and the computation proceeds directly
to the next pass through the loop.
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.
goto label;
---
---
Label: statement;
Functions
The use of user-defined functions
allows a large program to be broken
down into a number of smaller, self-
contained components, each of which
has some unique, identifiable purpose.
The C program can be modularized
through the intelligent use of such
functions.
Avoids the need of repeated
programming of the same instructions.
Types of functions
Functions with no parameters and no
return value
Functions with parameters and no
return value
Functions with parameters and with
return value.
Function Declaration
Prototype declaration at the declaration
part.
Data-type fname(type1 arg1, type2 arg2);
Function definition
Actual statements of the function to
solve the specified task.

Control statements and functions in c

  • 1.
    Control Statements and Functionsin C V.V.Subrahmanyam, Sr. Lecturer, SOCIS, IGNOU Date: 04-02-07 Time: 1-00 to 1-45
  • 2.
    Control Statements Branching:  Theif statement  If – Else statement Looping:  While Statement  Do-while statement  For Statement Switch statement
  • 3.
    If statement If (expr)statement Example: if (x<0) printf (“ %d”, x);
  • 4.
    If – elsestatement If (expr) statement1; else statement2; Example: if (status == ‘s”) tax = 0.20 * pay; else tax = 0.14 * pay;
  • 5.
    Nested if -else if e1 if e2 s1 else s2 else if e3 s3 else s4
  • 6.
    While statement while (expr) {statement(s); } Example: int digit=9; while(digit <= 9) { printf(“%dn”, digit); ++digit; }
  • 7.
    Do – whilestatement Do statement while (expr); Example: { int digit = 10; do printf(“%dn”, digit); while (digit <= 9); }
  • 8.
    For loop For (expr1;expr2; expr3) statement Example: int I; for (i=0;i<=10; i++) { printf(“%d”, i); }
  • 9.
    Break statement It canbe used within a for, while , do- while, or a switch statement. It is used to terminate a loop or to exit from a switch statement in a convenient way when a condition is met, if any error or irregular condition is detected.
  • 10.
    Continue statement It isused to bypass the remainder of the current pass through a loop. The loop does not terminate when a continue statements is encountered. Rather, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop.
  • 11.
    Goto statement The gotostatement is used to alter the normal sequence of program execution by transferring control to some other part of the program. goto label; --- --- Label: statement;
  • 12.
    Functions The use ofuser-defined functions allows a large program to be broken down into a number of smaller, self- contained components, each of which has some unique, identifiable purpose. The C program can be modularized through the intelligent use of such functions. Avoids the need of repeated programming of the same instructions.
  • 13.
    Types of functions Functionswith no parameters and no return value Functions with parameters and no return value Functions with parameters and with return value.
  • 14.
    Function Declaration Prototype declarationat the declaration part. Data-type fname(type1 arg1, type2 arg2);
  • 15.
    Function definition Actual statementsof the function to solve the specified task.