Unit 3
Unit 3
Unit 3
TECHNIQUES
Unit 3 : Decision
Structures and Branching
& Repetition Programming
and Modularization
• Decision Structures and Branching : Sequential execution, Boolean logic and
the selection structure, Simple selection, multiple selection and Nested
selection, relational comparison operators, working with AND, OR & NOT
logic, understanding of precedence. Pseudo code: Variable, Rules for naming
variables, variable types, basic operators, Basics to pseudo code, merits and
demerits for pseudo code.
• Repetition Programming and Modularization : Introduction, Understanding
the structures: DO WHILE structure and REPEAT…UNTIL Structure, Nested
loop control structures. Concept of modular programming, use of
modularization into a problem solving.
It is an operator. It is a statement.
In this we can write only single statement. In this we can write multiple statements.
Conditional operator can also be used for assigning a value We cannot be used for the assignment purpose.
to the variable.
Syntax : Condition?exp1:exp2; Suntax : if(condition) {
Statements;
}
(age>=18)?printf(“eligible for voting”):printf(“not If(age>=18)
eligible”); printf(“eligible for voting”);
else
printf(“not eligible”);
Condition is checked first then statement(s) is executed. Statement(s) is executed at least once, thereafter condition
is checked.
It might occur statement(s) is executed zero times, If At least once the statement(s) is executed.
condition is false.
No semicolon at the end of while. Semicolon at the end of while.
while(condition) while(condition);
If there is a single statement, brackets are not required. variable may be initialized before or within the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
The mechanisms that allows us to control the flow of execution are called control structures.
Control Structures are just a way to specify flow of control in programs. Any algorithm or program can be
more clear and understood if they use self-contained modules called as logic or control structures. It
basically analyzes and chooses in which direction a program flows based on certain parameters or
conditions.
Flowcharting is a method of documenting (charting) the flow (or paths) that a program would execute.
1. Sequence
2. Selection
3. Iteration
The sequential control structure, as the name implies, follows the sequential flow of control. The execution
of the program statements happens line by line. In the sequence control structure, an activity leads to the
next activity, which is ordered linearly. The sequences may contain plenty of actions, but none of the
actions can be skipped. Every action must be performed sequentially. One of the common applications of
the sequential control structures is that they are used in those programs that perform plenty of input /output
and arithmetic operations.
In the sequence control structure, the execution of the program statements happens line by line in a
sequential manner, following exactly the way they are written in the computer program. Only one statement
gets executed at a time, and it follows the top-down fashion until or unless they get directed by other
control structures.
The selection control structure allows one set of statements to be executed if a condition is true and another
set of actions to be executed if a condition is false. A selection structure, also called an “If-Then-Else”
structure, is flowcharted as follows :
In an entry control loop in C, a condition is checked before executing the body of a loop. It is also called
as a pre-checking loop.
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a
post-checking loop.
The control conditions must be well defined and specified otherwise the loop will execute an infinite
number of times.
The loop that does not stop executing and processes the statements number of times is called as an infinite
loop.
1. Simple selection
2. Multiple selection
3. Nested selection
4. Else if ladder
5. Switch statement
6. Conditional operator
7. Goto statement
Syntax :
if(boolean_expression)
C programming language assumes any non-zero and non-null values as true and if it is either zero or null,
then it is assumed as false value.
Syntax :
if(boolean_expression)
else
Flowchart :
1. An if can have zero or one else's and it must come after any else if's.
2. An if can have zero to many else if's and they must come before the else.
3. Once an else if succeeds, none of the remaining else if's or else's will be tested.
Syntax :
Switch case statements follow a selection-control mechanism and allow a value to change
control of execution. They are a substitute for long if statements that compare a variable to
several integral values.The switch statement is a multiway branch statement. It provides an easy
way to dispatch execution to different parts of code based on the value of the expression.
In C, the switch statement is used for executing one condition from multiple conditions. It is
similar to an if-else-if ladder.Switch statement consists of conditional based cases and a default
case.In a switch statement, the “case value” can be of “char” and “int” type.
Following are some of the rules while using the switch statement:
1) Break: This keyword is used to stop the execution inside a switch block. It helps to terminate the switch block and
break out of it.
2) Default: This keyword is used to specify the set of statements to execute if there is no case match.
1) The expression provided in the switch should result in a constant value otherwise it would not be valid.
3) The default statement is optional. Even if the switch case statement do not have a default statement, it would run
without any problem.
4) The break statement is used inside the switch to terminate astatement sequence. When a break statement is reached,
the switch terminates, and the flow of control jumps to the next line following the switch statement.
5) The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall
through to subsequent cases until a break is reached.
6) Nesting of switch statements is allowed, which means you can have switch statements inside another switch.
However nested switch statements should be avoided as it makes the program more complex and less readable.
7) Switch statements are limited to integer values and characters only in the check condition.
Prepared by : Ms. Aditi Mehta 23
Example :
Syntax :
In the above syntax, the first line tells the compiler to go to or jump to the statement marked as a
label. Here label is a user-defined identifier which indicates the target statement. The statement
immediately followed after ‘label:’ is the destination statement. The ‘label:’ can also appear
before the ‘goto label;’ statement in the above syntax.
Prepared by : Ms. Aditi Mehta 25
Example :
#include<stdio.h>
void main()
{
int num;
printf(“Enter number : “);
scanf(“%d”,&num);
if (num % 2 == 0)
goto even;
else
goto odd;
even:
printf("%d is even", num);
return;
odd:
printf("%d is odd", num);
getch();
}
Example,
50 – 2 * 15 is going to yield 20. It is because it gets evaluated as 50 – (2 * 15), and not as (50 – 2) * 15. The
reason here is that subtraction (-) has lower precedence as compared to multiplication (*).
Operator Associativity
It is is used when two operators of same precedence appear in an expression. Associativity can be either Left to
Right or Right to Left.
Example,
The precedence of Division and Multiplication arithmetic operators is the same. So, let’s say we have an
expression with us which is 6 * 3 / 20. The evaluation of this expression would be (6 * 3) / 20 because the
associativity will be left to right for both the operators – multiplication and division. In a similar case, the
calculation of 40 / 4 * 5 would be (40 / 4) * 5 because the associativity would be from right to left here as well.
1. For loop
2. While loop
3. Do while loop
5. Repeat until loop/As an alternative to the repeat/until block loop, the while/do loop will evaluate a
condition at the start of each iteration, thus providing a loop that can skip even the first iteration.
Syntax :
//statement(s);
• The initialization Statement step is executed first, and only once. This step allows you to
declare and initialize any loop control variables. You are not required to put a statement here,
as long as a semicolon appears.
• After the body of the 'for' loop executes, the flow of control jumps back up to the
increment/decrement/update statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after the condition.
• The condition is now evaluated again. If it is true, the loop executes and the process repeats
itself (body of loop, then increment step, and then again condition). After the condition
becomes false, the 'for' loop terminates.
Syntax :
while(Condition)
//statement(s);
While we are working with a while loop first we need to check the condition, if the condition is true
then the control will pass within the body if it is false the control pass outside the body.When we are
working with an iteration statement after execution of the body control will be passed back to the
condition, and until the condition becomes false. If the condition is not false, then we will get an
infinite loop.
Prepared by : Ms. Aditi Mehta 33
While we are working with a while loop first we need to check the condition, if the condition is
true then the control will pass within the body if it is false the control pass outside the body.
When we are working with an iteration statement after execution of the body control will be
passed back to the condition, and until the condition becomes false. If the condition is not false,
then we will get an infinite loop.
The pre-checking process means before the evaluation of the statement block conditional part
will be executed. When we are working with a while loop always pre-checking process will
occur. The loop in which before executing the body of the loop if the condition is tested first
then it is called an entry controlled loop.
While loop is an example of entry controlled loop because in the while loop before executing the
body first condition is evaluated if the condition is true then the body will be executed otherwise
the body will be skipped.
Syntax :
do
//statement(s);
while (condition);
Syntax :
Syntax :
while(condition)
while(condition)
Syntax :
do
do
}while(condition);
}while(condition);
Using the continue statement in the nested loop skips the inner loop's iteration only and doesn't
affect the outer loop.
Modular programming is a method of dividing a large program into small modules called functions. A
module is defined as a part of a software program that contains one or more routines. When we merge one
or more modules, it makes up a program.
3. Modularization allows you to reuse work more easily : There are times where a piece of
code is implemented everywhere in our program. Instead of copying and pasting it, again
and again, modularity gives us the advantage of reusability so that we can pull our code
from anywhere using interfaces or libraries. The concept of reusability also reduces the size
of our program.
1. There is a need for extra time and budget for a product in modular programming.
3. Careful documentation is required so that other program modules are not affected.
4. Some modules may partly repeat the task performed by other modules. Hence, Modular
programs need more memory space and extra time for execution.
5. Integrating various modules into a single program may not be a task because different
people working on the design of different modules may not have the same style.
6. It reduces the program's efficiency because testing and debugging are time-consuming,
where each function contains a thousand lines of code.