Unit III Converted
Unit III Converted
Unit – III
1. Control statements
Program is set of statements which are normally executed sequentially in order in
which they are appears. The control statements can be classified into
1. Branching statement
2. Looping statement
1. Branching statement
These statement can be used to alter the program execution sequence based on
certain condition or unconditionally. Branching statement can be classified into
(a) conditional branching
(b) unconditional branching
1. simple if statement
2. if …. else
3. if … else if ladder
4. nested if
Simple if statement
The general form of simple if statement is
if(test condition)
{
Statement-block;
}
Statement-x;
The ‘statement-block’ may be a single statement or a group of statements. If the
test-condition is true, the statement block will be executed; otherwise the statement-block
will be skipped.
Example:
if(category == sports)
{
marks = marks + 5;
}
printf(“%f”,marks);
The above program tests the category of student. If the student belongs to sports
category, then additional 5 marks are added.
if …else statement
The if…else statement is an extension of simple if statement. The general form is
if(test expression)
Page No 1
III B.Sc[Maths] – Programming in C – Unit III
{
True-block statements;
}
else
{
False-block statements;
}
Statement-x;
If the test expression is true, then the true-block statement(s), immediately
following the if statement are executed; otherwise, the false-block statement(s) are
executed. In either case, either true-block or false-block will be executed, not both.
Example:
if(category == sports)
{
marks = marks + 5;
}
else
{
Marks = marks + 2;
}
printf(“%f”, marks);
In the above program test the category of student. If the student belongs to sports
category, then bonus marks 5 will be added; otherwise bonus marks 2 will be added.
Page No 2
III B.Sc[Maths] – Programming in C – Unit III
else if(marks>=40)
printf(“Third class”);
else
printf(“Fail”);
The above program test the marks of student. If marks is greater than or equal to
60, then ‘first class’ will be printed. If not, test whether the marks is greater than or equal
to 50, if yes, then ‘second class’ will be printed. If not, test whether the marks is greater
than or equal to 40, if yes, then ‘third class’ will be printed. If not, then the final else
statement will be executed and ‘fail’ will be printed.
Nested if
if statement placed within another if statement is called nested if statement. The
general form of nested if is
if(test condition1)
{
If(test condition2)
{
Statement -1;
}
else
{
Statement -2;
}
}
else
{
Statement – 3;
}
Statement –x;
Example:
if(gender=’f’)
{
if(balance>5000)
bonus= balance * 0.05;
else
bonus=balance * 0.02;
}
else
{
bonus=balance * 0.02;
}
balance = bonus + balance;
Page No 3
III B.Sc[Maths] – Programming in C – Unit III
In the above program test the gender of person. If gender if female then balance of
person is checked. If balance is greater than 5000 then bonus will be 5 percent; otherwise
2 percent bonus will be added.
goto label;
----------
----------
-----------
Label:
Statement;
The label: can be anywhere in the program either before or after the goto label;
If label is placed before goto statement then some statement will be executed repeatedly.
If label is placed after goto statements then some statements are skipped.
Example:
goto begin;
when the above statement is executed, the flow of control jump to statement
immediately following the begin;. This happens unconditionally.
main()
{
Double x,y;
read:
scanf(“%f”,&x);
if(x<0)
goto read;
y=sqrt(x);
printf(“%f”,y);
goto read;
}
Page No 4
III B.Sc[Maths] – Programming in C – Unit III
-------------------------
case value-n: statement n;
break;
default: statement:
}
The switch statement test the value of expression or variable against the case
values and when a match is found, a block of statement associated with that case is
executed. When there is no match found the default statement is executed.
Example:
scanf(“%c”,&c);
Switch(c)
{
case ‘a’or ‘A’:
case ‘e’ or ‘E’:
case ‘i’ or ‘I’:
case ‘o’ or ‘O’:
case ‘u’ or ‘U’:printf(“the given character is vowel”); break;
default: printf(“the given character is not a vowel”);
}
If we give a or e or i or o or u for the variable c, then the result is ‘the given
character is vowel’ ; otherwise the result is ‘the given character is not a vowel’ .
Example:
sum=0;
n=1;
while(n<=10)
{
Page No 5
III B.Sc[Maths] – Programming in C – Unit III
sum = sum + n * n;
n = n+1;
}
printf(“sum = %d”, sum);
in the above example the body of loop is executed 10 times for n=1,2,….,10 each
time adding the square of value of n, which is incremented inside the loop.
Do ..while statement:
The general form is
do
{
Body of loop;
} while(condition);
It is the exit control loop. In do while statement the body of loop evaluated first.
At the end of the loop, the condition is evaluated. If the condition is true, the program
continue to evaluate body of loop once again. this process continues as long as condition
is true.
Example:
int n, sum=0;
do
{
Scanf(“%d”,&n);
sum=sum+n;
}while(n>0);
The above code read a number from keyboard until a zero or negative number is
keyed in.
Example:
for(x=0;x<10;x++)
{
printf(“%d”, x);
Page No 6
III B.Sc[Maths] – Programming in C – Unit III
}
In the above program the body of loop executed 10 times and prints digits
from 0 to 9.
Break statement:
When the break statement is encountered inside the loop, the loop is immediately
exited and program continues with the statement immediately following the loop. The
general form of break statement is
break;
Example:
for(i=0;i<1000;i++)
{
Scanf(“%d”,&n);
If(n<0)
break;
sum=sum+n;
}
In the above example the for loop written to read 1000 values. If we want to add
set of values less than 1000 values, then we enter negative value after the last value to
make the end of input.
Continue statement:
When continue statement encountered inside the loop, skip the statement after
‘continue’ statement and the loop continued with next iteration. The general form is
continue;
Example:
for(i=0;i<100;i++)
{
scanf(“%d”,&n);
if(n<0)
continue;
sum=sum+n;
}
In the above example for loop used to read 100 values. If the read value is
negative then it will not be added; otherwise added with the sum.
Array:
Array is a group of related data item of same type that shares common name. The
individual elements of array are referred by index number. Index number starts from 0 to
array size minus 1. There are three types of array.
1. one dimensional array
2. two dimensional array
3. multi dimensional array
One dimensional array:
List of items can be given one variable name using only one subscript value is
called one dimensional array.
Page No 7
III B.Sc[Maths] – Programming in C – Unit III
Syntax:
type array_name[size];
the type specifies the type of element that will be contained in the array such as
int, float or char and size indicates the maximum number of element that can be stored
inside the array. For example
int x[5];
Example program
int a[5],sum=0;
for(int i=0;i<5;i++)
{
Scanf(“%d”,&a[i]);
}
for(i=0;i<5;i++)
{
Sum=sum+a[i];
}
printf(“%d’”,sum);
in the above program a is the one dimensional array variable that can hold 5
integer values and for loop used to receive 5 integer values and to calculate sum.
Page No 8
III B.Sc[Maths] – Programming in C – Unit III
The type specifies the type of element that will be contained in the array such as
int, float or char and row-size specifies maximum number of row and column-size
specifies maximum number of column within row.
Example:
int number[3][3];
The array variable number stored in memory as shown below
Example Program
int a[5][5],sum=0,i,j;
for(int i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
Scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
Sum=sum+a[i][j];
}
}
printf(“%d’”,sum);
in the above program a is the two dimensional array variable that can hold 25
integer values and for loop used to receive 25 integer values and to calculate sum.
in two dimensional array initialization is done row by row. Each row value placed
within braces and each value in a row separated by comma.
Ex
int a[2][3]={{0,0,0},{1,1,1}};
initialize the elements of fist row to zero and second row to one
Page No 9
III B.Sc[Maths] – Programming in C – Unit III
Multidimensional array:
List of data items can be given one name using three or more subscript value is
called multidimensional array. The general form is
type array_name[s1][s2]…[s3];
where si is the size of ith dimension.
Example:
Int number[3][3][3];
number is the three dimensional array declared to contain 27 integer type
elements.
Example program
int a[5][5][5],sum=0,i,j.k;
for(int i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
for(k=0;k<5;k++)
{
Scanf(“%d”,&a[i][j][k]);
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
for(k=0;k<5;k++)
{
Sum=sum+a[i][j][k];
}
}
printf(“%d’”,sum);
In the above program a is the three dimensional array variable that can hold 125
integer values and for loop used to receive 125 integer values and to calculate sum.
Page No 10