Control Structures and Decision-Making
Control Structures and Decision-Making
PRINCIPLES OF PROGRAMMING
02/19/2025 © Hudson Nandere Lubinga 1
CONTROL
STRUCTURES AND
DECISION-MAKING
// 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())
#include <stdio.h>
void main(){
int n ;
printf("Even numbers up to 10\n");
#include <stdio.h>
void main(){
int n = 0;
printf("Even numbers up to 100\n");
#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.
#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:
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.
8. Data Structures:
• Data structures like arrays, lists, and structures are used to organize
and manage data in a structured manner.