0% found this document useful (0 votes)
44 views21 pages

5,7.control Statements

Uploaded by

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

5,7.control Statements

Uploaded by

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

Procedure Oriented Programming

In POP, program is divided into small parts called functions. In POP, Importance is not given to data but
to functions as well as sequence of actions to be done. POP follows Top Down approach. POP does not
have any access specifier. In POP, Data can move freely from function to function in the system. To add
new data and function in POP is not so easy. In POP, Most function uses Global data for sharing that can
be accessed freely from function to function in the system. POP does not have any proper way for hiding
data so it is less secure. In POP, Overloading is not possible. Example of POP are : C, VB, FORTRAN,
Pascal.

Object Oriented Programming


In OOP, program is divided into parts called objects. In OOP, Importance is given to the data rather than
procedures or functions because it works as a real world. OOP follows Bottom Up approach. OOP has
access specifiers named Public, Private, Protected, etc. In OOP, objects can move and communicate with
each other through member functions. OOP provides an easy way to add new data and function. In OOP,
data can not move easily from function to function, it can be kept public or private so we can control the
access of data. OOP provides Data Hiding so provides more security. In OOP, overloading is possible in
the form of Function Overloading and Operator Overloading. Example of OOP are : C++, JAVA,
VB.NET, C#.NET.

Type conversions – Implicit & Explicit Type Conversion

When variables and constants of different types are combined in an expression then they are converted to
same data type. The process of converting one predefined type into another is called type conversion.

Type conversion in C can be classified into the following two types:

Implicit Type Conversion

When the type conversion is performed automatically by the compiler without programmers intervention,
such type of conversion is known as implicit type conversion or type promotion.

The compiler converts all operands into the data type of the largest operand.

The sequence of rules that are applied while evaluating expressions are given below:

All short and char are automatically converted to int, then,

1. If either of the operand is of type long double, then others will be converted to long double and

result will be long double.


2. Else, if either of the operand is double, then others are converted to double.
3. Else, if either of the operand is float, then others are converted to float.
4. Else, if either of the operand is unsigned long int, then others will be converted to unsigned long
5. int.
6. Else, if one of the operand is long int, and the other is unsigned int, then
1. if a long int can represent all values of an unsigned int, the unsigned int is converted to
2. long int.
3. otherwise, both operands are converted to unsigned long int.
7. Else, if either operand is long int then other will be converted to long int.
8. Else, if either operand is unsigned int then others will be converted to unsigned int.

It should be noted that the final result of expression is converted to type of variable on left side of
assignment operator before assigning value to it.

Also, conversion of float to int causes truncation of fractional part, conversion of double to float causes
rounding of digits and the conversion of long int to int causes dropping of excess higher order bits.

Explicit Type Conversion

The type conversion performed by the programmer by posing the data type of the expression of specific
type is known as explicit type conversion.

The explicit type conversion is also known as type casting.

Type casting in c is done in the following form:

(data_type)expression;

where, data_type is any valid c data type, and expression may be constant, variable or expression.

For example,

1 x=(int)a+b*d;

The following rules have to be followed while converting the expression from one type to another to
avoid the loss of information:

1. All integer types to be converted to float.


2. All float types to be converted to double.
3. All character types to be converted to integer.
Standard input-output functions:

There are 3 types of i/o functions in ‘C’ language.

1. Character-oriented I/O functions


2. Formatted I/O functions
3. String oriented I/O functions

Character-oriented I/O functions:

(a) getchar()
(b) putchar()

getchar(): This function is used to read a single character from the standard input device i.e.,
keyboard.

The general syntax of getchar() function is

charactervariable=getchar();
Example: char ch;
printf(“Enter a character”);
ch=getchar();

putchar(): This function is used to display a single character on the standard input device i.e.,
monitor.
The general syntax of putchar() function is
putchar(charactervariable);
Example: char ch;

printf(“Enter a character”);
ch=getchar();
putchar(ch);
Formatted i/o functions:

(a) scanf()
(b) printf()

scanf(): This function is used to read any kind of data from the standard input device.

The general syntax of scanf() function is

scanf(“control string”, argument list);

Here, the control string contains the format specifications (%d %c etc) and the argument
list contains the addresses of variables that stores the actual data (&n, &ch)

Rules for using scanf() function:

1. The control string must be placed in a pair of double quotes.


2. The variables in the argument list must be preceded by & (ampersand).
3. There should be a one to one mapping between the conversion specifications and the
address given in the argument list.
Examples:
 scanf(“%d %c”, &n, &ch);
x scanf(“%d %c”, &n); no one-one mapping
x scanf(“%d %c”, &n1, &n2);
4. The order and types of conversion specifications in the control string must match with the
order and types of variables used in the argument list.
Examples:
int n;
char ch;
float m;
 scanf(“%d %c %f”, &n, &ch, &m);
x scanf(“%d %c %f”, &n, &m, &ch); ------ mismatch of types
printf(): This function is used to display any kind of data onto the standard input device.
The general syntax of printf() function is
printf(“control string”, argument list);

Here, the control string contains the format specifications (%d %c etc) along with some
message (optional) and the argument list contains the set of variables to be displayed onto the
monitor.

Rules for using printf() function:


1. The control string must be placed in a pair of double quotes.
2. There should be a one to one mapping between the conversion specifications and the
address given in the argument list.
Examples:
 scanf(“%d %c”, n, ch);
x scanf(“%d %c”, n); no one-one mapping
x scanf(“%d ”, n1, n2);
5. The order and types of conversion specifications in the control string must match with the
order and types of variables used in the argument list.
Examples:
int n;
char ch;
float m;
 scanf(“%d %c %f”, n, ch, m);
x scanf(“%d %c %f”, n, m, ch); ------ mismatch of types

The list of format specifiers used in ‘c’ language are given below

Format code Data Type

%d or %i or %n For integers

%c For characters

%f or %e or %g For floating point values

%h For short int

%ld For long integer values

%lf For double type values

%s For char.arrays or strings

%x For hexa decimal values

%o For octal values

%u For unsigned integers

%[ ] For strings with whitespaces


String oriented i/o functions:
(a) gets()
(b) puts()
gets(): This function is used to read a string from the standard input device i.e., keyboard.
The general syntax of gets() function is
getchar(stringvariable);
Example: char str[10];
printf(“Enter a string”);
gets(str);
puts(): This function is used to display a string on the standard input device i.e., monitor.
The general syntax of puts() function is
putchar(stringvariable);
Example: char str[10]=”hello”;

puts(str);
Escape sequences / back slash character constants:
Escape sequences in ‘c’ language are the character representations that may appear either in a
character constant or in a string constant.
An escape sequence always begins with a back slash (\) followed by one or more special
characters.
Description
Escape Sequence
\n New line character

\t To provide tab space

\a To get Bell sound

\\ Single \

%% Single %

\b Backspace

\v Vertical tab

\f Form feed

\r Carriage return

\” Double quotes

\’ Single quote
\\ Back slash

\? Question mark

\0 null

Control statements:
Control statements are used to jump from one location to another location in a program or to
execute a set of statements repeatedly.

The control statements in ‘C’ language can be broadly classified into two categories.
1. Conditional control statements
2. Unconditional control statements
Conditional Control statements:
There are 2 types of conditional control statements in ‘C’ language
1. Selection control statements
2. Looping control statements
Selection control statements:
There are 5 kinds of selection control statements in ‘C’ language.
1. Simple if (1-way decision control statement)
2. If-else (2-way decision control statement)
3. Nested if-else
4. Else-if ladder
5. Switch
Simple if:
The “simple if” is used to test simple conditions.
Working:
 If the condition is true then the set of statements in the if block will be executed.
 If the condition is false, then the control jumps to the statement which is the first
statement after the if block.
The general form of simple if follows:

if (condition/expression)
{
n statements;
}
Statement n+1;

The behavior of simple if statement can be described by using a flowchart as given below.

false condition true

Statement n+1; n statements;


If-else:

The if-else statement is a 2-way decision control statement. A condition is tested at the beginning
of if-else block.
Working:
 If this condition is true, then the set of statements in if block or true block gets executed.
 If this condition is false, then the set of statements in else block or false block gets
executed.
 After executing either if lock or else block, the control jumps to the statement which is
the first statement after the if-else statement.(statement n+1)
The general form follows:

if(condition/expression)
{
// true block
set of statements;
}
else
{ //false block
set of statements;
}
Statement n+1;

The behavior of simple if else statement can be described by using a flowchart as given below
Nested if-else :
Placing one if-else statement inside another if-else statement is called nested if-else statement.
The working of nested if-else statement is same as the if-else statement.
The general form follows:

if(condition1/expression)
{
if(condition2/expression)
{
set of statements;
}
else
{
set of statements;
}
The } behavior of nested if else statement can be
described by using a flowchart as given
below else
{
if(condition3/expression)
{
set of statements;
}
else
{
set of statements;
}
}
Statement n+1;
condition
false Condition1 true
false true

Else-if ladder:
Statements
The else-if ladder isinaelse block decision control statement. Statements in if block
multiway
if-else block(condition3) If-else block(condition2)
If condition1 is true, then the set of statements in if block gets executed and the control
goes out of the else if ladder and executes the first statement after the else-if ladder (statement
n+1).
If condition1 is false, then condition2 will be tested. The above process is repeated until any
of the conditions in the else-if ladder is true.
Statement n+1;
Statement n+1;
If all the conditions are false, then the set of statements in else block gets executed.
The general form follows:

if(condition1/expression)
{//block1
set of statements;
}
else if(condition2/expression)
{//block2
set of statements;
}
else if(condition3/ expression)
{//block3
set of statements;
}
:
else
{//else block
set of statements;
}
Statement n+1;
Switch:
Switch is also a multiway decision control statement which can be used as an alternative for else-
if ladder.
If the value of variable or expression matches with a case value then the set of statements in
the corresponding block gets executed. If there is no match with any of the case values then the
set of statements in default block gets executed.
The “break” statement in the switch causes the control to go out of the switch statement. The
default statement can be placed anywhere within the switch.
The general form follows:

switch (expression)
{
case constant1:
set of statements;
break;
case constant2:
set of statements;
break;
case constant3:
set of statements;
break;
………………………
default:
set of statements;
}

The behavior of switch statement can be described by using the flowchart given below.
Variable/expression
Value1 No matching

Value2 …….. Value n

Block1; Block2; Blockn; Default block


break; break; break;

Statement n+1;

Example:
printf(“Enter day value”);
scanf(“%d”,&day);
switch(day)
{
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;
case 6:
printf(“friday”);break;
case 7:
printf(“Saturday”);break;
default:
printf(“Illegal choice”);
}

Loop control statements:


There are 3 kinds of loop control statements in ‘C’ language
1. While loop ( entry controlled loop)
2. Do while loop (exit controlled loop)
3. For loop (entry controlled loop )
The main purpose of loop control statements is to execute a particular set of statements
repeatedly.
While loop:
The while loop is an “ entry controlled loop “ which may be executed zero or more times.
Working :
 The loop control variable (lcv) must be initialized before the start of while loop ,
 Then a condition will be tested at the beginning of while loop.
 If this condition is true, then the set of statements inside the body of while loop gets
executed.
 The body of while loop must contain a statement which updates the value of loop control
variable.
 After executing the statements in body of while loop, the control goes back to the
condition testing.
 This process is repeated until the condition becomes false.
 If the condition is false, then the control goes out of the while loop ( goes to the statement
n)
The general form follows:

Initialization statement;

while(condition/expression)

set of statements;

increment/decrement
statement;

Next_statements;

The behavior of while loop can be described by using a flowchart given below.
Initialization statement

condition
false true

Next_statement while loop body

Example: I = 5;
while(I<=10)
{
printf (“%d”,I);
I++;
}

Do while loop:
The do-while loop is an “ exit controlled loop “ which may be executed one or more times.
Working:
 The loop control variable (lcv) must be initialized before the start of do-while loop,
 Then the control directly enters into the body of do-while loop and executes all the
statements in the body.
 Then the condition will be tested at the end of do-while loop.
 If the condition is true , then the set of statements in the body of do-while loop will be
executed.
 If the condition is false, then the control goes out of the do-while loop (goes to statement
n+1;)
.
The general form follows:
Initialization statement;
do
{
set of statements;
increment/decrement statement;
}while(condition/expression);

Note : do..while control statement should be terminated by semicolon.


The behavior of do-while loop can be described by using a flowchart given below.

Initialization statement

do-while loop body

condition

false true

Next_statement

Example: I = 5;
do
{
printf (“%d”,I);
I++;
} while(I<=10);
Note: No other conditional control statement should be terminated by a semicolon.

For loop:
The “for” loop is an “entry controlled loop” which may be executed for zero or more times.

Note : In for loop, updation of loop control variable, conditional testing and initialization of loop
control variable are written at one place.
Working:
 In for loop, initialization of loop control variable is done at the beginning of “for” loop,
 Then the control goes to condition testing.
 If the condition is true, then the set of statements inside the body of “for” loop gets
executed and the control will go to the updation of loop control variable.
 After updation, the condition will be tested again.
 The set of statements in the body of “for” loop will be executed until the condition
becomes false.
 Whenever, the condition is false, the control will go out of the for loop (statement n+1;)

The general form follows:

for( initialization ; condition; increment/decrement)


{
Set of statements;
}

Example
ample:

1. for(x = 1;x<=10;x++)
printf(“%d”,x);

2. for(x=10;x>=1;x---)
printf(“%d”,x);

The behavior of “for” loop can be described by using a flowchart given below.
expression1

expression2
false true

for loop body

Next_statement

expression3

Unconditional Control Statements:


There are 3 types of unconditional control statements in ‘C’ language.
1. Break
2. Continue
3. Goto
The unconditional control statements transfers the control from one location to another location
within the ‘C’ program and does not involve any condition testing.
break:
Break statement is a jump statement which can be used to exit from a loop control
statement. The break statement must be used either inside a loop control statement or inside a
switch.
The syntax of break statement is break;

any loop
{
Statement1;
Statement 2;
:
break;
:
}
next statement

When a break statement in a nested loop, then it can break only one loop in which it is present.
Example:

output

I = 0; I=0
while(I<10) Hello
{ I=1
printf (“I=%d”,I); Hello
I++; I=2
if(I==4) Hello
break; I=3
printf(“hello”);}

Continue:
The continue statement is a jump statement which can be used to skip a set of statements
in the loops and continue with the next iteration.
The syntax of continue statement is
continue;

any loop
{
Statement1;
Statement 2;
:
continue;
:
}
next statement

The continue statement must be used inside a loop control statement only. When a continue
statement is written in a nested loop, then the control will be transferred to the condition testing
of the loop in which the continue statement is present.
Example:

output

I = 0; I=0 I=5 Hello


while(I<10) Hello Hello
{ I=1 I=6
printf (“I=%d”,I); Hello Hello
I++; I=2 I=7
if(I==4) Hello Hello
continue; I=3 I=8
printf(“hello”); I=4 Hello
} Hello I=9

goto statement:
The goto statement is a jump statement which is used to transfer the control from one
location to another location within a ‘c’ program.
The syntax of goto statement is

goto labelname;
label:
{
Statement1;
Statement 2;
:
}
:
goto label;

The goto statement can also be used to construct loops by using the backward jumps. The
use of goto statement in a ‘c’ program produces inefficient machine language code. So, it is
recommended to avoid the use of goto statement in ‘c’ programs.
If the block of statements that has label appears before the goto statement, then the
control has to move to backward and that goto is called as backward goto. If the block of
statements that has label appears after the goto statement, then the control has to move to
forward and that goto is called as forward goto.
Example:

Backward goto Forward goto


set1: (label name) goto set1;
Statements; statements;
goto set1; set1: (label name)
statements; Statements;

You might also like