JAVA

Decision Making

Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Following is the general form of a typical decision making structure found in most of the programming languages:

Java programming language provides following types of decision making statements.

Statement Description
if statement An if statement consists of a boolean expression followed by one or more statements.
if...else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
nested if statements You can use one if or else if statement inside another if or else if statement(s).
switch statement switch statement allows a variable to be tested for equality against a list of values.

The programming you're doing now is sequential programming, meaning the code is executed from top to bottom. It's very linear, in that each and every line of code will be read, starting with the first line of code you write and ending at the last line.

But you don't always want your programs to work like that. Often, you want code to be executed only if certain conditions are met. For example, you might want one message to display if a user is below the age of 18 and a different message if he or she is 18 or older. You want to control the flow of the program for yourself. You can do this with conditional logic.

Conditional logic is mainly about the if word: if user is logged in than display Wellcome message; ifuser is not-logged then display login form.
Fortunately, it's very easy to use conditional logic in Java.

Let's start with if Statements.




Subscribe us on Youtube

Share This Page on


Ask Question