Chapter 2nd (Control Statements in C)
CHAPTER NO.2 COMPUTER STATEMENTS IN C
2.1 INTRODUCTION
In the 11th class, we have studied about the basics
concepts of C Language such as identifiers, tokens, variables, constants, data
types, operators and expressions etc. in this chapter, we are going to discuss
some more advanced concepts related to programming in C.
Normally in C programming, a program consists of a
number of statements which are usually executed in sequence. This is called
sequential execution. Programs can be much more powerful
if we can control the order in which statements are
executed. In this chapter, we shall discuss about these various powerful
control constructs such as branching, looping and jumping.
Using these constructs we can control the execution
flow in a C program.
2.2 CONTROL STATEMENTS AND THEIR TYPES
C language includes a wide variety of powerful and
flexible control statements. Using these control statements, we can control the
execution flow of program statements. In simple words, we can say that those
statements which are used to control the execution flow in a program are called
Control Statements. We can transfer the control point to a desired location in
the program or we can repeat a statement any number of times using these
control constructs.
These contro] statements can be categorized into
three divisions:
1. Branching Control Statements
2. Looping Control Statements
3. Jumping Control Statements
All these are the powerful control statements which controls the execution of program statements in different ways. Branching statements are used to decide which actions to take,looping statements are used to define how many times to repeat a certain action, and Jumping
statements are
used to transfer unconditional control from one location to other location in
the program. We can use any of these statements anywhere in the program as per
our requirements,Following discussion shows a detailed explanation about these
statements,
2.3 BRANCHING CONTROL STATEMENTS
These statements are branching so called because the
program chooses to follow one branch or another during execution. These
statements can be used for decision - making purpose or for making multi-way
selection. Therefore these statements can further be categorized into two major
categories which given below:
A. Conditional control Statements
B. Multi-way conditional control Statements
A detailed discussion of these control statements
with suitable examples is given below:
2.3.1 Conditional Control Structures
(Decision Making Statement)
These statements are also called decision making
statements, In these constructs, two or more set of statements are written but
only one of these sets is executed. Which set of
statements will be executed depends upon a test
condition. If the test condition evaluates to true, we direct the program to
take one set of actions, otherwise, we direct the program to do
some other set of actions. For conditional control
statements, we use if else statements in the C programs. There are many variations
for using if else statements as show below:
if statement
if else statement
else if statement
nested if statement
2.3.1.1 Simple if statement :
It is the simplest form of if else statement. The ‘if statement’evaluates the
test condition and then proceed to carry out the set of actions only if the
test
condition is evaluated to true, It is terminated
when the test condition evaluates to false, The syntax for using this statement
is given below:
Here, the expression also referred so as test
condition must be enclosed in parenthesis,which causes the expression to be
evaluated first, If is evaluate to true, then the body of the
statements will be executed otherwise this body of
statements will be ignored and control will be passed to the next statement.
Consider the following example:
Program 2,1 ; Write a program in C Language to find
whether the student is
"Passed" by entering marks through the
Keyboard.
2.3.1.2 if else statement : In if else
conditional control statement, statements in if block gets executed only when
the condition is true and statements in else block gets executed only when the
condition is false. The syntax for using if else statement is given below:
Syntax: if (expression)
Block of statements;
else
Block of statements;
}
2.3.1.3 else if statement :
It is a chain of multiple if else statements. Here, statements in body get
executed when the corresponding condition is true. Statements in final else
block gets executed when all other conditions are false. We can use any number
of else if blocks between if and else.
Syntax:
if (expression1)
{
Block 1 of statements;
}
else if(expression2)
{
Block 2 of statements;
}
else
{
Block n of statements;
}
Program 2.3: Write a program to find Grade of the
student if marks>=80 then Grade
A, if marks>=60 and marks<80 then Grade B, if
marks>=40 and marks<60 then Grade
C, otherwise Grade D.
2.3.1.4 Nested-if statement :
Writing the if statement with-in another if is called as nested-if. Inner if is
processed only when outer if condition is true. Hence statements of inner
if gets executed when outer if and inner if
conditions are true.
Syntax:
if(condition1)
{
/* Executes when the condition lis true */
if(condition2)
{
/* Executes when the condition 2 is true */
Block 1 of Statements;
}
}
Program 2.4: Write a program to find largest number
between three numbers entered by the user.
2.3.2 Multi Way Conditional (Case)
Control Structures A multi-way branching is often the most
efficient method of passing control to one of a set of program labels. For
multi-way conditional control statement, we use switch case statement in C
Programs which is explained below with suitable example:
2.3.2.1 switch-case statement :
The statement switch-case is similar to else if statement.It's a matter of
preference which we use; switch statement can be slightly more efficient and
easier to read. In switch-case, statements get executed when corresponding case
constants are
true only. Statements of defaults block gets
executed only when all other cases are false.
Syntax:
switch (variable/expression)
{
case constant]:
statements 1; break:
case constant2:
statements2; break:
case constant3:
statements3; break:
default:
statements;
}
Program 2.5; Write a program to print name of day of
week corresponding to the number of the day entered by the user.
2.4 LOOPING CONTROL STATEMENTS
Looping statements are also called Iterntive
Statements. There may be situations when we need to execute a block of
statements several number of times. In such situations, loops
provide a way to repeat statements and they also
control how many times the statements to be repeated. C provides different
types of loops. All these loops can be categorized into two
categories as given below:
24.1 Pre-Test Loops
2.4.1.1 For loop
2.4.1.2 while loop
24.2 Post-Test Loop
2.4.2.1 Do-while loop
2.4.1 Pre-test Loops
Pre-Test loops are also called Entry-Controlled
loops. In these loops, the control conditions are tested before execution of
the body of loop. The ‘for’ and ‘while’ loops are the entry-
controlled loops in C. These loops are explained
below:
2.4.L.1 for loop :
A for loop is a repetition or iterative control structure that allows us to
write a loop that needs to be executed for a
specific number of times. The ‘for loop’ statements get executed as long as
condition is true. For loop contains three expressions:
Expression] includes initializing the variables
(counter variable),
Expression 2 includes condition and
Expression3 includes increment or decrement of variable
(counter variable).Syntax:for(Expression1(initialization);Expr2
(condition);Expr3(increment/decrement))
{Body of statements;}
Program 2.6: Write a program to print first o
natural numbers using for loop.
2.4.1.2 While loop :
In while-locp, statements get executed as long as condition is true. In
while-loop it checks the condition first and if the condition is true then it
executes the
statements later. So, minimum number of execution
times of statements in while loop is 0.
Program 2.7: Write a program to print even numbers
between 10 to 1 using while loop.
2.4.2 Post-Test Loop
Post-Test loopis also called Exit-Controlled loop.
In this loop, the control conditions are tested after the execution of the
statements of the body of loop. The ‘do while’ loop is the only one
exit-controlled loop in C. This leop is explained below.
24.2.1 do while loop : In do-while loop, statements
get executed as long as condition is true. In do-while loop, it executes the
statements first and checks the condition later. A do-
while loop is similar to a while loop, except that a
do-while loop is guaranteed to execute at least once. Therefore in do while
loop, the minimum number of executions for the body of the leop will be 1.
Syntax: do
{
statements;
} while(condition);
Program 2.8: Write a program to print odd oumbers
from 1 to n (entered by user)using do while loop.
2.5. JUMPING CONTROL STATEMENTS
Jumping statements in C programming are used for
altering the normal flow of a program.Using these statements, we can transfer
the execution control from one location to some other location in the program.
Following are the jumping statements that can be used in the C
programs:
2.5.1 Goto statement
2.5.2 Break statement
2.5.3 Continue statement
All these jumping statements are explained below:
2.5.1 goto statement
A goto in C programming provides an unconditional
jump from the goto statement to a labelled statement in the same function. It
transfers control to a labelled location. The label
must reside in the same function and can appear only
once in the same function. It is a kind of branching structure. It moves the
control flow forward or backward to a label. It is condition dependent.
Syntax:goto label;label: statement;
Here label can be any plain text except C keyword
and it can be set anywhere above or below the goto statement with in a
function.
Program 2.9 Write a program in C to show the use of
goto statement
The two more statements built in C programming to
alter the normal flow of a program are:
break
continue
Loops perform a set of repetitive task (statement)
until condition becomes false but it is sometimes desirable to skip some
statements inside loop or terminate the loop immediately
without checking the test expression. In such cases,
break and continue statements are used.The break statement is also used in
switch statement to exit switch statement,
2.5.2 break statement
Break statement terminates the loop or switch
statement and transfers execution to the statement immediately following the
loop or switch. The break statement in C programming language has the following
two usages:
1. When the break statement is encountered inside a
loop, the loop is immediately terminated and program control resumes at the
next statement following the loop.
2. It can be used tc terminate a case in the switch
statement.If we are using nested loop {i.e., one loop inside another loop), the
break statement will
stop the execution of the innermost loop and start
executing the next line of code after the block.
Syntax:
{
break;
}
2.5.3 continue statement
It is sometimes desirable to skip some statements
inside the loop without exiting the loop.In such cases, continue statements are
used. It causes the loop to skip the remaining statements and immediately
re-test its condition prior to reiterating.
Syntax:
{
continue;
}
RointsahomRemember,
1. C provides
three types of control flow statements: Branching, Looping and Jumping.
2, Branching is so called because the program
chooses to follow one branch or another.
3. Loops provide a way to repeat commands and
control how many times they are repeated.
4. Wecan use
any number of else if blocks between if and else.
5. Writing the if statement with-in another if is
called as nested-if
6. Switch-case is similar to if-else-if statement.
7. In Pre-Test loops, the control conditions are
tested before the body of loop.
8. In Post-Test loops, the control conditions are
tested after the body of loop.
9. A for loop is a repetition or iterative control structure
that allows us to write a loop that needs to execute a specific number of
times.
10. for and while loops are the examples of pre-test
loops whereas do while is an example of post-test loop.
11. Jumping statements in C programming are used for
altering the normal flow of a program.
12. A goto in C programming provides an
unconditional jump from the goto, to a labelled statement in the same function.
13. Break statement can be used to terminate a case
in the switch statement,
14. continue statement is used to skip some
statements inside the loop.
SEGARA
Part-A
1. Multiple Choice Questions:
1. Which of the following statement is also
called as conditional statement?
a. for
b. break
c. if
d, while
2 switch-case is similar to statement
a, ifelse
b. if else if
c. break
d. goto
3. Which statement can be used to terminate
a case in the switch statement?
a continue
b. gato
c. if
d. break
4. Which of the following is an example of
Post Test loop?
a. for
b. while
c. do while
d.Ro
5. Which of the following is not a jumping
statement?
a. while
b. continue
c. goto
d. break
2. Fill in the Blanks:
1 th loops, the control conditions are tested before
the body of loop.
2 In loops, the control conditions are tested after
the body of loop.
3 statement is used to skip some statements inside
the loop.
4. ___ sis: a mullti-way conditional control
statement.
5. The break statement can be used to terminate a
case in the ___ statement.
3. Very Short Answer Type Questions
1. Writing if statement with-in another if is called
as?
2 Which statements in C programming are used for
altering the normal flow of a
program?
3. Which statement is sometimes desirable to skip
some statements inside the loop?
4. Which statements provide a way to repeat commands?
Part-B
4. Short Answer Type Questions. (Write
the answers in 4-5 lines)
1. Define Branching? Name its different control
statements?
2 What is looping? Name three different types of
looping statements?
3. What is nested if statement? Write its syntax?
4. What is if-else statement? Write a program of
if-else statement?
5. What is while statement? Write its syntax?
Part-C
5. Long Answer Type Questions. (Write
the answers in 10-15 lines)
1. What are Control Statements? Explain their types.
2 What is switch statement? Write a program of
switch statement?
3 What is for loop? What are the two different
categories of loops?
4 What is jumping statement? Explain its types?
5 What is do while loop? How it differs from while
loop?