Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 50

LOGIC BUILDING

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.

Prepared by : Ms. Aditi Mehta 3


DIFFERENCE BETWEEN CONDITIONAL OPERATOR AND IF_ELSE?
CONDITIONAL OPERATOR IF_ELSE STATEMENT

It is an operator. It is a statement.

In this we can write only single statement. In this we can write multiple statements.

It is a single programming statement. It is a programming block in which statements come under


the parenthesis.
Nesting is more complex. Nesting is easy to read and maintain.

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”);

Prepared by : Ms. Aditi Mehta 4


DIFFERENCE BETWEEN WHILE AND DO-WHILE LOOP?
WHILE LOOP DO-WHILE LOOP

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.

Syntax : while(condition) Syntax : do { statement(s); }


{ statement(s); } while(condition);
while(i >= 1) do{
{ printf("%d \n",i);
printf("%d ", i); i++;
i = i - 1; }while(i<=10);
}

Prepared by : Ms. Aditi Mehta 5


WHAT IS CONTROL STRUCTURE ?
One of the most important concept of programming is the ability to control a program so that different lines
of code are executed or that some lines of code are executed many times.

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.

There are three main categories of control structures:

1. Sequence

2. Selection

3. Iteration

Prepared by : Ms. Aditi Mehta 6


WHAT IS SEQUENTIAL(SEQUENCE) STRUCTURE ?
The program which flows only from top to bottom without changing the flow control are known as
sequence control structure.It performs actions or tasks in order, one after the other. A sequence can contain
any number of tasks, but there is no option to branch off and skip any of the tasks. Once you start a series of
actions in a sequence, you must continue step by step until the sequence ends.

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.

Prepared by : Ms. Aditi Mehta 7


C program to calculate area of rectangle
#include<stdio.h>
#include<conio.h>
void main( )
{
int l, b, a;
printf(“Enter length and breadth”);
scanf(“%d %d”, &l, &b);
a = l*b;
printf(“Area is %d”, a);
getch();
}

Prepared by : Ms. Aditi Mehta 8


WHAT IS SELECTION STRUCTURE ?
Also known as a conditional structure, a selection structure is a programming feature that performs
different processes based on whether a Boolean condition or expression is true or false. Selection structures
use relational operators to test conditions.

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 :

Prepared by : Ms. Aditi Mehta 9


WHAT IS ITERATION/LOOPING STRUCTURE ?
Looping Statements in C execute the sequence of statements many times until the stated condition becomes
false. A loop in C consists of two parts, a body of a loop and a control statement. The control statement is a
combination of some conditions that direct the body of the loop to execute until the specified condition
becomes false. The purpose of the C loop is to repeat the same code a number of times.

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.

An infinite loop is also called as an “Endless loop.”

Prepared by : Ms. Aditi Mehta 10


Following are some characteristics of an infinite loop:

1. No termination condition is specified.

2. The specified conditions never meet.

The specified condition determines whether to execute the loop body or


not.

‘C’ programming language provides us with three types of loop constructs:

1. The while loop

2. The do-while loop

3. The for loop

Prepared by : Ms. Aditi Mehta 11


EXPLAIN SELECTION STRUCTURE IN DETAIL?
Decision making structures require that the programmer specifies one or more conditions to be evaluated or
tested by the program, along with a statement or statements to be executed if the condition is determined to
be true, and optionally, other statements to be executed if the condition is determined to be false.

Selection structure consist of :

1. Simple selection

2. Multiple selection

3. Nested selection

4. Else if ladder

5. Switch statement

6. Conditional operator

7. Goto statement

Prepared by : Ms. Aditi Mehta 12


1. Simple selection : An if statement consists of a boolean expression followed by one or more
statements.

Syntax :

if(boolean_expression)

//statements will execute

if the boolean expression is true

Prepared by : Ms. Aditi Mehta 13


If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed.
If the Boolean expression evaluates to false, then the first set of code after the end of the 'if' statement (after
the closing curly brace) will be executed.

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.

Prepared by : Ms. Aditi Mehta 14


2. Multiple selection : Also known as IF_ELSE. An if statement can be followed by an
optional else statement, which executes when the Boolean expression is false.

Syntax :

if(boolean_expression)

/* statement(s) will execute if the boolean expression is true */

else

/* statement(s) will execute if the boolean expression is false */

Prepared by : Ms. Aditi Mehta 15


If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else
block will be executed.

Prepared by : Ms. Aditi Mehta 16


3. Nested if selection : It is always legal in C programming to nest if-else statements, which
means you can use one if or else if statement inside another if or else if statement(s). A nested if
in C is an if statement that is the target of another if statement. Nested if statements mean an if
statement inside another if statement.

Flowchart :

Prepared by : Ms. Aditi Mehta 17


Syntax :

Prepared by : Ms. Aditi Mehta 18


4. Else If ladder : An if statement can be followed by an optional else if...else statement, which
is very useful to test various conditions using single if...else if statement.When using if...else
if..else statements, there are few points to keep in mind −

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 :

Prepared by : Ms. Aditi Mehta 19


Example :

Prepared by : Ms. Aditi Mehta 20


5. Switch Statement : Switch case statement evaluates a given expression and based on the
evaluated value(matching a certain condition), it executes the statements associated with it.
Basically, it is used to perform different actions based on different conditions(cases).

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. There can be one or N numbers of cases.

2. The values in the case must be unique.

3. Each statement of the case can have a break statement. It is optional.


Prepared by : Ms. Aditi Mehta 21
Prepared by : Ms. Aditi Mehta 22
Some important keywords:

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.

Important Points About Switch Case Statements:

1) The expression provided in the switch should result in a constant value otherwise it would not be valid.

2) Duplicate case values are not allowed.

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 :

Prepared by : Ms. Aditi Mehta 24


6. Goto Statement : The goto statement is a jump statement which is sometimes also referred to
as unconditional jump statement. The goto statement can be used to jump from anywhere to
anywhere within a function.

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();
}

Prepared by : Ms. Aditi Mehta 26


Operator Precedence
Operator precedence helps us determine which of the operators in an expression must be evaluated first in case
the expression consists of more than a single operator. (BODMAS)

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.

Prepared by : Ms. Aditi Mehta 27


Prepared by : Ms. Aditi Mehta 28
WHAT IS REPETITION STRUCTURE ?
Repetition structures are used to repeat statements or blocks of code. The decision whether to repeat the
code is based on the evaluation of a logical expression. If the expression is true, the code is executed. If
false, the code is not executed.

In a repetition structure we can use below statements;

1. For loop

2. While loop

3. Do while loop

4. Nested for 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.

Prepared by : Ms. Aditi Mehta 29


1. For Loop : A for loop is a repetition control structure that allows you to efficiently write a
loop that needs to execute a specific number of times. This is called entry controlled loop.

Syntax :

for ( initialization Statement; condition/test Expression; increment-decrement/update Statement )

//statement(s);

Here is the flow of control in a 'for' loop −

• 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.

Prepared by : Ms. Aditi Mehta 30


• Next, the condition/test Expression is evaluated. If it is true, the body of the loop is executed.
If it is false, the body of the loop does not execute and the flow of control jumps to the next
statement just after the 'for' loop.

• 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.

Prepared by : Ms. Aditi Mehta 31


Example :

Prepared by : Ms. Aditi Mehta 32


2. While Loop : A while loop is used for executing a statement repeatedly until a given condition
returns false. Here, statements may be a single statement or a block of statements. The condition may
be any expression, and true is any nonzero value. The loop iterates while the condition is true. If you
see the syntax and flowchart parallelly, then you will get more clarity of the while loop. This is called
entry controlled loop.

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.

What is the pre-checking process or entry controlled 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.

Prepared by : Ms. Aditi Mehta 34


Example :

Prepared by : Ms. Aditi Mehta 35


3. Do While Loop : The do while loop is a post tested loop. Using the do-while loop, we can
repeat the execution of several parts of the statements. The do-while loop is mainly used in the
case where we need to execute the loop at least once. The do-while loop is mostly used in menu-
driven programs where the termination condition depends upon the end user. It is called exit
control loop.

Syntax :

do

//statement(s);

while (condition);

Prepared by : Ms. Aditi Mehta 36


Example :

Prepared by : Ms. Aditi Mehta 37


4. Nested for loop : The nested for loop means any type of loop which is defined inside the 'for'
loop.

Syntax :

for (initialization; condition; update)

for(initialization; condition; update)

// inner loop statements.

// outer loop statements.

Prepared by : Ms. Aditi Mehta 38


Example :

Prepared by : Ms. Aditi Mehta 39


5. Nested while loop : The nested while loop means any type of loop which is defined inside
the 'while' loop.

Syntax :

while(condition)

while(condition)

// inner loop statements.

// outer loop statements.

Prepared by : Ms. Aditi Mehta 40


Example :

Prepared by : Ms. Aditi Mehta 41


6. Nested do while loop : The nested do..while loop means any type of loop which is defined
inside the 'do..while' loop.

Syntax :

do

do

// inner loop statements.

}while(condition);

// outer loop statements.

}while(condition);

Prepared by : Ms. Aditi Mehta 42


Example : Output :

Prepared by : Ms. Aditi Mehta 43


WHAT ARE JUMP STATEMENTS
1. Break Statement : The break is a keyword in C which is used to bring the program control
out of the loop. The break statement is used inside loops or switch statement. The break
statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop
first and then proceeds to outer loops.

Prepared by : Ms. Aditi Mehta 44


2. Continue Statement : Continue in jump statement in C skips the specific iteration in the loop.
It is similar to the break statement, but instead of terminating the whole loop, it skips the current
iteration and continues from the next iteration in the same loop. It brings the control of a
program to the beginning of the loop.

Using the continue statement in the nested loop skips the inner loop's iteration only and doesn't
affect the outer loop.

Prepared by : Ms. Aditi Mehta 45


WHAT IS MODULAR PROGRAMMING ?
Modular programming is defined as a software design technique that focuses on separating
the program functionality into independent, interchangeable methods/modules. Each of
them contains everything needed to execute only one aspect of functionality.
Talking of modularity in terms of files and repositories, modularity can be on different
levels -
1. Libraries in projects
2. Function in the files
3. Files in the libraries or repositories
Modularity is all about making blocks, and each block is made with the help of other
blocks. Every block in itself is solid and testable and can be stacked together to create an
entire application.

Prepared by : Ms. Aditi Mehta 46


The process of breaking down a large program into modules is modularization; computer scientists also call
it functional decomposition.

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.

Prepared by : Ms. Aditi Mehta 47


Advantages of Modular Programming :

1. Modularization provides abstraction : Abstraction is the process of paying attention to


important properties while ignoring nonessential details. Abstraction makes complex tasks
look simple. Modularized programs are easier to understand.

2. Modularization helps multiple programmers to work on a problem : As program is divided


into smaller functions, many people can work on the same program it is easier to work and
understand the smaller modules compared to one large 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.

Prepared by : Ms. Aditi Mehta 48


Disadvantages of Modular Programming :

1. There is a need for extra time and budget for a product in modular programming.

2. It is a challenging task to combine all the modules.

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.

Prepared by : Ms. Aditi Mehta 49


Example :
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20,c;
c = sum(a,b);
printf(“Sum : %d”, c);
getch();
}
int sum(int x,int y)
{
printf ("value of x in sum() = %d\n", x);
printf ("value of y in sum() = %d\n", y);
return x+ y;
}

Prepared by : Ms. Aditi Mehta 50

You might also like