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

Control Structure in C

Uploaded by

Lena Devaraj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Control Structure in C

Uploaded by

Lena Devaraj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PROGRAMMING IN C -7BCE1C1

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

2.1. MANAGING I/O OPERATIONS


Managing i/o as we all know the three essential functions of a computer are reading, processing
and writing data. Majority of the programs take data as input, and display the processed data
after known as result
I/O operations are useful for a program to interact with users. stdlib is the standard C library for
input-output operations. While dealing with input-output operations in C, two important streams
play their role. These are:
1. Standard Input (stdin)
2. Standard Output (stdout)
Standard input or stdin is used for taking input from devices such as the keyboard as a data
stream. Standard output or stdout is used for giving output to a device such as a monitor. For
using I/O functionality, programmers must include stdio header-file within the program.

2.2. READING AND WRITING A CHARACTER


Reading a Character
The easiest and simplest of all I/O operations are taking a character as input by reading that
character from standard input (keyboard). getchar() function can be used to read a single
character. This function is alternate to scanf() function.

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

2.3. FORMATTED INPUT, OUTPUT

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

Where str is a pointer to an array of characters where C strings are stored.


puts:
The function - int puts(const char *str) is used to write a string to stdout, but it does not include
null characters. A new line character needs to be appended to the output. The declaration is:

Syntax:
int puts(const char *str)

where str is the string to be written in C.

Decision Making And Branching

‘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

(a) Simple if statement


(b) If else statement
(c) Nested If-else statement
(d) Else –If ladder

Simple If Statement : The general form of simple if statement is

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

Some other forms of nesting If-else

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.

Ex : If (code = = 1) Color = “red”;


Else if ( code = = 2) Color = “green”
Else if (code = = 3) Color = “white”;
Else Color = “yellow”;

If code number is other than 1,2 and then color is yellow.

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;

default : default block;


break;
}
st – x;

The expression is an integer expression or character value1, value-2---- are constants or


constant expressions and also known as case lables. Each of the values should be a unit within a
switch and may contain zero or more statements.

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

Conditional expression? Expression 1:expression2

The conditional expression is evaluated first If the result is non-zero expression is


evaluated and is returns as the value of the conditional expression, Otherwise expression2 is
evaluated and its value is returned.

Ex : flag = ( x<0) ? 0 : 1

It’s equalent of the If-else structure is as follows

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

While (boolean expression)


{
body of the loop;
}
Flow chart:

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)

While statement Do-while statement


1. It is an entry controlled loop. 1. It is an exit controlled
looping.
2. If boolean expression is false 2. The body of the loop executed
then the body of the loop never atleast ones even be executed if the
Boolean expression is either true or false

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

For ( initialisation; test condition; increment)


{
body of the loop;
}

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 :

Ex : for (I=1; I<=5; I++)


{ Output : 1 2 3 4 5
printf(“%d”,i);
}

Jumping From The Loops :

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.

Ex : for (I=1; I<5; I++)


{ Output : 1 2 3
if ( I == 4)
break;
printf(“%d”,i);
}

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 .

Ex : for (I=1; I<5; I++)


{ Output : 1 2 4 5
if ( I == 3)
continue;
printf(“%d”,i);
}
In the above example when I=3 then the continue statement will rise and skip
statement in the loop and continues for the next iteration i.e.., I=4.

You might also like