Computer Science  ·  Level 6
Fundamentals Of Programming
Chapter 4: Control Structures
📚 3 Topics
⚑ What you will be able to do

By the end of this chapter, you will be able to:

  • correctly implement control structures in your programs
  • choose and use different control statements for various programming tasks
  • create programs that effectively use control statements to control the flow of execution
  • write clear and logical code that responds to different conditions and repeats actions as needed

Mastering these skills helps you build programs that work smoothly and solve real-world problems efficiently in any programming job.

4.1 Java Control Statements

Java control statements determine the order in which individual statements, instructions, or function calls are executed or evaluated. In professional settings such as Nairobi’s tech firms or county ICT departments, the ability to manipulate control flow is critical for developing software that responds dynamically to user input or system states.

4.1.1 Decision Making Statements

Decision making statements in Java allow programs to execute different code blocks based on certain conditions. These statements introduce conditional logic, enabling software to behave differently depending on input or environmental factors, a capability vital for applications like hospital management systems or mobile banking apps.

If Statement

The if statement is the simplest decision making construct, executing a block of code only when a specified Boolean condition evaluates to true. In practice, at a county government payroll system, the if statement can be used to verify if an employee’s payment exceeds a tax threshold before applying deductions.

If-Else Statement

The if-else statement extends the if construct by providing an alternative code block to execute when the condition is false. For example, a university admission portal might use if-else to determine whether a candidate’s exam score qualifies them for a particular course, directing them accordingly.

Else-If Ladder

The else-if ladder handles multiple mutually exclusive conditions by chaining several if-else statements. In a retail inventory system, this structure can categorize stock levels as “Low”, “Medium”, or “High” to trigger different restocking actions.

Switch Statement

The switch statement offers a cleaner alternative to multiple else-if statements when evaluating a single variable against constant values. A mobile money application could use switch to process different transaction types such as deposit, withdrawal, or balance inquiry, each handled in distinct case blocks.

4.1.2 Looping Statements

Looping statements allow repeated execution of code blocks, which is essential in scenarios like processing batches of data or continuously monitoring sensor inputs in smart agriculture applications.

For Loop

The for loop is used when the number of iterations is known beforehand. For instance, a financial report generator in a SACCO system might use a for loop to iterate through a fixed list of members’ account balances to calculate interest.

While Loop

The while loop executes a block of code as long as a specified condition remains true, making it suitable for situations where the number of iterations depends on dynamic conditions. A weather monitoring app could use a while loop to continuously check sensor readings until a safe threshold is reached.

Do-While Loop

The do-while loop guarantees that the code inside the loop executes at least once before the condition is evaluated. This is useful in user input validation processes, such as in an online registration system, where the program prompts for data entry and verifies it after the first input.

Nested Loops

Nested loops involve placing one loop inside another, allowing traversal of multi-dimensional data structures such as matrices. In image processing software used in Kenyan hospitals, nested loops can process pixel data row by row and column by column.

4.1.3 Branching Statements

Branching statements alter the flow of control by transferring execution to another part of the program, crucial for breaking out of loops or skipping sections of code based on runtime conditions.

Break Statement

The break statement terminates the nearest enclosing loop or switch statement prematurely. In a hotel booking system, break can be used to exit a search loop once a matching room is found, improving performance.

Continue Statement

The continue statement skips the current iteration of a loop and proceeds to the next iteration. For example, in a payroll system, continue might skip processing employees who are on unpaid leave during salary computation.

Return Statement

The return statement exits from the current method and optionally returns a value. In a banking application, return is used to provide the result of a transaction processing method back to the caller, such as confirming success or failure.

Labeled Break and Continue

Java supports labeled break and continue statements to control flow in nested loops by specifying which loop to break out of or continue. This feature is useful in complex data processing tasks, such as scanning multi-level financial records in an audit application.

Practice Questions

  1. Explain the difference between the if-else statement and the switch statement in Java, providing examples relevant to Kenyan software applications. (10 marks)

  2. Describe a scenario in a county government system where a do-while loop would be more appropriate than a while loop. Justify your answer. (8 marks)

  3. Write a Java code snippet using nested loops to process a two-dimensional array representing seating arrangements in a conference hall. (12 marks)

  4. How do the break and continue statements affect loop execution? Illustrate with examples from a hospital patient management system. (10 marks)

The rest of this chapter
🔒

Create a free account to open more of this chapter.

Free: practical guides, quick cards, workplace scenarios and more.

Create a free account
🔒4.2 Uses of different control statements in Java

In the development of software applications within Kenyan institutions such as universities, banks, and government agencies, control structures in Java play a crucial role in directing the flow of program execution. Control statements enable programmers to imp…

🔒4.3 Creation of programs using control statements

In the Kenyan software development industry, mastery of control statements is fundamental for creating efficient and effective programs. Control statements direct the flow of execution, enabling applications to make decisions, repeat actions, or branch to diff…

Chapter Summary

This chapter explored the fundamental control structures in Java programming, beginning with an overview of Java control statements and their classification into decision making, looping, and branching statements. Decision making statements enable programs to execute specific blocks of code based on conditions, while looping statements facilitate repeated execution of code segments. Branching statements provide mechanisms to alter the normal flow of control within loops or switch cases. The chapter then examined the practical uses of different control statements, detailing decision making constructs such as if then, if then else, and switch statements. Looping constructs including for, while, and do while loops were explained with their syntax and typical applications. Branching statements like break and continue were discussed in terms of their role in managing loop execution. Finally, the chapter emphasized the integration of these control structures in creating functional Java programs that respond dynamically to varying conditions and inputs.

Self-Assessment

🔒 PDFDownload this self-assessment, with answers

A. Written Assessment

  1. Which Java control statement would you use to execute a block of code only if a certain condition is true? (2 marks)
  2. What is the difference between the while and do-while loops in Java? (3 marks)
🔒20 more in this section.

Chapter Examination Questions

🔒 PDFDownload these examination questions, with worked answers

SECTION A (40 Marks) - Answer ALL Questions

  1. Explain how the if-then-else statement can be used to validate user input in a banking application such as Equity Bank's mobile banking system. (4 marks)
  2. Distinguish between the for and while loops in Java, highlighting a scenario in a payroll system where each would be appropriate. (4 marks)
🔒18 more in this section.

Chapter Practical Activities

Practical 1: Develop a Java program using if then statement for student grade evaluation

Computer Science · Level 6
Fundamentals Of Programming
PRACTICAL ASSESSMENT
TIME: 4 HOURS
⬇ PDFCandidate Instructions (Candidate Tool)

Type: Individual

INSTRUCTIONS TO CANDIDATE:
1.  You are required to perform the following task:
i.  Write and run a Java program that accepts a student's score and prints 'Pass' if the score is 50 or more, otherwise prints 'Fail'.
2.  You have been provided with the following resources for the practical task:
Tools & EquipmentMaterials
Functional computer installed with Java IDE (Eclipse/NetBeans/IntelliJ)Plain paper
⬇ PDFResources Required (Cutting List)
S/NItemQuantity
1Functional computer installed with Java IDE (Eclipse/NetBeans/IntelliJ)1 Pc per Candidate
2Plain paper1 Pc per Candidate
⬇ PDFAssessor Guide
Items to be EvaluatedMarks AvailableMarks ObtainedComments
TASK 1: Develop Java program with if then statement
Opened Java IDE and created a new Java project
(Award 2 marks or zero)
2
Declared variables to store student score
(Award 3 marks or zero)
3
Implemented input mechanism to accept student score
(Award 3 marks or zero)
3
Used if then statement to check if score >= 50
(Award 5 marks or zero)
5
Printed 'Pass' when condition is true
(Award 3 marks or zero)
3
Printed 'Fail' when condition is false
(Award 3 marks or zero)
3
Compiled and ran the program without errors
(Award 4 marks or zero)
4
Sub-Total23
PRODUCT CHECKLIST
Program correctly outputs 'Pass' for scores 50 and above and 'Fail' for scores below 50
(Award 7 marks or zero)
7
Sub-Total7
GRAND TOTAL30
ASSESSMENT OUTCOME:   ☐ Competent    ☐ Not Yet Competent (competent if at least 50%)

Practical 2: Implement a Java Program Using If Then Else Statement to Determine Pass or Fail

Computer Science · Level 6
Fundamentals Of Programming
PRACTICAL ASSESSMENT
TIME: 4 HOURS
⬇ PDFCandidate Instructions (Candidate Tool)

Type: Individual

INSTRUCTIONS TO CANDIDATE:
1.  You are required to perform the following task:
i.  Write and run a Java program that uses an if then else statement to check a student's exam score and display 'Pass' if the score is 50 or more and 'Fail' if below 50.
2.  You have been provided with the following resources for the practical task:
Tools & EquipmentMaterials
Functional computer installed with Java IDE (Eclipse/NetBeans/IntelliJ)Plain paper
⬇ PDFResources Required (Cutting List)
S/NItemQuantity
1Functional computer installed with Java IDE (Eclipse/NetBeans/IntelliJ)1 Pc per Candidate
2Plain paper1 Sheet per Candidate
⬇ PDFAssessor Guide
Items to be EvaluatedMarks AvailableMarks ObtainedComments
TASK 1: Setup and Variable Declaration
Launched the Java IDE correctly
(Award 2 marks or zero)
2
Declared variable 'score' as integer
(Award 3 marks or zero)
3
Declared variable 'result' as String
(Award 2 marks or zero)
2
Sub-Total7
TASK 2: Implement If Then Else Statement
Implemented if condition to check if score >= 50
(Award 4 marks or zero)
4
Assigned 'Pass' to result if condition true
(Award 3 marks or zero)
3
Assigned 'Fail' to result if condition false
(Award 3 marks or zero)
3
Sub-Total10
TASK 3: Display Output and Run Program
Displayed the score and result using System.out.println
(Award 4 marks or zero)
4
Compiled and ran the program without errors
(Award 4 marks or zero)
4
Tested program with example inputs (e.g., 45 and 55) showing correct output
(Award 5 marks or zero)
5
Sub-Total13
PRODUCT CHECKLIST
Program uses correct if then else statement for decision making
(Award 5 marks or zero)
5
Program correctly assigns and displays 'Pass' or 'Fail' based on score
(Award 5 marks or zero)
5
Program runs successfully and produces expected output for test cases
(Award 5 marks or zero)
5
Sub-Total15
GRAND TOTAL45
ASSESSMENT OUTCOME:   ☐ Competent    ☐ Not Yet Competent (competent if at least 50%)
🔒

Free: practical guides, quick cards, workplace scenarios and more.

Create a free account
🔒Java Program Using Switch Statement for Grade EvaluationPractical 3
🔒Write and run a Java program using a for loop to iterate fixed timesPractical 4
🔒Java Program Using While Loop for Conditional IterationPractical 5
🔒Java Program Using Do While Loop for Post-Condition IterationPractical 6
🔒Java Program to Demonstrate Break Statement in LoopPractical 7
🔒Java program demonstrating continue statement to skip iterationPractical 8
🔒Java program to process multiple inputs using if-then-else inside a for loopPractical 9
🔒Develop a Java Menu-Driven Program Using Switch Inside While LoopPractical 10
🔒Implement nested loops with break and continue in JavaPractical 11
🔒Develop a Java program using break and continue in a do-while loopPractical 12
🔒Develop a Java program using multiple decision making statementsPractical 13
🔒Develop Java program demonstrating for, while, and do-while loopsPractical 14
🔒Develop a Java Program Using Control Statements to Manage Student Grade EvaluationPractical 15
Flashcards 20 cards Study deck ▾
Question
1

↻ Tap card to reveal answer
🔒

18 more in this section.

Create a free account
Test Yourself 16 questions Start quiz ▾
0%
0 / 2
🔒

14 more in this section.

Create a free account
Am I competent?

At the start of this chapter we promised you would be able to:

  • correctly implement control structures in your programs
  • choose and use different control statements for various programming tasks
  • create programs that effectively use control statements to control the flow of execution
  • write clear and logical code that responds to different conditions and repeats actions as needed

Tick each one you can genuinely do.

So, are you there yet?

You're competent when you can confidently do 50% or more of what this chapter promised.

Sign in to record how you're doing.