0% found this document useful (0 votes)
3 views

Unit III Converted

This document covers control statements in C programming, including branching statements like if, if-else, and switch, as well as looping statements such as while, do-while, and for loops. It explains the syntax and usage of each type of statement with examples, highlighting how they alter program execution based on conditions. Additionally, it discusses arrays, including one-dimensional, two-dimensional, and multidimensional arrays, along with their initialization and example programs.

Uploaded by

buvaneshwaris831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit III Converted

This document covers control statements in C programming, including branching statements like if, if-else, and switch, as well as looping statements such as while, do-while, and for loops. It explains the syntax and usage of each type of statement with examples, highlighting how they alter program execution based on conditions. Additionally, it discusses arrays, including one-dimensional, two-dimensional, and multidimensional arrays, along with their initialization and example programs.

Uploaded by

buvaneshwaris831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

III B.

Sc[Maths] – Programming in C – Unit III

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

(a) conditional branching statement:


These statements alters the programming execution sequence based on condition.
The following are the conditional branching statemenmts

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.

The if…else if ladder


This is the multipath decision making statement. A miltipath decision is a chain of
ifs in which the statement associated with each else is an if. Its general form is
if(condition 1)
statement -1;
else if(condition 2)
statement -2;
else if (condition 3)
statement -3;
-----------
----------
else if(condition n)
statement – n;
else
default-statement;
statement –x;
The condition are evaluated from top. As soon as a true condition is found, the statement
associated with it is executed and control is transferred to statement – x. When all n
conditions become false, then the final else containing default-statement will be executed.
Example:
If(marks>=60)
printf(“First class”);
else if(marks>=50)
printf(“Second class”);

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;

If condition -1 is false the statement-3 will be executed; otherwise it continues to


perform the second test. If condition-2 is true, the statement-2 will be evaluated;
otherwise statement-2 will be evaluated and then the control is transferred to 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.

Unconditional branching statement:


goto statement used to branch unconditionally from one point to another point in
the program.
The goto statement requires a label in order to identify the place where the branch
is to be made. The general form of goto and label is as follows.

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;
}

The switch statement


Designing program using ‘if’ statement is increase complexity when number of
alternatives increases for that switch statement can be used.
The general form of switch statement is
switch(expression or variable)
{
case value-1: statement 1;
break
case value-2: statement 2;
break;
-------------------

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’ .

Iterative control statement(loop control statements)


The iterative control statements are used to execute the block of repeatedly until
certain condition satisfied or a specific number of times. The c has three loop control
statements, they are
1. while loop
2. do .. while loop
3. for loop
While loop
The general form is
while(condition)
{
Body of loop;
}
It is the entry control loop. While loop first evaluate the condition, if the condition
is true then the it execute the body of loop. The test condition once again evaluated and if
condition is true, the body of loop executed once again. This process continued until the
test condition finally becomes false.

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.

The for statement


The general form is
for(initialization;test-condition;incrememt/decrement)
{
Body of loop;
}
It is the entry control loop. It involve three statements are as follows
Initialization of control variable done first, using assignment statement such as
i=0and j=1. the i and j are called loop control variable/
The value of control variable is tested using the test-condition. The test-condition
is the relational expression such as i<10. if the test-condition is true then the body of loop
will be executed; otherwise the loop is terminated.
The value of control variable is increment/ decrement in third statement. the
updated value of control variable is again tested to see whether it satisfies the test
condition. If the condition is satisfied the body of loop is again executed.

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];

declares the x to be an array containing 5 integer element and computer reserves


five storage locations as shown below

X[0] x[1] x[2] x[3] x[4]


initialization of array
the general form is
type array_namr[size] = {list of values};
one dimensional array variable can be initialize like any other variable at the time
of declaration by placing list of values with in curly braces, the values in the list are
separated by comma. For example,
int number [3] = {0,0,0};
will declare the variable number as an array of size 3 and will assign zero to each
element. If the number of values in the list is less than the number of elements, then only
that many element will be initialized. The remaining element set to zero automatically.
The size may be omitted. In such case the compiler allocate enough space for all
initialized elements. For example
Int number[] = {1,1,1,1};
Will declare number array to contain four elements with initial vales 1.

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.

Two dimensional array:


List of data item can be given one variable name using two subscript value is
called two dimensional array. The general form of two dimensional array is
Syntax:
type array_name[row-size][column-size2];

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

Column 0 column 1 column 2


Row 0 [0][0] [0][1] [0][2]

Row 1 [1][0] [1][1] [1][2]

Row 3 [2][0] [2][1] [2][2]

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.

Two dimensional array initialization:


The general form is
data_type array_name[row_size][col_size] = {{row1 values}, {row2
values},…..{row N values}};

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

You might also like