0% found this document useful (0 votes)
12 views25 pages

Introduction Assignment Control Structure

Uploaded by

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

Introduction Assignment Control Structure

Uploaded by

SUNEEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

introduction

Assignment Control
Structure
Dr. Suneel Pappala
Introduction
• In programming languages, assignment and control structures form
the backbone of program logic and execution. They provide the
fundamental mechanisms through which data is stored, updated, and
manipulated, and through which the flow of instructions is directed to
achieve specific computational goals.
• Assignment is the operation that associates a value with a variable. It
allows programmers to store results, reuse values, and perform
updates during program execution. For example, in most
programming languages, a statement like x = 10 assigns the value 10
to the variable x. Without assignment, programs would not be able to
maintain state or remember information between computations.
• Control Structures determine the order in which instructions are
executed. They allow a program to make decisions, repeat certain
tasks, or execute different instructions depending on conditions.
Control structures are generally divided into three main categories:
• Sequential – instructions are executed one after another in order.
• Selection (Decision-making) – e.g., if, if-else, switch, which allow branching of
execution based on conditions.
• Iteration (Loops) – e.g., for, while, do-while, which repeat a block of code
until a condition is satisfied.
Selection Statements in Assignment
and Control Structures
• In programming languages, selection statements are a type of control
structure that allow a program to make decisions and execute
different blocks of code based on specified conditions. They are often
called decision-making statements because they control the flow of
execution depending on whether a condition evaluates to true or
false.
• The selection statement works closely with the assignment
statement. An assignment stores values in variables, and those values
are then tested in the selection condition to determine the path of
execution.
example:
• x = 10; // assignment
• if (x > 5) { // selection condition
• printf("x is greater than 5");
•}
• Here, the assignment stores a value in x, and the selection statement
(if) decides what to execute based on that value.
Types of Selection Statements
• If Statement
• Executes a block of code only if the condition is true.
• age = 18
• if age >= 18:
• print("Eligible to vote")
If-Else Statement
Provides two paths: one if the condition is true, and
another if it is false.
• int num = -5;
• if (num >= 0)
• System.out.println("Positive");
• else
• System.out.println("Negative");
• Nested If Statement
• An if inside another if for multiple levels of conditions.
• Else-If Ladder
• Allows checking multiple conditions in sequence.
Switch Statement (available in languages like C, Java, etc.)
Useful when a variable is compared with multiple constant
values.

• int day = 3;
• switch(day) {
• case 1: printf("Monday"); break;
• case 2: printf("Tuesday"); break;
• case 3: printf("Wednesday"); break;
• default: printf("Invalid day");
•}
Iterative Statements
• In programming languages, iterative statements (also called looping
statements) are control structures that allow a block of code to be
executed repeatedly as long as a given condition is true. Instead of
writing the same statements multiple times, iteration provides a
systematic way of repeating operations efficiently.
• Iteration works hand in hand with assignment statements. The values
of variables are often updated (assigned new values) inside a loop,
and these updated values determine whether the loop continues or
stops.
Types of Iterative Statements
• While Loop
• A condition is tested first; if it is true, the body of the loop executes.
• It is known as an entry-controlled loop.
• int i = 1; // assignment
• while (i <= 5) { // iteration
• printf("%d ", i);
• i++; // assignment (updating)
•}
• Output: 1 2 3 4 5
Do-While Loop
Executes the body at least once, then checks the condition.
Known as an exit-controlled loop.

• int i = 1;
• do {
• System.out.println(i);
• i++;
• } while (i <= 5);
For Loop
A compact loop structure that combines initialization,
condition, and increment/decrement in a single line.

• for i in range(1, 6): # Python for loop


• print(i)
• C code
• for (int i = 1; i <= 5; i++) {
• printf("%d ", i);
•}
Nested Loops
One loop inside another, useful for multidimensional
problems like matrices.
• for (int i = 1; i <= 3; i++) {
• for (int j = 1; j <= 3; j++) {
• printf("(%d,%d) ", i, j);
• }
• }
• Importance of Iteration
• Eliminates redundancy (no need to write the same statement repeatedly).
• Efficiently processes large amounts of data.
• Commonly used in tasks like searching, sorting, simulations, and mathematical
calculations.
• Works with assignment to update variables and control loop execution.
Unconditional Branching
• In programming languages, branching refers to altering the normal
sequential flow of execution in a program. When branching is
unconditional, the control of the program jumps directly to another
part of the code without evaluating any condition. Unlike selection
(which depends on conditions) or iteration (which repeats until
conditions are satisfied), unconditional branching transfers control
immediately.
Examples of Unconditional
Branching
• Goto Statement
• The goto statement transfers control to a labeled statement in the
program.
• It is considered risky because it can make the program unstructured
and hard to read ("spaghetti code").
• #include <stdio.h>
• int main() {
• int n = 1;
• start:
• printf("%d ", n);
• n++;
• if (n <= 5) goto start; // unconditional jump
• return 0;
• }
• Output: 1 2 3 4 5
Break Statement
Used to exit from a loop or switch
unconditionally, regardless of the condition.
• for (int i = 1; i <= 10; i++) {
• if (i == 5) break; // unconditional exit from loop
• System.out.println(i);
•}
• Output: 1 2 3 4
Continue Statement
Skips the remaining statements in the current iteration
and moves control to the next iteration of the loop.

• for i in range(1, 6):


• if i == 3:
• continue # unconditional skip
• print(i)
• Output: 1 2 4 5
Return Statement
Immediately transfers control back to the calling
function, ending the current function’s execution.
• int sum(int a, int b) {
• return a + b; // unconditional transfer of result
•}
• Importance of Unconditional Branching
• Provides immediate exit or jump when needed.
• Useful for error handling, loop termination, and function termination.
• Must be used carefully; excessive use (especially goto) reduces
readability and maintainability.
Guarded Commands
• Guarded commands are a control structure introduced by Edsger W.
Dijkstra to provide a more structured and mathematical way of
describing program logic. Unlike traditional if and while statements
that rely on explicit conditions, guarded commands use a set of
boolean expressions (guards), and if a guard is true, its associated
command may be executed.
• The main idea is that program execution becomes nondeterministic
whenever more than one guard evaluates to true, meaning the
system can choose any one of the true guards to proceed. This
introduces flexibility and supports correctness reasoning in program
design.
General Form
Selection (if) guarded command
• Rust
• if
• guard1 -> command1
• [] guard2 -> command2
• [] guard3 -> command3
• fi
• At least one guard must be true for execution to proceed.
• If multiple guards are true, one command is chosen
nondeterministically.
Iteration (do) guarded command
• rust
• do
• guard1 -> command1
• [] guard2 -> command2
• [] guard3 -> command3
• od
• As long as at least one guard is true, one of the corresponding
commands is executed.
• Loop continues until all guards are false.
Example
• if
• x > 0 -> y := y + 1
• [] x == 0 -> y := y
• [] x < 0 -> y := y - 1
• fi
• Here:
• If x > 0, then y increases.
• If x == 0, then y remains the same.
• If x < 0, then y decreases.
• If more than one guard is true, one valid option is chosen nondeterministically.
Example (Iteration)
• do
• x > 0 -> x := x - 1
• [] x < 0 -> x := x + 1
• od
• This loop continues until x becomes zero, adjusting it toward zero
either by incrementing or decrementing depending on its value.
Advantages of Guarded Commands
• Provides a formal, mathematical way to express program logic.
• Supports nondeterminism, which is useful in concurrent and
distributed systems.
• Encourages correctness proofs during program design (e.g., weakest
preconditions).
• Avoids ambiguity of deeply nested if-else constructs.

You might also like