Control Structure in C
Control Structure in C
UNIT II
2.1. Managing I/O Operations
2.2. Reading and Writing A Character
2.3. Formatted Input, Output
2.4. Decision Making and Branching
If Statement
If –Else Statement
Nested If –else statement
Else-If ladder
Switch Statement
The Conditional ( ? : ) Operator
Goto Statement
2.5. The While Statement
2.6. Do-While Statement
2.7. The For Statement
2.8. Jumping From The Loops
Syntax:
var_name = getchar();
Writing a Character
Similar to getchar() there is another function putchar() which is used to write characters, but one
at a time.
Syntax:
putchar(var_name);
Formatted Input
It refers to an input data which has been arranged in a specific format. This is possible in C
using scanf(). We have already encountered this and familiar with this function.
Syntax:
scanf("control string", arg1, arg2, ..., argn);
The field specification for reading integer inputted number is:%w sd .Here the % sign denotes
the conversion specification; w signifies the integer number that defines the field width of the
number to be read. d defines the number to be read in integer format.
Input data items should have to be separated by spaces, tabs or new-line and the punctuation
marks are not counted as separators.
Formatted output
The function printf() is used for formatted output to standard output based on a format
specification. The format specification string, along with the data to be output, are the parameters
to the printf() function.
Syntax:
printf("control string", arg1, arg2, ..., argn);
In this syntax format is the format specification string. This string contains, for each variable to
be output, a specification beginning with the symbol % followed by a character called the
conversion character.
Reading and Writing Strings in C
There are two popular library functions gets() and puts() provides to deal with strings in C.
gets:
The char *gets(char *str) reads a line from stdin and keeps the string pointed to by the str and is
terminated when the new line is read or EOF is reached. The declaration of gets() function is:
Syntax:
char *gets(char *str);
Syntax:
int puts(const char *str)
‘C’ language processes decision making capabilities supports the flowing statements known as
control or decision making statements
1. If statement
2. switch statement
3. conditional operator statement
4. Goto statement
If Statement : The if statement is powerful decision making statement and is used to control
the flow of execution of statements The If statement may be complexity of conditions to be
tested
If(test expression)
{
statement block;
} statement-x ;
The statement -block may be a single statement or a group of statement if the test
expression is true the statement block will be executed. Otherwise the statement -block will be
skipped and the execution will jump to the statement –X. If the condition is true both the
statement –block sequence .
Flow chart :
Ex : If(category = sports)
{ marks = marks + bonus marks;
} printf(“%d”,marks);
If the student belongs to the sports category then additional bonus marks are added to
his marks before they are printed. For other bonus marks are not added .
If –Else Statement : The If statement is an extension of the simple If statement the general
form is
If (test expression)
{
true-block statements;
}
else
{
false-block statements;
}
statement – x;
If the test expression is true then block statement are executed, otherwise the false –block
statement are executed. In both cases either true-block or false-block will be executed not both.
Flow chart :
Ex : If (code == 1)
boy = boy + 1;
else
girl = girl + 1;
st-x;
Here if the code is equal to ‘1’ the statement boy=boy+1; Is executed and the control is
transfered to the statement st-n, after skipping the else part. If code is not equal to ‘1’ the
statement boy =boy+1; is skipped and the statement in the else part girl =girl+1; is executed
before the control reaches the statement st-n.
Nested If –else statement : When a series of decisions are involved we may have to use
more than one if-else statement in nested form of follows .
If(test expression)
{ if(test expression)
{ st –1;
}
else
{ st – 2;
}else
{
st – 3;
}
}st – x;
If the condition is false the st-3 will be executed otherwise it continues to perform the
nested If –else structure (inner part ). If the condition 2 is true the st-1 will be executed
otherwise the st-2 will be evaluated and then the control is transferred to the st-x
If ( test condition1)
{ if (test condition2)
st –1 ;
} else
if (condition 3)
{ if (condition 4)
st – 2;
}st – x;
Else-If ladder : A multi path decision is charm of its in which the statement associated with
each else is an If. It takes the following general form.
If (condition1)
St –1;
Else If (condition2)
St –2;
Else if (condition 3)
St –3;
Else
Default – st;
St –x;
This construct is known as the wise-If ladder. The conditions are evaluated from the top
of the ladder to down wards. As soon as a true condition is found the statement associated with
it is executed and the control the is transferred to the st-X (i.e.., skipping the rest of the ladder).
when all the n-conditions become false then the final else containing the default – st will be
executed.
Switch Statement : Instead of else –if ladder, ‘C’ has a built-in multi-way decision
statement known as a switch. The general form of the switch statement is as follows.
Switch (expression)
{
case value1 : block1;
break;
case value 2 : block 2;
break;
When the switch is executed the value of the expression is successively compared against
the values value-1,value-2------- If a case is found whose value matches with the of the
expression then the block of statements that follows the case are executed .
The break statement at the end of each block signals the end a particular case and causes
an exist from the switch statement transfering the control to the st-x following the switch. The
default is an optional case . If will be executed if the value of the expression doesn’t match with
any Of the case values then control goes to the St-x.
Ex : switch (number)
{
case 1 : printf(“Monday”);
break;
case 2 : printf(“Tuesday”);
break;
case 3 : printf(“Wednesday”);
break;
case 4 : printf(“Thursday”);
break;
case 5 : printf(“Friday”);
break;
default : printf(“Saturday”);
break;
}
The Conditional ( ? : ) Operator : These operator is a combinations of question and
colon and takes three operands this is also known as conditional operator. The general form of
the conditional operator is as follows
Ex : flag = ( x<0) ? 0 : 1
If ( x<0)
Flag = 0;
Else
Flag = 1;
Goto Statement : The goto statement is used to transfer the control of the program from one
point to another. It is something reffered to as unconditionally branching. The goto is used in the
form
Goto label;
Label statement : The label is a valid ‘C’ identifier followed by a colon. we can precode any
statement by a label in the form
Label : statement ;
This statement immediately transfers execution to the statement labeled with the label
identifier.
Ex : i = 1;
bc : if(1>5) Output : 1
goto ab; 2
printf(“%d”,i); 3
++i; 4
gotobc; 5
ab : {
printf(“%d”,i); }
Decision making looping
The ‘C’ language provides three loop constructs for performing loop operations they
are
1. The while statement
2. Do-while statement
3. The for statement
The While Statement : This type of loop is also called an entry controlled, is executed and
if is true then the body of the loop is executed this process repeated until the boolean expression
becomes false. Ones it becomes false the control is a transferred out the loop. The general form
of the while statement is
Ex : i = 1;
While(I<=5)
{ printf(“%d”,i);
i++; }
In the above example the loop with be executed until the condition is false
Do-While Statement : This type of loop is also called an exist controlled loop statement.
i.e.., The boolean expression evaluated at the bottom of the loop and if it is true then body of the
loop executed again and again until the boolean expression becomes false. Ones it becomes false
the control is do-while statement is
Do
{
body of the loop ;
}
while ( boolean expression)
Flowchart:
Ex : i = 1;
Do
{
printf(“%d”,i);
i++;
}
While(i<=5)
The For Statement : The for loop is another entry control led loop that provides a more
concise loop control structure the general form of the for loop is
Where initialization is used to initialize some parameter that controls the looping action,
‘test condition’ represents if that condition is true the body of the loop is executed, otherwise
the loop is terminated After evaluating information and the new value of the control variable is
again tested the loop condition. If the condition is satisfied the body of the loop is again
executed it this process continues until the value of the control variable false to satisfy the
condition.
Flow chart :
Break Statement : The break statement can be accomplish by using to exist the loop. when
break is encountered inside a loop, the loop is immediately exited and the program continues
with the statement which is followed by the loop. If nested loops then the break statement inside
one loop transfers the control to the next outer loop.
Continue Statement : The continue statement which is like break statement. Its work is to
skip the present statement and continues with the next iteration of the loop .