You are on page 1of 28

C Programming

(Upto Control Statement)

1. Who develops C, when and where?

Ans:
Development of ‘C’:
‘C’ was originally developed in the 1970s by Dennis Ritchie at Bell Telephone Laboratories, Inc.
C was largely confined to use within Bell Laboratories until 1978, when Brian Kernighan and
Dennis Ritchie published a definitive description of the language. In 1983, American National
Standards Institute (ANSI) appointed a technical committee to define a standard for C. The
committee approved a version of C in 1989 which is now known as ANSI C. It was then
approved by International Standards Organization (ISO) in 1990.

2. Write down the importance of ‘C’.


Ans:

Importance of C:
1. It is a robust language whose rich set of built-in functions and operators can
be used to write any complex program.
2. The C compiler combines the capability of an assembly language with the
feature of a high-level language.
3. Well-suited for writing both system software and business packages.
4. Due to its variety of data types and powerful operators, programs written in C
are efficient and fast.
5. C is highly portable.
6. C language is well suited for structured programming that makes program
debugging, testing, and maintenance easier.
7. C has the ability to extend itself. We can continuously add our own functions
to the C library.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 1
3. What is structured programming? Briefly describe the executing a ‘C’ program with
necessary diagram.
Or
Why C is called ‘Structured programming language’?
Or
Draw the process of computing and running a C program.

Ans:

Structured programming:
Structured programming refers to a programming strategy that encompasses a number of
methodologies to produce good quality design and code, which can be easily understood, tested,
debugged, modified, and maintained in the future. There are three main principles of structured
programming:

1. Program design using the top-down or bottom-up approach.


2. Modular programming.
3. Structuring of control flow.
 Three basic control flows:
i. Sequence
ii. Selection
iii. iteration

‘C’ supports these features of structured programming strategy hence ‘C’ is called ‘Structured
programming language’.

Executing a ‘C’ program:


Executing a program written in ‘C’ involves a series of steps. These are:
1. Creating the program.
2. Compiling the program.
3. Linking the program with functions that are needed from the C library.
4. Executing the program.

The below figure illustrates the overall process of creating, compiling, and executing a ‘C’
program.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 2
Figure: process of compiling and running a ‘C’ program.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 3
4. What are C Tokens?

Ans:
C Tokens:
The smallest element identified by the compiler in a source file is called a token. It may be a
single character or a sequence of characters to form a single item. C tokes are classified into six
types: Keywords, Identifiers, Constants, Strings, Special symbols, and operators.

5. What is the difference between keyword and identifier?


Or, Write short notes on ‘identifier’.

Ans:
Keywords:
In ‘C’, there are certain reserved words that have standard predefined meanings, these are called
keywords. These keywords can be used only for their intended purpose; they cannot be used as
programmer-defined identifiers.

Identifiers:
Identifiers refer to the names of variables, functions, and arrays. These are user-defined names
and consist of a sequence of letters and digits, with a letter as a first character. Both uppercase
and lowercase letters are permitted. The underscore character is also permitted and the identifier
name must not contain any white space.

6. What is the variable?

Ans:
Variable:
A variable is a data name that may be used to store a data value. Unlike a constant that remains
unchanged during the execution of a program, a variable may take different values at different
times during execution.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 4
7. What are the escape sequence characters? Or Write short notes on: ‘escape sequence’.

Ans:
Escape sequence characters:
An escape sequence always begins with a backslash ( / ) and is followed by one or more
characters. For example, a line feed (LF), which is referred to as a newline in ‘C’, can be
represented as \n . Such escape sequences always represent single characters, even though they
are written in terms of two or more characters.

8. Determine which of the following numerical values are valid constants. If a constant is
valid, specify whether it is integer or float. If invalid, explain why?
i) 1.0 ii) 32,767 iii) 1.3e-12.3 iv) 0587 v) 12345678L vi) 2.8E 7

Ans:

Constant Valid? Remarks


1.0 Yes Represents floating-point constant.
32,767 No A comma is not allowed in integer constant
2.3e-12.3 No Exponent(12.3) is not allowed, it must be an integer
0587 No 8 is not permitted in octal integer constant
12345678L Yes Represents long integer
2.6E 7 No White space is not allowed in floating-point constant

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 5
These are the Rules to identify Valid Constant:

For Integer constant:


 Decimal integers consist of a set of digits, 0 through 9, preceded by an optional - or +
sign. Embedded spaces, commas, and non-digit characters are not permitted between
digits.
 An octal integer constant consists of any combination of digits from the set 0 through
7, with a leading 0.
 A sequence of digits preceded by 0x or 0X is considered as a hexadecimal integer.
 Qualifiers U or u, L of l, and UL or ul can be appended to store larger integer constant
.
Real or floating-point constant:
 Numbers containing fractional parts like 17.548 and can be preceded by an optional -
or + sign.
 Possible to omit digits before the decimal point or digits after the decimal point. That
is 215. .95 -.71 +.5 are valid.
 The general form of exponential is: mantissa e exponent. The mantissa is either a
real number expressed in decimal notation or an integer. An exponent is an integer
number with an optional plus or minus sign.
 Embedded white space is not allowed. Qualifiers F or f, L of l can be appended.

Single character constant:


 A single character contains a single character enclosed within a pair of single quote
marks. Blank space ( ‘ ‘ ) may also be a character constant.

String constant:
 A sequence of characters is enclosed in double quotes. The characters may be letters,
numbers, special characters, and blank spaces.

Backslash character constants:


 Escape sequence characters ‘\n’ are backslash character constant.

Ref: programming in ANSI C , E Balagurusamy, 3rd edition, page 27.


Dr. Abu Nowshed Chy
Assistant Professor, Dept. of CSE, CU 6
9. Write the different ways to define constants in a program?
Or
Write the rules defining a name as a constant.

Ans:
Constants in C refer to the fixed values that do not change during the execution of a program.

The const keyword is used to declare a constant, as shown below:


int const a = 1;
const int a =2;

The preprocessor #define is another more flexible method to define constants in a program.
#define TRUE 1
#define FALSE 0
#define NAME_SIZE 20
Here TRUE, FALSE and NAME_SIZE are constant

10. State the differences between the declaration of a variable and the definition of a
symbolic name.

Ans:
A variable may take different values at different times during execution whereas the value of the
symbolic name constants remains unchanged during the execution of the program.

Declaration of variables:
 Primary Type Declaration:
data type V1, V2, ….. Vn;
where V1, V2, ….. Vn are the names of variables.

Example: int count;

 User-Defined Type Declaration:


typedef type identifier;

Example: typedef int units;

Declaration of symbolic constants:


 #define symbolic-name value-of-constant

Example: #define PASS_MARK 50

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 7
11. Name and describe the four basic data types in C.

Ans:
The ‘C’ language has four basic data types, namely; int, char, float, double

Data Description Typical memory Range of Values


type Requirements
int integer quantity 2 bytes or 1 word -2,147,483,648 to 2,147,483,647

char single character 1byte -128 to 127

float floating point number 1 word (4 bytes) 3.4e-38 to 3.4e+38

double double-precision 2 words (8 bytes) 1.7e-308 to 1.7e+308


floating-point number

** 8bit = 1byte 4bytes = 1word

12. How could we extend the range of values of basic data types?

Ans:
The basic data types can be augmented by the use of the data type qualifiers short, long, signed,
and unsigned. For example, integer quantities can be defined as short int, long int, or unsigned
int. Thus a short int may require less memory than an ordinary int or it may require the same
amount of memory as an ordinary int, but it will never exceed an ordinary int in word length.
Similarly, a long int may require the same amount of memory as an ordinary int or it may require
more memory, but it will never be less than an ordinary int.

13. What is the library function?

Ans:
Library function:
‘C’ library functions are the built-in functions that are accompanied along with the C compiler.
They are defined and declared in their respective header files. They are readily available
functions that can be used directly without writing a code for them. For example: printf ( ) and
scanf ( ) are the formatted input and output functions that are declared and defined in the header
file <stdio.h>

14. Write the elaboration of <stdio.h>.

Ans:
stdio.h refers to the “standard I/O header file” containing standard input and output functions.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 8
15. A C program contains the following declarations and initial assignments:

int i = 8, j = 5;
double x = 8.5, y = -2.1;
char c = ‘E’, s[10] = “CSE CU’’

Determine the value of each of the following expressions which involve use of library
functions.

i) abs(i – 2 * j) iv) pow(2, i)


ii) ceil(x + y) v) tolower(c)
iii) floor(x – y) vi) strlen(s)

Ans:

i) 2
ii) 7.000000
iii) 10.000000
iv) 256.000000
v) e
vi) 6

16. What is an Operator? Write the classification of different types of the operator that
are included in C.

Ans:
Operator:
An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. Operators are used in programs to manipulate data and variables. They usually
form a part of the mathematical or logical expressions.

C operators can be classified into a number of categories. They include:


1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 9
17. What do you about shorthand assignment operators?
Or
What are the advantages of using a shorthand assignment operator?
Ans:
C has a set of ‘shorthand’ assignment operators of the form
v op = exp;
Where v is a variable, exp is an expression and op is a C binary arithmetic operator. Here, the
operator op= is known as the shorthand assignment operator.

For example, the expression a=a+1; can be written by using the shorthand assignment operator
as a +=1;

The use of the shorthand assignment operators has three advantages:


1. What appears on the left-hand side need not be repeated and therefore it becomes
easier to write.
2. The statement is more concise and easier to read.
3. The statement is more efficient.

18. What is the difference between ++i and i++? Explain with example.
Ans:
A prefix operator (++i) first adds 1 to the operand and then the result is assigned to the variable
on left. For example, consider the following statement,
i = 5;
y = ++ i;
after execution the value of y = 6 and i = 6.

On the other hand, a postfix operator (i++) first assigns the value to the variable on left and then
increments the operand.
For example, consider the following statement,
i = 5;
y = i++;
after execution the value of y = 5 and i = 6.

19. What is sizeof operator?


Ans:
The sizeof Operator:
The sizeof is a compile-time operator and, when used with an operand, it returns the number of
bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier.

Example: m = sizeof (sum);

The sizeof operator is normally used to determine the lengths of arrays and structures when their
sizes are not known to the programmer. It is also used to allocate memory space dynamically to
variables during the execution of a program.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 10
20. What do you mean by operator precedence and associativity?

Ans:
Operator precedence:
Operator precedence is used to determine how an expression involving more than one operator is
evaluated. The operators at higher levels of precedence are evaluated first.

Associativity:
The operators of the same precedence are evaluated either from ‘left to right’ or from ‘right to
left’, depending on the level. This is known as the associativity property of an operator.

21. How the natural precedence of operations can be altered?

Ans:
As the expressions within parentheses assume the highest priority so we can alter the natural
precedence of operations by using parentheses. If two or more sets of parentheses appear one
after another as shown below, the expression contained in the left-most set is evaluated first and
the right-most set in the last.
9-12/(3+3)*(2-1)

Parentheses may be nested and in such cases, evaluation of the expression will proceed outward
from the innermost set of parentheses. Just make sure that every opening parenthesis has a
matching closing parenthesis. For example,
9-(12/(3+3)*2)-1 = 4

Moreover, parentheses improve the understandability of the program.

22. What is automatic type conversion and type-casting?


Or
What is Automatic Type Conversion? How can u perform casting operation of a value?

Ans:
Automatic / Implicit type conversion:
C automatically converts any intermediate values to the proper type so that the expression can be
evaluated without loosing any significance. This automatic conversion is known as implicit type
conversion. If the operands are of different types, the ‘lower’ type is automatically converted to
the ‘higher’ type before the operation proceeds.

int main()
{
char x;
int i;
x = 'a';
i = x; /*Implicitly convert x to int, and assign the result to i */
}

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 11
Type casting / Explicit type Conversion:
Explicit type conversions can be forced in any expression, with a unary operator called a cast.
Syntax is: (type-name) expression;

int main()
{
char x;
int i;
x = 'a';
i = (int)x; /*Explicitly convert x to int, assign the result to i */
}

23. What is the limitation of scanf function in case of taking string data?
Or
What is the disadvantage of scanf function in case of taking string input? How will you
solve this problem?
Or
What is the problem of using scanf ( ) in case of taking a line of text? How can u solve
this problem?

Ans:
The limitation of scanf ( ) function in case of taking string data is that %s specifier cannot be
used to read strings with blank spaces i.e.; it can read only the word before the first space. But this
problem can be solved with the help of %[ ] specification. Blank spaces may be included within
the brackets, thus enabling the scanf ( ) to read strings with blank spaces.

24. What is the difference between %[characters] and %[^characters] conversion


specification used in scanf?

Ans:
The specification %[characters] means that only the characters specified within the brackets are
permissible in the input string. If the input string contains any other character, the string will be
terminated at the first encounter of such a character.

The specification %[^character] does exactly the reverse. That is, the characters specified after
the circumflex (^) are not permitted in the input string. The reading of the string will be
terminated at the encounter of one of these characters.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 12
25. What are the commonly used output flags in ‘C’?

Ans:

Commonly used output flags in ‘C’:

Flag Meaning
- Output is left-justified within the field. The remaining
field will be blank
+ + or – will precede the signed numeric item.
0 Causes leading zeros to appear.
#(with o or x) Causes octal and hex items to be preceded by O or Ox,
respectively.
#(with e, f or g) Causes a decimal point to be present in all floating-point
numbers, even if it is the whole number. It also prevents
the truncation of trailing zeros in g-type conversion.

26. Write the output of the following program. [Spring 2012 3(a)]

Ans:
y = 98.7654

Ref: programming in ANSI C , E Balagurusamy, 3rd edition, page 97.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 13
27. The following is a segment of a program:

float x = 999.7, y = 87.546;


i) printf("%3.0f %3.1f", x, y);
ii) printf("%5.2f %4.2f", x, y);

What will be the values of x and y?

Ans:

28. What are the decision making statements?

Ans:
Decision-making statements are used to execute a part of a statement based on some conditions
and exclude others. The decision-making statements available in C are:

1. if statement.
 Simple if statement
 if … else statement
 Nested if … else
 else … if ladder
2. switch statement.
3. Conditional operator statement.
4. goto statement.

29. Define branching. What are the branching techniques used in ‘C’?

Ans:
Branching:
A realistic ‘C’ program may require that a logical test be carried out at some particular point
within the program. One of several possible actions will then be carried out, depending on the
outcome of the logical test. This is known as branching.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 14
The branching techniques available in ‘C’ are:
1. if statement
 simple if statement
 if…else statement
 Nested if…else statement
 else…if ladder
2. switch statement
3. Conditional operator statement.

30. Show the general form to write if-else statement with an example.

Ans:
The general form of an if-else statement is:
if( test expression )
{
True-block statement(s);
}
else
{
False-block statement(s);
}
statement-x;

Here is an example:

#include<stdio.h>
void main ( ) {
float first, second;
scanf (“%f, %f”, &first, &second);

if (first==second)
printf (“\n Both numbers are equal.”);
else
printf (“\n Numbers are not equal.”);
}

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 15
31. What is the difference between if…else statement and else if ladder?

Ans:
The if … else structure is called a double-selection structure because it selects between two
different actions. The general form is,

if( test expression )


{
True-block statement(s);
}
else
{
False-block statement(s);
}
statement-x;

On the other-hand, else…if ladder structure is used when multipath decisions are involved. The
conditions are evaluated from top of the ladder to downwards. The general form is,

if(condition 1)
statement-1;
else if(condition 2)
statement-2;
else if(condition 3)
statement3;
else
default-statement;
statement-x;

32. Draw the flow chart of nesting if…else and else if ladder.

Ans:

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 16
Dr. Abu Nowshed Chy
Assistant Professor, Dept. of CSE, CU 17
33. Why the switch statement is used?

Ans:
Sometimes it becomes cumbersome to write lengthy programs using if and if-else statements. To
avoid this we can use Switch statements. The switch statement is used to select multiple
alternative execution paths. This means it allows any number of possible execution paths.
However, this execution depends on the value of a variable or expression. The switch statement
in C is the best way to test a single expression against a series of possible values and execute the
code. Here is the general form of a switch statement:

34. Compare switch statement with the nested if..else statement

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 18
35. Write the benefits of conditional operator over if..else statement. Write only a single C
statement using the conditional operator of the following.

4𝑥 + 100 𝑓𝑜𝑟 𝑥 < 40


salary = { 300 𝑓𝑜𝑟 𝑥 = 40
4.5𝑥 + 150 𝑓𝑜𝑟 𝑥 > 40
Ans:

Conditional operator:
The conditional operator is the combination of ? and :, and takes three operands. It is useful for
making two-way decisions. The general form is:

Conditional-expression ? expression1 : expression2

The conditional expression is evaluated first. If the result is nonzero, expression1 is evaluated
and is returned as the value of the conditional expression. Otherwise, expression2 is evaluated
and its value returned.

For example, the segment


if(x<0)
flag=0;
else
flag=1;
can be written as : flag = (x<0) ? 0 : 1;

Benefits of the conditional operator:


When the conditional operator is used, the code becomes more concise and perhaps, more
efficient.

Drawbacks:
When the conditional operator is used, the readability of the program is poor. So it is better to
use if statements when more than a single nesting of the conditional operator is required.

4𝑥 + 100 𝑓𝑜𝑟 𝑥 < 40


salary = { 300 𝑓𝑜𝑟 𝑥 = 40
4.5𝑥 + 150 𝑓𝑜𝑟 𝑥 > 40

This complex equation can be written as,

salary = (x==40) ? 300 : ((x<40) ? (4*x+100) : (4.5*x+150));

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 19
36. Compare if..else statement with the ? : operator.

Ans:
Comparison between if … else and ? : operator:

if … else ? : operator
The general form of if … else statement is: The general form of ? : operator is:

if( test expression ) Conditional expression ? expression1 :


{ expression2
True-block statement(s);
}
else
{
False-block statement(s);
}

statement-x;
When if … else is used the code is lengthy When a conditional operator is used, the
and inefficient. code becomes more concise and perhaps,
more efficient.
The code is well-understood i.e.; the The readability of the code is poor.
readability of the code is easy.
Better to use if statements when more than Better to use ? : operator when only a
a single nesting of the conditional operator single nesting of the conditional operator is
is required. required.

37. What are forward jumping and backward jumping? How can we make these jumping
in program?

Ans:

Forward jumping:
In goto, if the label: is placed before the statement goto label; a loop will be formed and some
statements will be executed repeatedly. Such a jump is known as the forward jump.

Backward jumping:
In goto, if the label: is placed after the statement goto label; some statements will be skipped and
the jump is known as the backward jump.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 20
38. Why it is not feasible to create a loop using the goto statement?
Or
Why goto statement is generally discouraged for looping?
Or
Why should the use of goto generally be avoided?

Ans:
‘goto’ statements can cause a jump to any location in your source code, backward or forward.
The indiscriminate use of ‘goto’ statements has caused tangled, miserable, impossible-to-read
programs known as "spaghetti code." Because of this, it is not feasible to create a loop using
goto.

To avoid the use of ‘goto’, more sophisticated, tightly controlled looping commands have been
introduced: for, while, and do...while. Using these makes programs that are more easily
understood, and ‘goto’ is generally avoided, but one might argue that the case has been a bit
overstated. Like any tool, carefully used and in the right hands, ‘goto’ can be a useful construct,
and the ANSI committee decided to keep it in the language because it has its legitimate uses.

39. What is a loop?

Ans:
Loop:
A loop is a sequence of statements that is continually repeated until a certain condition is reached. A
program loop consists of two segments, one known as the body of the loop and the other known as the
control statement.

40. What do you mean by looping?

Ans:
Looping:
A program may require that a group of instructions will be executed repeatedly until some
logical condition has been satisfied. This is known as looping.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 21
41. Using a diagram discuss the exit-controlled loop and entry-controlled loop.

Ans:
Entry-controlled loop:
In the entry-controlled loop, the control conditions are tested before the start of the loop
execution. If the conditions are not satisfied, then the body of the loop will not be executed.
Example: while loop.

Exit-controlled loop:
In the case of an exit-controlled loop, the test is performed at the end of the body of the loop and
therefore the body is executed unconditionally for the first time.
Example: do-while loop.

42. What is the minimum number of times that the do-while loop can be executed?

Ans:
Since the test condition is evaluated at the bottom of the loop, the do…while construct provides
an exit-controlled loop and therefore the body of the loop executed at least once.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 22
43. Compare do-while with while loop.

Ans:

Comparison between while and do…while:

while do … while
The while is an entry-controlled loop that is The do…while is an exit-controlled loop that
the control conditions are tested before the is the test is performed at the end of the
start of the loop execution. If the conditions body of the loop and therefore the body is
are not satisfied, then the body of the loop executed unconditionally for the first time
will not be executed. i.e., the body of the loop executed at least
once though the conditions are not satisfied.

General form: General form:


while (test condition) do
{ {
body of the loop body of the loop
} }
while (test condition)

44. Convert the following program from for loop to while loop.

int i, n = 10;
for (i=0; i<n; i++)
{
printf ("I like for loop.\n");
printf ("%d\n",i);
printf ("I also like while loop.\n");
}

Ans:
The converted code is:

int i, n = 10;

i=0;
while ( i<n ) {
printf("I like for loop.\n");
printf("%d\n",i);
printf("I also like while loop.\n");

i=i+1;
}

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 23
45. Convert the following program from while loop to for loop.
x = 5;
y = 50;
while (x<=y)
{
printf ("%d", x);
y = y/x;
}

Ans:
The converted code is:

y=50;
for ( x=5; x<=y; y=y/x)
{
printf("%d", x);
}

46. Show the comparison of for, while and do-while loop

Ans:

for while do-while

for(n=1;n<=10;++n) n=1; n = 1;
{ while (n<=10) do
------------- { {
------------- -------------- --------------
} -------------- --------------
n=n+1; n=n+1;
} }
while (n<=10);

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 24
47. How does comma operator is used in conjunction with the for statement? Show with
example.

Ans:
The comma operator is used in conjunction with the for statement. This operator permits two
different expressions to appear in situations where only one expression would ordinarily be used.

By using comma operator, more than one variable can be initialized at a time. For example the
statements
p = 1;
for ( n=0; n<17; ++n)
can be rewritten as
for ( p=1, n=0; n<17; ++n)

Like the initialization section, the increment section may also have more than one part. For
example, the loop
for ( n=1, m=50; n<=m; n=n+1, m=m-1)
{
p = m / n;
printf ( “%d %d %d \n”, n, m, p)
}
is perfectly valid. Here the multiple arguments in the increment section are separated by commas

48. What is the null statement in the case of looping? Explain its usefulness.

Ans:
Null statement:
The body of the loop contains only a semicolon, known as a null statement. We can set up time-
delay loops using the null statements as follows:
for( j=1000; j>0; j=j-1 )
;
This loop is executed 1000 times without producing any output; it simply causes a time delay.
This implies that ‘C’ compiler will not give an error message if we place a semicolon by mistake
at the end of for statement. The semicolon will be considered as a null statement and the program
causes a time delay.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 25
49. Discuss with an example, how can a program jump out from a loop?

Ans:
An early exiting from a loop can be accomplished by using the break statement or the goto
statement. Below figure illustrate this:

Fig: Exiting a loop with a break statement.

Fig: Jumping within and Exiting from the loops with goto statement.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 26
When a break statement is encountered inside a loop, the loop is immediately exited and the
program continues with the statement immediately following the loop. When the loops are nested
the break would only exit from the loop containing it. That is, the break will exit only a single
loop.

Since a goto statement can transfer the control to any place in a program, it is useful to provide
branching within a loop. Another important use of goto is to exit from deeply nested loops when
an error occurs. A simple break statement would not work here.

50. What are the purposes of break and continue statements? Explain with example.
Or
Why do we use the continue statement in a loop?

Ans:
Purpose of continue statement:
The continue statement causes the loop to be continued with the next iteration after skipping any
statements in between. The continue statements tell the compiler “skip the following statements
and continue with the next iteration”. The format of the continue statement is simply:

continue;

The use of the continue statement is illustrated in the below figure:

In while and do-while loops, continue causes the control to go directly to the test condition and
then to continue the iteration process.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 27
Purpose of break statement:
An early exiting from a loop can be accomplished by using the break statement When a break
statement is encountered inside a loop, the loop is immediately exited and the program continues
with the statement immediately following the loop. When the loops are nested the break would
only exit from the loop containing it. That is, the break will exit only a single loop.

Fig: Exiting a loop with a break statement.

Dr. Abu Nowshed Chy


Assistant Professor, Dept. of CSE, CU 28

You might also like