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

COM 413 Lesson 3

The document provides an introduction to control structures in Java, detailing three main categories: decision, looping, and jumping control structures. It explains various control statements such as if, if-else, nested if-else, switch-case, for loop, while loop, and do-while loop, along with examples and syntax. Additionally, it includes program demonstrations and assignments related to these control structures.

Uploaded by

edwinmurimi902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

COM 413 Lesson 3

The document provides an introduction to control structures in Java, detailing three main categories: decision, looping, and jumping control structures. It explains various control statements such as if, if-else, nested if-else, switch-case, for loop, while loop, and do-while loop, along with examples and syntax. Additionally, it includes program demonstrations and assignments related to these control structures.

Uploaded by

edwinmurimi902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

3.

Introduction to Control Structures/Flow in


Java

Lesson 3:
Control Structures / Statements
Directs the flow of execution in a program.
Control structures enable the programmer to achieve iteration and branching.

C language control statement are divided into three categories:

1. Decision control structures


2. Looping control structures
3. Jumping control structures
Branching: The process of taking the alternative course of action against the
other.
Looping: The process of repeating / iterating a block of statements any number
of times.
Jumping: Moving to a specified location / point in a program
Decision Control Structures

Facilitate the action of branching

Examples of structures / statements;

i. if statement
ii. if ... else statement
iii. nested if … else statement
iv. switch case statement
Looping Control structures
Facilitate the action of repetition / iteration

Examples:

i. for loop
ii. do … while loop
iii. while loop
Jumping Control Structures

Facilitate the action of jumping to some other point in the program:

Examples:

i. break statement
ii. continue statement
If control structure
Control structure that considers only the true part of the condition;
Syntax:
Example 1;
if(condition) T
Statement;
Example 2
if(condition)
{
Statement;
Statement;
Statement;
}
Cont’d …
Program example (if control statement)

a. Write a program to check if any input number is a an even number.

b. Write a program to check if any input number is less than zero.

c. Nb: also draw a flowchart of the same


if … else
Syntax:

if(condition)
{
Statement; TP
Statement;
}
else
{
statement; FP
Statement;
}
Cont’d …
Program demo (if .. else)

a. Program to check if any input number is either odd or even number


b. Program to check if any input number is divisible by 5

Nb: also draw a flowchart for the same


Nested if … else
if(cond)

statement

else if(cond)

statement

else if(cond)

statement;

else

statement;
Cont’d …
Program demo for nested if … else
Write a program that reads the name and the marks scored by a
student, and to display the corresponding grade:
Use the given grading system:

< 0 marks Invalid marks


Between 0 and 39 grade = E, Fail
Between 40 and 49 Grade = D
Between 50 and 59 Grade = C
Between 60 and 69 Grade = B
Between 70 and 100 Grade = A
> 100 marks beyond the range
statement;
Switch case control structure switch(expression)
{
case 10:
Simplified version of
Statement;
nested if… else break;
case 2:
control structure Statement;
break;
X+y= value case 133:
Statement;
break;
default:
Statement;
break;
}
Statement;
Switch case control
structure
switch case: Explanation

An expression must evaluate to a value. The value of the expression is


compared upon several cases in the switch case control structure.
The case that matches the value of an expression, is executed.

The break statement in each of the cases transfers the control of execution
outside the switch control structure.
The default option handles any mismatch in the control structure.
Program demo for switch case:
Write a menu driven program to preform the following:
Compute the perimeter of a rectangle
Compute the sum of two numbers

Ie: *131#
Main menu
1. perimeter
2. sum
Enter your choice
Looping control structures

1. for Loop control structure

2. do… while loop control structure

3. while loop control structure


for loop control structure

Used to repeat a section of code a certain number of times known in advance

Syntax:

for(initialization; test expression; inc/dec)


Why initialization?
Example:
Provide initial value.
for(i = 0; i<5; i++) Eliminate Garbage values

for(i=5; i>0; i--) Done once


T
Demo programs using for loop

1. Program to display numbers 1 to 5

2. Program to check if any input number is a prime number

3. Program to display Fibonacci series to nth term


do ….. while loop control structure

1. It is an exit control structure.

2. The condition is checked after executing


the block of code at least once.

F
do… while loop (syntax)

Statement;
do
{
Statement;
Statement;
statement;
}
while(cond);
Statement;
Demo programs using do… while loop

1. Program to display numbers 1 to 5.

2. Program to find the sum of even numbers between 1 and 10.

3. Program to check if any input number is a palindrome number


While loop control structure

It is an entry control loop F

COND
The condition is checked before
executing the block of code. T

Syntax:
while(cond)
BLOCK
{
statement;
statement;
}
Demo programs using while loop

1. Program to display numbers 1 to 5

2. Program to compute the sum of odd numbers between 1 and 5

3. Program to check if any input number is an Armstrong number


Jumping Control structures

1. break control structure / statement

2. continue control structure / statement


break control structure / statement
Transfers control of execution outside the switch control structure
Transfers control of execution from a deeply nested loop.
Transfers control of execution in the forward direction.
Syntax: break;

T
Cond break

Normal F
Loop
//break demo program
public BreakDemo() {
public class BreakDemo {
void checkNumber() { }
for(int i=0;i<10;i++)
public static void main(String[] args) {
{
if(i==8) BreakDemo bd = new
break; BreakDemo();
System.out.println(" " +i);
} bd.checkNumber();
} }

}
Continue control structure

1. Transfers control to the beginning


of the loop or program

2. Transfers control in the backward


direction

3. By passes the remainder of the


loop.
import java.util.Scanner; if(number < 0) {
public class ContinueDemo2 { System.out.println("negative
number");
void computePositive() {
continue;
Scanner sc = new } sum = sum + number;
Scanner(System.in);
}
int number; System.out.println("Sum of positive
int sum=0; integer numbers = " +sum);
for(int i=0; i<5;i++) }
public ContinueDemo2() {}
{
public static void main(String[] args) {
System.out.println("Enter the next
ContinueDemo2 cd = new
no");
ContinueDemo2();
number = sc.nextInt(); cd.computePositive();
Sample programs.
1. Prime.java //to check if any input no is prime or not
2. Prime1.java // prime numbers between range
3. SumOfDigits.java //sum of digits of any input integer number
4. ArmstrongWhile.java //Armstrong numbers between range
5. Factorial program
6. Compound interest program
7. Display mathematical table program
8. Display pyramid program
9. Java program to check if number is palindrome number
Assignment:

• Write short notes on foreach loop control structure. Provide


programming examples to illustrate the concept
• Write short notes on operator precedence in Java.

You might also like