2. TYPE OF CONTROL STATEMENT
C support 3 kinds of control structure:
sequential
selective
iterative
3. statement that is used to alter the
continuous sequential execution of
statements
sequential
no branching, looping or have any
decision making in the statement
A series of coding.
Source code is execute one after and after.
CONTROL STATEMENT ??
Use to control the flow of the source code/programming
Key to write a structured program where CS can :
Reduce complexity
Improve clarity.
Facilitate debugging and modifying
Technique to organize a program
4. Operation is simple to trace
ADVATAGES OF CONTROL STATEMENT
Structures are easier to define in flowcharts
Structure programming increases programmer's
productivity
5. CONTROL STATEMENT
Simple if statement
If else statement
If-else if ladder statement
Switch case statement
go to statement for statement
While statement
do-while
Unconditional Conditional Looping
6. If statement
If (test condition)
{
Statement block;
}
Next statement;
GENERAL FORM
#include <stdio.h>
#include <conio.h>
int main (void)
{
int num1;
int num2;
printf (“Please Enter 2 numbers”);
scanf(“%d%d”, &num1,&num2);
If (num1== num2)
{
printf(“There are equal”);
}
getch();
}
EXAMPLE
To execute one statement or
group of statements for
particular condition
7. If –else-statement
If (test condition)
{
Statement block-1;
}
else
{
Statement block-2
}
Next statement;
GENERAL FORM
#include <stdio.h>
void main()
{
int mark;
printf (“Please Enter mark”);
scanf( “%d”, &mark);
If (mark=>39)
{
printf(“Pass”);
}
else
{
printf(“Fail”);
}
getch();
}
EXAMPLE
To execute one group of
statements if the test condition is
true or other group if the test
condition is false
8. If else.. ladder statement
If (test condition-1)
{
Statement block-1;
}
Else if (test condition-2)
{
Statement block-2;
}
…
…
Else if (test condition-n)
{
Statement block-n;
}
Else
Default statement;
Next statement;
GENERAL FORM
To take multiple decision. This
statement is form by joining
if..else statement.
If one condition is false, it
checks for the next condition
and so on. When all the
conditions are false, then else
block is executed
9. #include<stdio.h>
#include<string.h>
void main()
{
int n;
printf(" Enter 1 to 3 to select your pet");
scanf("%d",&n);
if(n==1)
{
printf("You pet is a tiger");
}
else if(n==2)
{
printf("You pet is a sugar glider");
}
else if(n==3)
{
printf("You pet is a cow");
}
else
{
printf("No pet selected"); }
getch();
}
EXAMPLE
If else.. ladder statement
10. switch statement
switch (expression)
{
case value1:
Statement block-1:
break;
case value2:
Statement block-2:
break;
…
.. .
case value-n:
statement block-n:
break;
default:
default statement;
break;
}
Next statement
GENERAL FORM
Alternative to if else if ..ladder
statement
to execute a block of code
based on selection from multiple
choices.
C switch statement is a multiway
decisions that tests whether a
variable or expression matches
one of a number of constant
integer values, and branches
accordingly.
'break' is a statement. It is most
commonly used with the switch
statement. 'break' statement
stops the execution within the
switch statement and exits from
the switch statement.
11. #include <stdio.h>
main()
{
int Grade;
choose:
printf(“Please insert your grade A, B, C, D, E,
or F”);
scanf("%d",&Grade);
switch( Grade )
{
case 'A' :
printf( "Excellentn" );
break;
case 'B' :
printf( "Goodn" );
break;
case 'C' :
printf( "OKn" );
break;
EXAMPLE
case 'D' :
printf( "Mmmmm....n" );
break;
case 'F' :
printf( "You must do better than
thisn" );
break;
default :
printf( "What is your grade anyway?n"
); go to choose; break;
}
getch();
}
12. goto statement
goto label:
GENERAL FORM
Used to transfer the program
control unconditionally from one
point to another
its functionality is limited and it is only
recommended as a last resort if structured
solutions are much more complicated