Introduction Assignment Control Structure
Introduction Assignment Control Structure
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.