UNIT-3 Programming in C
UNIT-3 Programming in C
Decision control and looping statements are used to execute a statement and group of
statement conditionally and/or repeatedly. These are used in the following situations:
To execute statements based on a condition
To execute a set of statements repeatedly until a condition is satisfied
To branch the execution sequence from one statement to another statement.
In ‘C’ language, the control statements are classified into 3 types. They are as follows:
if statement:-
Flowchart:
The ‘if’ statement is a conditional execution
statement that executes the specified set of instructions
based on the condition.
Syntax:
T
if (condition) statement-block ;
condition
F Statement-block
In the above syntax, the condition is a relational/logical/boolen expression that must be
enclosed within parenthesis. If the condition is true then the statements in the statement-block
are executed, otherwise they are not executed. To specify more than one statement in a
statement-block, they must be enclosed within a pair of flower braces ({}).
Examples:
1. if (avg >= 75) printf(“grade is distinction”);
2. if (a < 0) a++;
’if…else’ statement:
It is also conditional execution statement that executes the statements based on the
condition. If the condition is true then it executes one group of instructions otherwise it
executes another group of statements.
Flowchart:
F T
condition
Syntax:
Statement-block2 Statement-block1
if (condition)
statement-block1 ;
else
statement-block2 ;
if (condition1)
{
if (condition2)
statement-block1 ;
else
statement-block2 ;
}
else
{
if (condition3)
statement-block3 ;
else
statement-block4 ;
}
Flowchart:
T F
Cond-1
T F T F
Cond-2 Cond-3
if (a= =b)
else
else
if (a= =b)
else
Switch Statement:
Syntax:
switch (op)
{
case V1 : statement-block1;
[break;]
case V2 : statement-block2;
[break;]
: :
: :
case Vn : statement-block-n;
[break;]
[default : statement-block-d;]
}
In the above syntax, ‘op’ is either a constant, variable (or) expression that results an integer
value. The switch statement uses the value of ‘op’ to execute the statement-blocks.
V1, V2, …, Vn are the values that represent the value of ‘op’. These are the constants
either integer or character type. These values must be unique.
When statement-block contains many statements then they must be enclosed within a
pair of braces ({}).
Working: The switch statement searches for a match of switch variable (op) and case
constants (V1, V2, …, Vn). If a value matches to the ‘op’, the switch statement executes all the
statements in that case-block. If there is ‘break’ statement in that block then it terminates the
execution of switch statement. If ‘break’ is not present then all the statements of remaining
blocks are executed.
If any value doesn’t match to the ‘op’ then the ‘default’ block is executed, if it is
specified in the switch statement.
Example:
main( )
int a;
clrscr( );
scanf(“%d”,&a);
switch (a)
case 1: printf(“Sunday”);
break;
case 2: printf(“Monday”);
break;
case 3: printf(“Tuesday”);
break;
case 4: printf(“Wednesday”);
break;
case 5: printf(“Thursday”);
break;
Goto Statement:
case 6: printf(“Friday”);
break;
case 7: printf(“Saturday”);
break;
Syntax:
goto label ;
Here, the label is an identifier used to name the target location to which the control is
transferred. The target statement must be named with label name followed by a colon as
follows:
label :
statement ;
Working: While program execution, when ‘goto’ statement is reached then it branches the
execution control to the specified label in the program and it continues execution from that
point.
main( ) main( )
scanf(“%d”,&num); scanf(“%d”,&num);
}
Flowchart:
Switch (op)
T Statement-block1
case V1
T Statement-block2
case V2
T Statement-block-n
case Vn
do…while Statement:
This statement is used to execute one or more statements repeatedly as long as the
specified condition is satisfied. It is an exit control loop structure. The do…while structure
executes at least one time even initially the condition is false.
Flowchart:
Syntax:
do Statement-block
{
statement-block;
} while (condition) ; T
condition
F
In the above syntax,
Working: The ‘do…while’ statement executes all the statements in the statement-block first
and then tests the condition. When the condition true (non-zero) then the loop is repeated
otherwise the loop is terminated.
#include<stdio.h>
main( )
int a=1, n;
clrscr( );
printf(“Enter n value”);
scanf(“%d”,&n);
do
a++;
} while (a<=n);