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

Java Control Structures - Conditional Branches

The document provides an overview of Java control structures, specifically focusing on conditional branches such as if statements, if-else statements, if-else-if statements, and switch statements. It includes syntax and examples for each type of statement, demonstrating how they can be used to control the flow of a program based on conditions. Additionally, various sample programs illustrate practical applications of these control structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Control Structures - Conditional Branches

The document provides an overview of Java control structures, specifically focusing on conditional branches such as if statements, if-else statements, if-else-if statements, and switch statements. It includes syntax and examples for each type of statement, demonstrating how they can be used to control the flow of a program based on conditions. Additionally, various sample programs illustrate practical applications of these control structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Programming 2

Module 3: Java Control Structures – Conditional Branches

Conditional Control Structures allows the program to select between the alternatives during the program execution.

They are also called as decision-making statements or selection statements.

Conditional Control Statements


 if statement
 if - else statement
 if - else - if statement
 switch statement

If Statement

It will go inside the block only if the condition is true otherwise, it will not execute the block.

Syntax:

if (condition) {
//statements (if block)
}
//other statements - executed irrespective of the condition

Example:

int number = 23;


if(number > 20) {
System.out.println("Number is greater than 20.");
}
If - Else Statement

If the condition is true then, it will execute the if block. Otherwise, it will execute the else block.

Syntax:

if (condition) {
statements (if block)
} else {
statements (else block)
}
Example:

int number = 15;


if(number > 20) {
System.out.println("Number is greater than 20.");
} else {
System.out.println("Number is less than 20.");
}

If - Else - If Statement

If the condition is true, then it will execute the if block. Otherwise, it will execute the else-if block. Again, if the condition
is not met, then it will move to the else block.

Syntax:

if (condition 1) {
statements (if block)
} else if (condition 2) {
statements (else - if block)
} else {
statements (else block)
}
Example:

int number = 2;
int anotherNumber = 3;
int theOtherNumber = 4;

if(number > anotherNumber){


System.out.println("Output.");
} else if(number > theOtherNumber) {
System.out.println("Another output.");
} else {
System.out.println("The other output.");
}

Switch Statement

Switch statement allows program to select one action among multiple actions during the program execution.

Syntax:

switch (variable or value or expression) {


case :
statements;
break;
case :
statements;
break;

default:
statements;
break;
}
Based on the argument in the switch statement suitable case value will be selected and executed.
If no matching case found, then the default will be executed.
It is optional to write a break statement at the end of each case statement.

Example:

int option = 2;
switch(option){
case 1:
System.out.println("Output.");
break;
case 2:
System.out.println("Another output.");
break;
default:
System.out.println("The other output.");
break;
}
Other program examples

Program #1

import java.util.Scanner;
public class PositiveOrNegative {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int inputNumber;
System.out.println("Positive or negative number");
System.out.print("Enter a number: ");
inputNumber = scanner.nextInt();
if(inputNumber >= 0) {
System.out.println("The input number is positive.");
} else {
System.out.println("The input number is negative.");
}
}
}
Program #2

import java.util.Scanner;
public class PositiveOrNegative {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int inputNumber;
System.out.println("Positive or negative number");
System.out.print("Enter a number: ");
inputNumber = scanner.nextInt();
if(inputNumber == 0) {
System.out.println("The input number is not positive or negative.");
} else if(inputNumber > 0){
System.out.println("The input number is positive.");
} else {
System.out.println("The input number is negative.");
}
}
}
Program #3

import java.util.Scanner;
public class ColorEquivalent {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int inputNumber;
System.out.println("Color Equivalent");
System.out.print("Enter a number: ");
inputNumber = scanner.nextInt();
switch(inputNumber) {
case 1: case 2: case 3:
System.out.println("The equivalent color is red.");
break;
case 4: case 5: case 6:
System.out.println("The equivalent color is yellow.");
break;
case 7: case 8: case 9:
System.out.println("The equivalent color is blue.");
break;
default:
System.out.println("Input error, there is no equivalent color.");
break;
}
}
}
Program #4

import java.util.Scanner;
public class InchesToCentimeters {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
final double CENTIMETERS_PER_INCH = 2.54;
double inches;
double centimeters;

System.out.println("Inches to Centimeters");
try {
System.out.print("Enter value in inches: ");
inches = scanner.nextDouble();
if(inches <= 0) {
System.out.println("Zero or negative input is not allowed.");
} else {
centimeters = inches * CENTIMETERS_PER_INCH;
System.out.println("Output is " + String.format("%,.2f" ,centimeters) + ".");
}
} catch(Exception e) {
System.out.println("Invalid input, letters or special symbols in inches are not allowed.");
}
}
}
Program #5

import java.util.*;
public class AreaOfTheRectangle {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
double length;
double width;
double area;

System.out.println("Area of the Rectangle");


try {
System.out.print("Enter length: ");
length = console.nextDouble();
if(length <= 0) {
System.out.print("Input error, zero or negative length is not allowed.");
} else {
try {
System.out.print("Enter width: ");
width = console.nextDouble();
if(width <= 0) {
System.out.print("Input error, zero or negative width is not allowed.");
} else {
area = length * width;
System.out.println("The area is " + String.format("%,.2f", area) + ".");
}
} catch(Exception e) {
System.out.print("Invalid input, letters or special symbols in width are not allowed.");
}
}
} catch(Exception e) {
System.out.print("Invalid input, letters or special symbols in length are not allowed.");
}
}
}
Program #6

import java.util.*;
public class AreaOfTheRectangle {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
double length;
double width;
double area;

System.out.println("Area of the Rectangle");


System.out.print("Enter length: ");
if((console.hasNextDouble()) == true) {
length = console.nextDouble();
if(length <= 0) {
System.out.print("Input error, zero or negative length is not allowed.");
} else {
System.out.print("Enter width: ");
if((console.hasNextDouble()) == true) {
width = console.nextDouble();
if(width <= 0) {
System.out.print("Input error, zero or negative width is not allowed.");
} else {
area = length * width;
System.out.println("The area is " + String.format("%,.2f", area) + ".");
}
} else {
System.out.print("Invalid input, letters or special symbols in width are not allowed.");
}
}
} else {
System.out.print("Invalid input, letters or special symbols in length are not allowed.");
}
}
}
Program #7

import java.util.Scanner;
public class ColorEquivalent {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int inputNumber;

System.out.println("Color Game");
try {
System.out.print("Enter a number: ");
inputNumber = scanner.nextInt();
switch(inputNumber) {
case 1: case 2:
System.out.println("The equivalent color is red.");
break;
case 3: case 4:
System.out.println("The equivalent color is yellow.");
break;
case 5: case 6:
System.out.println("The equivalent color is blue.");
break;
default:
System.out.println("Input error, please input from 1 to 6 only.");
break;
}
} catch(Exception e) {
System.out.println("Invalid input, letters or special symbols in input number are not allowed.");
}
}
}
Quick review

Control structures alter the sequential flow of control.

Selection structures incorporate decisions in a program.

Including a semicolon before the statement in a one-way selection creates a semantic error. In this case, the action of the if
statement is empty.

There is no stand-alone else statement in Java programming language. Every else has a related if.

An else is paired with the most recent if that has not been paired with any other else.

A sequence of statements enclosed between braces, { and }, is called a compound statement or block of statements. A
compound statement is treated as a single statement.

A switch structure is used to handle multiple selections.

The expression in a switch statement must evaluate to an integral value.

A switch statement executes according to the following rules:

When the value of the expression is matched against a case value, the statements execute until either a break
statement is found or the end of the switch structure is reached.

If the value of the expression does not matched any of the case values, the statements following the default
label execute. If the switch structure has no default label, and if the value of the expression does not match any of the
case values, the entire switch statement is skipped.

A break statement causes an immediate exit from the switch structure.


References:

Java programming: From program analysis to program design by: D. S. Malik


https://www.w3schools.com
https://www.javatpoint.com

You might also like