0% found this document useful (0 votes)
4 views14 pages

Module - 3

This document provides an overview of control statements in C programming, including conditional and unconditional branching statements, decision control, and looping statements. It explains various constructs such as if statements, switch cases, while loops, do-while loops, for loops, and control flow statements like break, continue, and goto. The content is structured into sections that detail the syntax and usage of each type of control statement.

Uploaded by

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

Module - 3

This document provides an overview of control statements in C programming, including conditional and unconditional branching statements, decision control, and looping statements. It explains various constructs such as if statements, switch cases, while loops, do-while loops, for loops, and control flow statements like break, continue, and goto. The content is structured into sections that detail the syntax and usage of each type of control statement.

Uploaded by

bgmidaredevil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CONTENTS

MODULE-3: Control statements ..................................................................................................... 1

1. Introduction ........................................................................................................................... 1

2. Conditional and Unconditional branching statements .......................................................... 1

3. Decision control and Looping statements ............................................................................. 1

if Statement:................................................................................................................... 2

if else statement ............................................................................................................. 2

if else if statement .......................................................................................................... 3

Switch case .................................................................................................................... 4

4. Iterative Statements ............................................................................................................... 5

while loop: ..................................................................................................................... 6

do–while loop ................................................................................................................ 7

for loop .......................................................................................................................... 8

Break and continue statements .................................................................................... 10

Break statement ........................................................................................................... 10

continue statement: ...................................................................................................... 11

goto statement: ............................................................................................................. 12

1
MODULE-3: CONTROL STATEMENTS.

1. Introduction
▪ The control statements used in the C language help a user to specify a program control’s
flow.
▪ In simpler words, the control statements help users specify the order of execution of the
instructions present in a program.
▪ These make it possible for the program to make certain decisions, perform various tasks
repeatedly, or even jump from any one section of the code to a different section.

2. Conditional and Unconditional branching statements


❑ A branch instruction can be either an unconditional branch, which always results in
branching or a conditional branch, which may or may not cause branching depending on
some condition. Also, depending on how it specifies the address of the new instruction
sequence (the target address).

3. Decision control and Looping statements


❑ Decision control statements are used to alter the flow of a sequence of instructions.
❑ These statements help to jump from one part of the program to another depending on
whether a particular condition is satisfied or not.
❑ These decision control statements include:
🗸 If statement
🗸 If else statement
🗸 If else if statement

2
🗸 Switch statement

3
if Statement:
❑ simplest form of decision control statements used in decision making. The general form of
a simple if statement is shown in the figure.
❑ First the test expression is evaluated. If the test expression is true, the statement of if block
(statement 1 to n) are executed otherwise these statements will be skipped and the execution
will jump to statement x.

Ex:

Output: x=11

if else statement :
❑ first the test expression is evaluated. If the expression is true, statement block 1 is executed
and statement block 2 is skipped. Otherwise, if the expression is false, statement block 2 is
executed and statement block 1 is ignored.
❑ In any case after the statement block 1 or 2 gets executed the control will pass to statement
x. Therefore, statement x is executed in every case.

2
Dr. Naveenkumar D T
if else if statement :
❑ Used to test additional conditions apart from the initial test expression. The if-else-if
construct works in the same way as a normal if statement.

3
Dr. Naveenkumar D T
Switch case:
❑ Is a multi-way decision statement. Switch statements are used:
🗸 When there is only one variable to evaluate in the expression
🗸 When many conditions are being tested for
❑ Switch case statement advantages include:
🗸 Easy to debug, read, understand and maintain
🗸 Execute faster than its equivalent if-else construct

4
Dr. Naveenkumar D T
4. Iterative Statements
❑ Iterative statements are used to repeat the execution of a list of statements, depending on
the value of an integer expression. In this section, we will discuss all these statements.
❑ While loop
❑ Do-while loop
❑ For loop

5
Dr. Naveenkumar D T
while loop:
❑ Used to repeat one or more statements while a particular condition is true.
❑ The condition is tested before any of the statements in the statement block is executed.
❑ If the condition is true, only then the statements will be executed otherwise the control will
jump to the immediate statement outside the while loop block.
❑ We must constantly update the condition of the while loop.

6
Dr. Naveenkumar D T
do–while loop :
❑ similar to the while loop. Except here the test condition is tested at the end of the loop.
❑ The body of the loop gets executed at least one time (even if the condition is false).
❑ Continues to execute whilst a condition is true.
❑ Disadvantage: always executes at least once, so even if the user enters some invalid data,
the loop will execute.
❑ Do-while loops are widely used to print a list of options for a menu driven program
7
Dr. Naveenkumar D T
for loop :
❑ For loop is used to repeat a task until a particular condition is true.
❑ The syntax of a for loop
❑ When a for loop is used, the loop variable is initialized only once.
❑ With every iteration of the loop, the value of the loop variable is updated and the condition
is checked.
❑ If the condition is true, the statement block of the loop is executed else, the statements
comprising the statement block of the for loop are skipped and the control jumps to the
immediate statement following the for loop body.
8
Dr. Naveenkumar D T
❑ Updating the loop variable may include incrementing the loop variable, decrementing the
loop variable or setting it to some other value like, i +=2, where i is the loop variable.

9
Dr. Naveenkumar D T
Break and continue statements

Break statement :
❑ used to terminate the execution of the nearest enclosing loop in which it appears.
❑ the control passes to the statement that follows the loop in which the break statement
appears. Its syntax is quite simple, just type keyword break followed with a semi-colon.

10
Dr. Naveenkumar D T
🗸 break;
❑ In switch statement if the break statement is missing then every case from the matched case
label to the end of the switch, including the default, is executed.

continue statement:
❑ can only appear in the body of a loop.
❑ When the compiler encounters a continue statement then the rest of the statements in the
loop are skipped and the control is unconditionally transferred to the loop-continuation
portion of the nearest enclosing loop.
❑ Its syntax is quite simple, just type keyword continue followed with a semi-colon.
🗸 continue;
❑ If placed within a for loop, the continue statement causes a branch to the code that updates
the loop variable. For example,

11
Dr. Naveenkumar D T
goto statement:
❑ transfer control to a specified label.
❑ Label can be any valid variable name that is followed by a colon (:).
❑ Note that label can be placed anywhere in the program either before or after the goto
statement.
❑ Whenever the goto statement is encountered the control is immediately transferred to the
statements following the label.
❑ If the label is placed after the goto statement then it is called a forward jump and in case it
is located before the goto statement, it is said to be a backward jump.

12
Dr. Naveenkumar D T

You might also like