04 Control_flow_structure Pr (1)
04 Control_flow_structure Pr (1)
STRUCTURES
Sequence control structures.
Selection control structures.
Single selection structure (IF - Statement).
Multi selection control structure.
• ( IF-ELSE Statement).
• Nested IF (IF-ELSE-IF) Statement.
Switch case control structure.
Repetition control structure.
While control structure.
Do-while repetition control structure.
For repetition control structure.
2
MUNYAO 10/29/2024
Are used to conditionally execute statements, to
repeatedly execute a block of statements and to
otherwise change the sequential flow of control.
They block up the flow of execution by employing
decision making, looping, and branching enabling your
program to conditionally execute particular block of
code.
They add intelligence to a program
3
MUNYAO 10/29/2024
This is control structure where instructions are
executed one after the other in the order they were
written.
4
MUNYAO 10/29/2024
Used to select among many alternatives
There are three types of selection control structure namely:
Single selection structure (IF - Statement).
Multi selection control structure.
• ( IF-ELSE Statement).
• Nested IF (IF-ELSE-IF) Statement.
Switch case control structure.
5
MUNYAO 10/29/2024
Decision-Making Statements
√Determine whether a condition is true, and take
some action based on determination
if (balance == 0) {
User.message(“You have no money”);
}
All IF statements produce a TRUE/FALSE outcome
The result of TRUE above causes the block to be
executed – “You have no money”
10
MUNYAO 10/29/2024
class IfTest
{
public static void main(String arg[])
{
int x=5;
int y=4;
if(x>y)
{
System.out.println("x is greater than y");
System.out.println("am happy");
}
else
System.out.println("y is greater than x");
}
}
11
MUNYAO 10/29/2024
Sales people in a company xyz are given a commission
of 10% of their sales value, if the sales value exceeds
10,000. Write a program that will compute the
commission for these sales people.
NB: The if selection structure normally expects only one
statement in its body. To include
several statements in the body of an if structure, enclose
the statements in braces ({ and
}). A set of statements contained within a pair of braces is
called a block.
12
MUNYAO 10/29/2024
a) IF-ELSE
Used where we are selecting more than one alternative. The
statement enables one to perform a certain action if a condition
(Boolean Expression) is true and to perform an alternative
action if the condition is false.
if (balance < 0) {
User.message(“You have overdrawn”)
}
else {
User.message(“You have some money!”)
}
ELSE identifies the block of code to execute if the condition is
FALSE i.e. balance >= 0
13
MUNYAO 10/29/2024
import java.util.Scanner;
public class StudentGradeIF {
14
MUNYAO 10/29/2024
import java.util.Scanner;
public class Odd_EvenDeterminant {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int x = 0;
System.out.println("Enter a number:");
x = in.nextInt();
if (x % 2 == 0)
System.out.println(x + " is even.");
else
System.out.println(x + " is old.");
}}
15
MUNYAO 10/29/2024
import java.util.Scanner;
public class Calculate {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int x = 0;
int y = 0;
int sum = 0;
System.out.println("Enter a number:");
x = in.nextInt();
System.out.println("Enter a second number:");
y = in.nextInt();
System.out.println("Enter the sum of :"+ x+" and "+y);
sum = in.nextInt();
if (sum == x+y)
System.out.println(sum + " is the correct answer.Congratulations");
else
System.out.println(sum + " is wrong answer. Try again");
}}
16
MUNYAO 10/29/2024
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int radius;
final double PI=3.142;
double area=0;
System.out.println("Enter a radius of a cirle:");
radius = in.nextInt();
if (radius >= 0)
{
area = radius*radius*PI;
System.out.println("The area for the " + "circle of radius " + radius +
" is " + area);
}
else
{
System.out.println("Negative input");
} } }
import javax.swing.JOptionPane;
public class Old_EvenDeterminant {
public static void main(String[] args) {
// Prompt the user to enter an integer
String intString = JOptionPane.showInputDialog( "Enter an integer:");
// Convert string into int
int number = Integer.parseInt(intString);
if (number % 2 == 0)
//System.out.println(number + " is even.");
JOptionPane.showMessageDialog(null, number + " is even ");
else
//System.out.println(number + " is old.");
JOptionPane.showMessageDialog(null, number + " is old ");
}
}
18
MUNYAO 10/29/2024
import javax.swing.JOptionPane;
public class LeapYearCalculator {
public static void main(String args[]) {
// Prompt the user to enter a year
String yearString = JOptionPane.showInputDialog("Enter a year");
// Convert the string into an int value
int year = Integer.parseInt(yearString);
// Check if the year is a leap year
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400
== 0);
// Display the result in a message dialog box
JOptionPane.showMessageDialog(null, year + " is a leap year? " +
isLeapYear);
}
}
19
MUNYAO 10/29/2024
Nested IF Statements
import java.util.Scanner;
public class ValueChecker{
public static void main(String[] args){
int value = 0;
System.out.println("Enter Students Marks:");
value = in.nextInt();
if( value <= 39)
{ System.out.println("YOUR GRADE IS : Fail!"); }
else if( value <= 49)
{ System.out.println("YOUR GRADE IS: D"); }
else if( value <= 59)
{ System.out.println("YOUR GRADE IS: C"); }
else if( value <= 69)
{ System.out.println("YOUR GRADE IS: B"); }
else { System.out.println("YOUR GRADE IS : A !"); }
}}
21
MUNYAO 10/29/2024
import java.util.Scanner;
public class ValueChecker{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int value = 0;
System.out.println("Enter a number:");
value = in.nextInt();
if( value == 7) {
System.out.println("That's lucky!");
}
else if( value == 13) {
System.out.println("That's unlucky!");
}
else {
System.out.println("That is neither lucky nor unlucky!");
} }
}
22
MUNYAO 10/29/2024
Nested if/else structures test for multiple cases by placing
if/else structures
inside if/else structures.
if ( grade >= 70 )
System.out.println( "A" );
else if ( grade >= 60 )
System.out.println( "B" );
else if ( grade >= 50 )
System.out.println( "C" );
else if ( grade >= 40 )
System.out.println( "D" );
else
System.out.println( "F" );
23
MUNYAO 10/29/2024
import java.util.Scanner;
public class Grade {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int mark = 0;
System.out.println("Enter student mark:");
mark = in.nextInt();
if ( mark >= 70 )
System.out.println( "GRADE IS : A" );
else if ( mark >= 60 )
System.out.println( "GRADE IS : B" );
else if ( mark >= 50 )
System.out.println( "GRADE IS : C" );
else if ( mark >= 40 )
System.out.println( "GRADE IS : D" );
else
System.out.println( "GRADE IS : F" );
}}
24
MUNYAO 10/29/2024
In a raffle game a player is blind folded and asked to
pick a card, upon which he/she wins a gift depending
on the colour of the card.
In particular
If the colour is yellow the gift is DVD,
If the colour is Blue the gift is Mobile Phone,
If the colour is White the gift is a Pen.
Any other colour the person looses.
Assign each colour an integer value.
25
MUNYAO 10/29/2024
Its like an IF statement.
Syntax switch case
switch(expression)
{
case label 1:
stat. to execute
break;
case label 2:
stat. to execute
break;
case label n:
stat. to execute
break;
default:
stat.
}
26
MUNYAO 10/29/2024
switch(grade)
{
case 'A':
remark="Distinction";
break;
case 'B':
remark="credit";
break;
case 'C':
remark="Pass";
break;
case 'D':
remark="Fail";
break;
default:
27
MUNYAO 10/29/2024
import java.util.Scanner;
public class SwitchGrade {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char grade ;
System.out.println("Enter THE STUDENT GRADE:");
grade = in.next().charAt(0);
switch(grade) {
case'A': System.out.println("Excellent!");
break;
case'B':
case'C': System.out.println("Well done");
break;
case'D': System.out.println("You passed");
case'F': System.out.println("Better try again");
break;
default: System.out.println("Invalid grade"); }
System.out.println("Your grade is "+ grade); }
}
28
MUNYAO 10/29/2024
29
MUNYAO 10/29/2024
Are used to execute an instruction repeatedly until a
certain condition is met.
The following must be specified
a) Starting condition
b) Condition to be evaluated i.e. termination condition
c) A way of incrementing the counter i.e. the program
should keep on incrementing the counter
30
MUNYAO 10/29/2024
a) While control structure.
b) Do – while repetition control structure.
c) For repetition control structure.
31
MUNYAO 10/29/2024
A type of control statement whereby the instruction is
executed as long as the condition is true: if the
condition becomes false you exit the loop (ring)
The while statement continually executes a block of
statements while a particular condition is TRue. Its
syntax can be expressed as:
while (expression) {
statement(s)
}
32
MUNYAO 10/29/2024
class WhileDemo {
public static void main(String[] args)
{
int count = 1;
while (count < 11)
{
System.out.println("Count is: " + count);
count++;
}
}
}
33
MUNYAO 10/29/2024
class Multiplication {
public static void main(String[] args){
int x = 1;
while (x < 11) {
int prod=x*10;
System.out.println(x+" X 10 = " + prod);
x++; }
}
}
34
MUNYAO 10/29/2024
chart
Action
35
MUNYAO 10/29/2024
This is having a while statement inside another while
statement. i.e. having two loops, one outer loop and one
inner loop.
Action
False
False True
condition
Action
False
True
condition
Action
36
MUNYAO 10/29/2024
The do while control statement, first executes the
instruction the checks whether the condition is true
The do/while structure tests the loop-continuation
condition after performing the body of the loop; therefore,
the loop body always executes at least once.
Syntax
do
{
statement(s)
}
while (expression);
37
MUNYAO 10/29/2024
38
MUNYAO 10/29/2024
class DoWhileDemo
{
public static void main(String[] args)
{
int count = 1;
do
{
System.out.println("Count is: " + count);
count++;
}
while (count <= 11);
}
}
39
MUNYAO 10/29/2024
The for statement provides a way to iterate over a range
of values.
The Syntax for the For Loop:
for( initialization; termination; increment)
{
statements;
}
NB: two semicolons are compulsory which separates
Initialization, Condition and Incrementation.
40
MUNYAO 10/29/2024
Initialization: It allows the variable to be initialize. Such as:
int i = 1;
int j = 1;
Termination (or condition): It allows to check the certain
condition. If condition is true then all statements and
processes written in the for block will be executed otherwise
ignored. Condition such as:
i <= 5;
j <= i;
Increment: It allows the how much increase the given
variable. Such as:
i++;
j++;
41
MUNYAO 10/29/2024
public class DisplayNumbers{
public static void main(String[] args){
int x = 0;
for(int i = 1;i <= 10;i++)
{
System.out.println("COUNT IS "+ i );
}
}
}
42
MUNYAO 10/29/2024
public class Multiplication{
public static void main(String[] args){
int x = 5, prod=0;
for(int i = 1;i <= 10;i++)
{
prod = x*i;
System.out.println("5 x "+ i +" = "+prod);
}
}
}
43
MUNYAO 10/29/2024
initialize
test
Body of loop Increment
44
MUNYAO 10/29/2024
import java.util.Scanner;
public class Kplc {
public static void main(String[] args) {
String name;
int bill,units,tbill, charge=120;
Scanner in = new Scanner(System.in);
System.out.println("Enter Client Name:");
name = in.next();
System.out.println("Enter Units Consumed:");
units = in.nextInt();
if( units <= 100) { bill=150; }
else if( units <= 300) { bill=200; }
else { bill=250; }
tbill=charge+bill;
System.out.println("---------------------------------------");
System.out.println(" KPLC CUSTOMER BILL ");
System.out.println("THE CLIENTS NAME IS : "+ name);
System.out.println("Standing Charges : "+ charge);
System.out.println("Other Charges : "+ bill);
System.out.println("TOTAL BILL : "+ tbill);
System.out.println("THANK YOU : "+ name+" FOR BEING OUR CUSTOMER");
System.out.println("---------------------------------------");
45
MUNYAO } 10/29/2024
Print out multiples of seven between 1 and 100
Print out patterns
x
xx
xxx
46
MUNYAO 10/29/2024
public class Main { public static void main(String[] args)
{ int rows = 5;
for (int i = 1; i <= rows; ++i)
{ for (int j = 1; j <= i; ++j)
{ System.out.print("* "); }
System.out.println(); }
}}
47
MUNYAO 10/29/2024
public class PyramidPattern
{ public static void main(String args[])
{
int i, j, row = 6;
for (i=0; i<row; i++)
{
for (j=row-i; j>1; j--)
{ System.out.print(" "); }
for (j=0; j<=i; j++ )
{
System.out.print("* ");
}
System.out.println();
}
}
MUNYAO
} 10/29/2024
48
public class JavaProgram {
public static void main(String args[]) {
int i, j, k=8; for(i=0; i<5; i++) {
for(j=0; j<k; j++)
{ System.out.print(" "); }
k = k - 2; for(j=0; j<=i; j++)
{ System.out.print("* "); }
System.out.println();
}
}}
49
MUNYAO 10/29/2024
public class halfPyramidUpper {
public static void main(String[] args) {
int rows = 5; for (int i = rows; i > 0; i--)
{ for (int s = 1; s < i; s++) {
System.out.print(" "); }
for (int j = rows; j >= i; j--)
{ System.out.print("*"); }
System.out.println(); }
}}
50
MUNYAO 10/29/2024
public class halfPyramidNumber
{ public static void main(String[] args)
{ int rows = 5; for (int i = 1; i <= rows; ++i)
{ for (int j = 1; j <= i; ++j)
{ System.out.print(j + " "); }
System.out.println(); }
}}
51
MUNYAO 10/29/2024
public class halfPyramidAlphabets {
public static void main(String[] args) {
char last = 'F', alphabet = 'A';
for (int i = 1; i <= (last - 'A' + 1); ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print(alphabet + " "); }
++alphabet; System.out.println(); }
}}
52
MUNYAO 10/29/2024
public class pyramid {
public static void main(String[] args) {
int rows = 5; for (int i = 1; i <= rows; i++) {
for (int s = rows-i; s > 0; s--) {
System.out.print(" "); }
for (int j = 1; j < i*2; j++) {
System.out.print("*"); }
System.out.println(); }
}}
53
MUNYAO 10/29/2024
public class IfInsideFor{
public static void main(String[] args){
for(int i = 1;i <= 30;i++)
{ if (i%2== 0)
{
System.out.println( i+" IS EVEN NUMBER."); }
else { System.out.println(i+ " IS AN ODD NUMBER."); }
}
}
}
54
MUNYAO 10/29/2024
public class IfInsideFor{
public static void main(String[] args){
for(int i = 1;i <= 70;i++) {
if (i%7== 0) {
System.out.println( i+" is divisible by 7."); }
else { }
}
}}
55
MUNYAO 10/29/2024
public class DoWhileIf {
public static void main(String[] args) {
int i = 1;
do {
if (i%2== 0) {
System.out.println( i+" IS EVEN NUMBER."); }
else { System.out.println(i+ " IS AN ODD NUMBER."); }
i++;}
while (i <= 11);
}
}
56
MUNYAO 10/29/2024
public class DoWhileIf {
public static void main(String[] args) {
int i = 1;
do
{
if (i%7== 0)
{
System.out.println( i+" DIVISIBLE BY 7");
}
i++;
}
while (i <= 70);
}
}
57
MUNYAO 10/29/2024
END
INTRODUCTION TO CLASSES
58
MUNYAO 10/29/2024