0% found this document useful (0 votes)
78 views5 pages

3 Conditional Statements

This document discusses Java programming fundamentals related to selection and conditional statements. It covers relational and logical operators used in conditional statements like if/else and nested if statements. It also covers switch statements which provide an alternative to sequential program execution based on different cases. Sample code is provided to illustrate if/else, nested if, and switch statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views5 pages

3 Conditional Statements

This document discusses Java programming fundamentals related to selection and conditional statements. It covers relational and logical operators used in conditional statements like if/else and nested if statements. It also covers switch statements which provide an alternative to sequential program execution based on different cases. Sample code is provided to illustrate if/else, nested if, and switch statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA PROGRAMMING FUNDAMENTALS

SELECTION / CONDITIONAL STATEMENTS

A computer can process a program in one of three ways:


- In sequence
- By making a selection or a choice, which is also called a branch
- By repetition, executing a statement over and over using a structure called a loop

Control structure provide alternatives to sequential program execution and are used to alter the flow
of execution. The two most common control structures are selection repetition. In selection, the
program executes particular statements depending on one or more conditions. In repetition, the
program repeats particular statements a certain number of times depending on one or more
conditions.
SELECTION (CONDITIONAL STATEMENTS)

Relational Operators in Java

OPERATOR DESCRIPTION
== equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to

Logical Operators

OPERATOR DESCRIPTION
! not
&& and
|| or

if else conditional statements

Sample Code:

import java.util.Scanner;
public class ConditionalStatements_ifElse {
static Scanner console = new Scanner (System.in);

public static void main(String[] args) {

double wages, rate, hours;

System.out.println("Enter hours:");
hours = console.nextDouble();
System.out.println();

System.out.println("Enter rate:");
rate = console.nextDouble();
System.out.println();

if (hours> 40.0){
wages = 40.0 * rate + 1.5 * rate * (hours - 40.0);
}
else {
wages =hours * rate;
}

System.out.println("The total wages are"+" "+wages);


}

Output:
Enter hours:
2
Enter rate:
200
The total wages are 400.0

Nested if conditional statements

Sample Code:

import java.util.Scanner;
public class ConditionalStatements_Nested {
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {

double balance, interestRate;

System.out.println("Enter balance:");
balance = console.nextDouble();
System.out.println();

if (balance>= 50000.00){
interestRate = 0.05;
}
else if (balance>= 25000.00){
interestRate = 0.04;
}
else if (balance>= 1000.00){
interestRate = 0.03;
}
else {
interestRate = 0.00;
}
System.out.println("The interest rate is:"+" "+
interestRate);
}

Output:

Enter balance:
20000

The interest rate is: 0.03


SWITCH STRUCTURES

A selection structure, which does not require the evaluation of a logical expression.
Java’s switch structure gives the computer the power to choose from many alternatives

A switch statement executes according to the following rules:


1) When the value of the expression is matched against a case value (also called a label), the
statements execute until either a break statement is found or the end of the switch structure is
reached.
2) If the value of the expression does not match 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.
3) A break statement causes an immediate exit from the switch structure.

THE FOLLOWING RULES APPLY TO A SWITCH STATEMENT:


 You can have any number of case statements within a switch. Each case is followed by the
value to be compared to and a colon.
 The value for a case must be the same data type as the variable in the switch and it must
be a constant or a literal.
 When the variable being switched on is equal to a case, the statements following that case
will execute until a break statement is reached.
 When a break statement is reached, the switch terminates, and the flow of control jumps to the
next line following the switch statement.
 Not every case needs to contain a break. If no break appears, the flow of control will fall
through to subsequent cases until a break is reached.
 A switch statement can have an optional default case, which must appear at the end of the
switch. The default case can be used for performing a task when none of the cases is true. No
break is needed in the default case.

Sample Code:

import java.util.Scanner;
public class ConditionalStatements_Switch {
static Scanner console = new Scanner (System.in);
public static void main (String[] args){

int num;

System.out.print("Enter an integer between 0 and 3:");


num=console.nextInt();
System.out.println();
System.out.print("The number you entered is"+" "+num);
System.out.println();

switch (num)
{
case 0:
System.out.println("Hello");

case 1:
System.out.println("Hi");

case 2:
System.out.println("Good Day");
break;
case 3:
System.out.println("How are you?");
break;
default:
System.out.print("Sorry the number is out of range");
break;
}
}
}

Output:

Enter an integer between 0 and 3: 4

The number you entered is 4


Sorry the number is out of range
Sample Code:

import java.util.Scanner;
public class ConditionalStatements_Switch2 {

static Scanner console = new Scanner (System.in);


public static void main (String[] args){

char letter;

System.out.print("Choose letter between A and C:");


letter=console.next().charAt(0);
System.out.println();

System.out.println("The letter you entered is"+" "+letter);


System.out.println();

switch (letter)
{
case 'A':
System.out.println("Hello");
break;
case 'B':
System.out.println("Hi");
break;
case 'C':
System.out.println("Good Day");
break;
default:
System.out.print("Sorry the letter is out of range");
break;
}
System.out.println("Out of switch structure");
}
}

Output:

Choose letter between A and C:B

The letter you entered is B

Hi
Out of switch structure

You might also like