Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Chapter 3 - Control Structures

Chapter 3 discusses control structures in programming, which dictate the execution order of statements, including sequential, conditional, and iterative structures. It covers various control statements in C, such as decision-making statements (if, switch, and ternary), looping statements (while, do-while, and for), and loop control statements (break, continue, and goto). The chapter also explains nested loops and their execution behavior.

Uploaded by

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

Chapter 3 - Control Structures

Chapter 3 discusses control structures in programming, which dictate the execution order of statements, including sequential, conditional, and iterative structures. It covers various control statements in C, such as decision-making statements (if, switch, and ternary), looping statements (while, do-while, and for), and loop control statements (break, continue, and goto). The chapter also explains nested loops and their execution behavior.

Uploaded by

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

Chapter 3 - Control

Structures
3.1 Control Structures
• In a program, a control structure determines the order in which statements
are executed.
• Control structure refers to the structure of the program that may be
sequential or conditional or iterative.
• In sequential execution each statement in the source code will be executed
one by one in a sequential order. This is the default mode of execution.
• The conditional or selection statement is used for making decisions and
branching statements.
• The iterative or loop structure are used for repetitively executing a block of
code multiple times.
3.2 Control Statement
• The statement that alters the flow of execution of the program is
known as control statement.
• These control statements divert the control flow from one point to
other point in a program
• There are four types of control statements in C programming
• Decision making (Conditional) statements
• Looping statements
• Loop control Statements
3.2.1 Decision making (Conditional)
statements
• A statement that is executed only when a certain condition within a
program has been fulfilled is called conditional statement.
• Conditional statements are used to check if the conditions is TRUE or
False and then branch the execution.
• They are used for decision making.
• Following statements are used to perform the task of decision making
• if statement
• switch statement
• Ternary statement
if statements
• It is used to control the flow of execution of statements.
• It evaluates the condition first and then depending upon whether the
value is true or false, it transfers the control to a particular
statements.
• It has two paths to follow, one for true condition and other for false
condition.
If statements
• If statements are implemented in different form depending on the
complexity of condition to be tested.
• The different forms of statements are:
• Simple if statement
• if…. else statement
• if …. else if ….else ladder
• Nested if…. else statement
Simple if statement
• The simple if statement in C language is used to
execute the code if condition is true.
• If the condition is true, the statement block will
be executed, otherwise the statement block will
be skipped.
• General form:
if (condition) {
// Code to be executed if the condition is true
}
if….else statement
• The if….else statement is used to execute the code if condition is true
or false.
• If the condition is true, the statement inside if is executed.
• If the condition is false, the statement inside else is executed.
• General form:
if (condition) {
// Statement
} else {
// Statement
}
if …. else if ….else ladder
• if …. else if ….else ladder is used to executed code where conditions are
tested sequentially from top to bottom.
• If the first condition is found true then the statement associated with it will
be executed.
• But if the condition is found false, the control goes to second condition in
the else if .
• Similarly, if this second condition is true, the statement associated with it
will be executed. But if is false, the control goes down sequentially to test
other conditions.
• If none of the conditions aree true, the program statement in the else is
executed.
if …. else if ….else ladder
• if …. else if ….else ladder is used to executed code where conditions are
tested sequentially from top to bottom.
• If the first condition is found true then the statement associated with it will
be executed.
• But if the condition is found false, the control goes to second condition in
the else if .
• Similarly, if this second condition is true, the statement associated with it
will be executed. But if is false, the control goes down sequentially to test
other conditions.
• If none of the conditions are true, the program statement in the else is
executed.
if …. else if ….else ladder

• General form :
if (condition1) {
// Statement 1
} else if (condition2) {
// Statement
} else if (condition2) {
// Statement
} else {
// Statement
}
Nested if…. else statement
• Nested if…. else statement is a structure which have another if…else
body of structure within the body.
• In other words, if there is an if…else statement inside another if….else
statement then it is called nested if….else statement.
Nested if…. else statement
• General form:
if (condition1) {
if (condition2){
//Statement 1
} else {
//Statement 2
}
} else {
if (condition3){
//Statement 3
} else {
//Statement 4
}
}
3.2.1.2 Switch statements
• A switch statement allows a variable to be tested for equality against
a list of values.
• Each values is called a case, and the variable being switched on is
checked for each switch case.
• It is similar to if….else ladder. The only difference is that it tests only
for the true value.
• If none of the cases in the switch statement are matching, the default
case will be executed.
• In each block of case there is a break statement which signals the end
of a particular case and causes exit from the switch statement.
3.2.1.2 Switch statements
• Syntax:
3.2.1.2 Switch statements
3.2.1.2 Switch statements
3.2.1.3 Ternary statements
• It’s general format is
conditional_expression ? exp1 :exp2;
• If the conditional_expression is true, exp1 is evaluated and if the
contional expression is false, exp2 is evaluated.
• It is similar to if….else statement.
• E.g.,
x = (a>b) ? a : b;
• In the above example,
• if a > b is true, then x = a
• if a > b is false then x = a
3.2.1.3 Ternary statements
3.2.1.3 Ternary statements
Prog 12. Using ternary statement, write a program to enter and display
the result depending upon following case:
even = 2n odd =2n+1
3.2.2 Looping Statements
• Loop statements are the control statements that allow us to execute a block of
statements repeatedly until a certain termination condition is met.
• C support following looping statements
• while loop
• do-while loop
• for loop
while loop
• A while loop statement executes a block of statements repeatedly as long
as the given condition is found true.
• It is an entry-controlled loop.
• Syntax:
do-while loop

The do...while loop in C is a control flow statement that executes a


block of code at least once, and then repeats the execution based on a
specified condition.
It is an exit-controlled loop.
Syntax:
while vs do-while loop
• The main difference between while and do while loop is
• In a while loop, the condition is checked before the loop body executes. If the
condition is initially false, the loop body may never execute. i.e., it is entry-
controlled loop.
• In a do-while loop, the condition is checked after the loop body executes,
ensuring the loop body runs at least once. It is exit-controlled loop.
for loop
• A for loop is a looping control statement structure that allows us to
mention initial condition, final condition and increment / decrement
inside its expression.
• It is also an entry-controlled loop.
• Syntax:
while, do-while and for loop sample programs
Prog 16. WAP to find the factorial of given
number
Entry Controlled vs Exit Controlled Loop
Entry Controlled Loop Exit Controlled Loop

1. The condition is checked before 1. The condition is checked after the loop
entering the loop body is executed.
2. Statements inside the loop may not be 2. Statements inside the loop executes at
executed even once, if the condition is least once, at the beginning, regardless of
false initially. the condition.
3. E.g., for loop, while loop 3. E.g., do-while loop

4. Control flow diagram 4.Control flow diagram


(draw control flow diagram of for or while (draw control flow diagram of do-while
loop) loop)
5. Sample Program 5. Sample Program
(provide a sample program) (provide a sample program)
3.2.3 Loop Control Statements
• Those statements that can control the continuous execution of the
loop are called loop control statements.
• These includes
• break
• continue
• goto
Break statement
• The break statement has two uses. One is to terminate “case” in the switch
statement and second is to force immediate termination of a loop.
• When the break statement is encountered inside a loop (for, while, do-
while), the loop is immediately terminated and program control goes to the
next statement following the loop.
• Syntax:
break;
Continue statement
• While break statement passes the control out of the loop, continue
statement forces the next iteration, skipping any code in between.
• It is used to block certain loop and then continue onwards.
• Syntax:
continue;
Goto Statement (Jumping Statement)
• The goto statement is used to unconditionally jump to specified label
within the same function.
• When a goto statement is encountered in a loop, the loop is
terminated and the control goes to the label specified by the goto
statement.
• Syntax:
goto label_name;
Nested Loop
• When the body part contains another loop then the inner loop is said
to be nested within the outer loop.
• In nested loop, the inner loop gets execute completely for each pass
of the outer loop before returning to the outer loop.
• All the looping statement (i.e., for, while, do-while) support nesting.
Nested Loop
Nested Loop Sample Program
Prog 20. WAP to print all prime numbers up to N numbers

You might also like