0% found this document useful (0 votes)
2 views

Control Structures and Decision-Making

The document covers control structures and decision-making in programming, focusing on conditional statements (if, else, switch) and iteration (for, while, do). It explains various types of if statements, switch statements, and looping mechanisms, along with examples in different programming languages. Additionally, it discusses structured programming principles, emphasizing code organization, modularization, and the avoidance of goto statements.

Uploaded by

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

Control Structures and Decision-Making

The document covers control structures and decision-making in programming, focusing on conditional statements (if, else, switch) and iteration (for, while, do). It explains various types of if statements, switch statements, and looping mechanisms, along with examples in different programming languages. Additionally, it discusses structured programming principles, emphasizing code organization, modularization, and the avoidance of goto statements.

Uploaded by

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

Prepared by:

HUDSON NANDERE LUBINGA


+256 753 625 255 / +256 773 892 222
[email protected]

PRINCIPLES OF PROGRAMMING
02/19/2025 © Hudson Nandere Lubinga 1
CONTROL
STRUCTURES AND
DECISION-MAKING

02/19/2025 © Hudson Nandere Lubinga 2


What to cover:
• Conditional Statements (if, else, switch)
• Iteration and Loops (for, while, do)
• Structured Programming Principles

02/19/2025 © Hudson Nandere Lubinga 3


Conditional Statements (if, else,
switch)
Decision making Statements
• Decision making statements are the statements that are used to verify
a given condition and decides whether a block of statements gets
executed
• In programming, there are two decision making statements they are
as follows...
• if statement
• Switch/Select statement

02/19/2025 © Hudson Nandere Lubinga 4


if statement
• If statement is used to make decisions based on a condition.
• It verifies the given condition and decides whether a block of
statements are executed or not based on the condition result.
• If statement is classified into four types as follows...
1. Simple if statement
2. if - else statement
3. Nested if statement
4. if-else-if statement (if-else ladder)

02/19/2025 © Hudson Nandere Lubinga 5


Simple if statement
• Simple if statement is used to verify the given condition and executes
the block of statements based on the condition result.
• The simple if statement evaluates specified condition.
• If it is TRUE, it executes the next statement or block of statements.
• If the condition is FALSE, it skips the execution of the next statement
or block of statements.
Syntax and flow diagram

Simple if statement is used when we


have only one option that is executed
or skipped based on a condition.
Example Program | Test whether given number
is divisible by 5 - C
#include <stdio.h>
void main(){
int n ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n%5 == 0 )
printf("Given number is divisible by 5\n") ;
printf("statement does not belong to if!!!")
;
}
if - else statement
• The if - else statement is used to verify the given condition and
executes only one out of the two blocks of statements based on the
condition result.
• The if-else statement evaluates the specified condition.
• If it is TRUE, it executes a block of statements (True block).
• If the condition is FALSE, it executes another block of statements
(False block).
• The if-else statement is used when we have two options and only one
option has to be executed based on a condition result (TRUE or
FALSE).
Syntax and flow diagram
Example Program | Test whether given number is even or odd -
Python
# Input
n = int(input("Enter any integer number:\n"))

# Check if the number is even or odd


if n % 2 == 0:
print("Given number is EVEN")
else:
print("Given number is ODD")
Nested if statement
• Writing an if statement inside another if statement is called nested if
statement.
• The nested if statement can be
defined using any combination of
simple if & if-else statements.
Example Program | Test whether given number is even or
odd if it is below 100 - Java
import java.util.Scanner;

public class EvenOdd {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input
System.out.print("Enter any integer number: ");
int n = scanner.nextInt();

if (n < 100) {
System.out.println("Given number is below 100");
if (n % 2 == 0) {
System.out.println("And it is EVEN");
} else {
System.out.println("And it is ODD");
}
} else {
System.out.println("Given number is not below 100");
}

scanner.close();
}
}
if - else - if (elif) statement (if-else ladder)
• Writing an if statement inside else of an if statement is called if - else -
if statement.
• The if-else-if statement can be
defined using any combination of
simple if & if-else statements.
• The elif keyword is python’s way of
saying "if the previous conditions
were not true, then try this
condition".
Example Program | Find the largest of three numbers - c

#include <stdio.h>
void main(){
int a, b, c ;
printf("Enter any three integer numbers: ") ;
scanf("%d%d%d", &a, &b, &c) ;
if( a>=b && a>=c)
printf("%d is the largest number", a) ;
else if (b>=a && b>=c)
printf("%d is the largest number", b) ;
else
printf("%d is the largest number", c) ;
}
Example Program | Find the largest of
three numbers - Python
# Input 3 integers separated by spaces
a, b, c = map(int, input("Enter any three integer
numbers: ").split())

# Determine the largest number


if a >= b and a >= c:
print(f"{a} is the largest number")
elif b >= a and b >= c:
print(f"{b} is the largest number")
else:
print(f"{c} is the largest number")

02/19/2025 © Hudson Nandere Lubinga 16


Switch/Select statement
• In switch/select statement, we provide a value that is to be compared
with a value associated with each option.
• Whenever the given value matches with the value associated with an
option, the execution starts from that option.
• In switch statement every option is defined as a case.
• If no case value matches with the switch Value specified in the switch
statement, then a special case called default is executed.
• The break statement is used to terminate the switch statement.
• NOTE: In Python, there is no direct equivalent to the switch statement
as found in languages like C++ or Java.
02/19/2025 © Hudson Nandere Lubinga 17
Example of switch in C

#include <stdio.h> case 3:


int main() {
int day = 3; printf("Tuesday");
break;
switch (day) {
default:
case 1: printf("Invalid
day entered");
printf("Sunday"); break;
break; }
case 2: return 0;
}
printf("Monday");
break;
02/19/2025 © Hudson Nandere Lubinga 19
Example of Select in VB.NET
Dim day As Integer = 3

Select Case day


Case 1
MessageBox.Show("Sunday")
Case 2
MessageBox.Show("Monday")
Case 3
MessageBox.Show("Tuesday")
Case Else
MessageBox.Show("Invalid day entered")
End Select
02/19/2025 © Hudson Nandere Lubinga 20
Iteration and Loops (for, while,
do)
• Iteration, also known as looping, is a fundamental programming concept
that allows you to repeat a block of code multiple times.
• Loops are used when you need to perform the same or similar
operations repeatedly
• A Loop is a block of code that is executed repeatedly until a certain
condition is met.
• Most programming languages provide the following looping statements:
1. for statement
2. while statement
3. do-while/do-until statement
02/19/2025 © Hudson Nandere Lubinga 21
for Statement
• The for statement is used to execute
a single statement or a block of
statements repeatedly as long as the
given condition is TRUE.
Example Program | Program to display even numbers up to 10.

#include <stdio.h>
void main(){
int n ;
printf("Even numbers up to 10\n");

for( n = 0 ; n <= 10 ; n++ )


{
if( n%2 == 0)
printf("%d\t", n) ;
# In Python
} print("Even numbers up to
10")
getch() ;
}
for n in range(0, 11):
if n % 2 == 0:
print(n, end='\t')
while Statement
• The while statement is used to
execute a single statement or block
of statements repeatedly as long as
the given condition is TRUE.
• The while statement is also known
as Entry control looping statement.
Example Program | Program to display even numbers up to 100.

#include <stdio.h>
void main(){
int n = 0;
printf("Even numbers up to 100\n");

while( n <= 100 )


{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}
getch() ;
}
do-while Statement in C
• The do-while statement is used to
execute a single statement or block
of statements repeatedly as long as
given the condition is TRUE.
• The do-while statement is also
known as Exit control looping
statement.
Example Program | Program to display even numbers up to
50.

#include <stdio.h>
void main(){
int n = 52;
printf("Even numbers up to 50\n");
do
{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}while( n <= 50 ) ;
getch() ;
}
Other control statements
• There are other control statements which do not need any condition
to control the program execution flow.
• These control statements are called as unconditional control
statements.
• The following are the common unconditional control statements...
• break
• continue
• goto
break statement
• The break statement is used to...
1. terminate switch case statement
2. terminate looping statements like while, do-while and for.
• When a break statement is encountered inside the switch case statement, the
execution control moves out of the switch statement directly.

Example Exit the loop when x is "banana":


fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
The continue Statement
• With the continue statement we can stop the current iteration of the
loop, and continue with the next:
• Example: Do not print banana:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
goto statement
• The goto statement is used to jump from one line to another line in
the program.
• Using goto statement we can jump from top to bottom or bottom to
top.
• To jump from one line to another line, the goto statement requires a
lable.
• Lable is a name given to the instruction or line in the program.
• When we use goto statement in the program, the execution control
directly jumps to the line with specified lable.
Example Program for goto statement.

#include <stdio.h>
void main(){
printf("We are at first printf statement!!!\n") ;
goto last ;
printf("We are at second printf statement!!!\n") ;
printf("We are at third printf statement!!!\n") ;
last: printf("We are at last printf statement!!!\n") ;
getch() ;
}
Structured Programming
Principles
• Structured programming is a programming paradigm that emphasizes
the use of structured control flow constructs to create clear, efficient,
and maintainable code.
• It promotes the organization of code into logical structures, making it
easier to read, understand, and debug.
• Here are some key principles and concepts of structured
programming:

02/19/2025 © Hudson Nandere Lubinga 33


1. Sequence:
• In structured programming, a program is organized as a sequence of
statements executed one after another.
• Code is executed from top to bottom in a linear manner, following the
order of statements.

2. Selection (Conditional Statements):


• Conditional statements, like if, else if, and else, are used to make
decisions in the program.
• They allow you to execute different code blocks based on specific
conditions.

02/19/2025 © Hudson Nandere Lubinga 34


3. Iteration (Loops):
• Loops, such as for, while, and do-while, are used to repeat a block of
code multiple times.
• They enable you to perform repetitive tasks efficiently.

4. Modularization (Functions/Procedures):
• Structured programming encourages the use of functions or
procedures to break down complex problems into smaller,
manageable parts.
• Functions promote code reusability and make the code more
modular.

02/19/2025 © Hudson Nandere Lubinga 35


5. Avoidance of Goto Statements:
• One of the key principles of structured programming is to avoid the
use of goto statements, which can make code difficult to understand
and maintain.

6. Single Entry, Single Exit (SESE):


• Structured programs typically follow the "Single Entry, Single Exit"
rule, meaning that there's only one entry point (usually at the
beginning of a function) and one exit point (usually at the end of the
function).
• This principle enhances code readability and maintainability.

02/19/2025 © Hudson Nandere Lubinga 36


7. Top-Down Design:
• Structured programming often involves a top-down design approach,
where you start with a high-level overview of the program and then
break it down into smaller functions and modules.

8. Data Structures:
• Data structures like arrays, lists, and structures are used to organize
and manage data in a structured manner.

9. Meaningful Variable and Function Names:


• Descriptive and meaningful names for variables and functions are
encouraged to make the code self-documenting.
02/19/2025 © Hudson Nandere Lubinga 37
A structured programming
language
• A structured programming language is one that supports and enforces
structured programming principles and constructs
• Structured programming languages, such as C, Pascal, Java, C++ and
Python, provide constructs that support these principles.
• By adhering to structured programming principles, programmers can
create code that is easier to read, maintain, and debug.
• It also promotes the development of reliable and efficient software.

02/19/2025 © Hudson Nandere Lubinga 38


End
“In God, Everything is Possible!”

02/19/2025 © Hudson Nandere Lubinga 39

You might also like