New if loop in java

Java If . Else

You already know that Java supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

Java has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the if statement to specify a block of Java code to be executed if a condition is true .

Syntax

if (condition) < // block of code to be executed if the condition is true > 

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

In the example below, we test two values to find out if 20 is greater than 18. If the condition is true , print some text:

Example

We can also test variables:

Example

int x = 20; int y = 18; if (x > y)

Example explained

In the example above we use two variables, x and y, to test whether x is greater than y (using the > operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen that «x is greater than y».

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false .

Syntax

if (condition) < // block of code to be executed if the condition is true > else < // block of code to be executed if the condition is false > 

Example

int time = 20; if (time < 18) < System.out.println("Good day."); >else < System.out.println("Good evening."); >// Outputs "Good evening." 

Example explained

In the example above, time (20) is greater than 18, so the condition is false . Because of this, we move on to the else condition and print to the screen «Good evening». If the time was less than 18, the program would print «Good day».

The else if Statement

Use the else if statement to specify a new condition if the first condition is false .

Syntax

if (condition1) < // block of code to be executed if condition1 is true > else if (condition2) < // block of code to be executed if the condition1 is false and condition2 is true > else < // block of code to be executed if the condition1 is false and condition2 is false > 

Example

int time = 22; if (time < 10) < System.out.println("Good morning."); >else if (time < 18) < System.out.println("Good day."); >else < System.out.println("Good evening."); >// Outputs "Good evening." 

Example explained

In the example above, time (22) is greater than 10, so the first condition is false . The next condition, in the else if statement, is also false , so we move on to the else condition since condition1 and condition2 is both false — and print to the screen «Good evening».

However, if the time was 14, our program would print «Good day.»

Источник

New if loop in java

  • Introduction to Java
  • The complete History of Java Programming Language
  • C++ vs Java vs Python
  • How to Download and Install Java for 64 bit machine?
  • Setting up the environment in Java
  • How to Download and Install Eclipse on Windows?
  • JDK in Java
  • How JVM Works – JVM Architecture?
  • Differences between JDK, JRE and JVM
  • Just In Time Compiler
  • Difference between JIT and JVM in Java
  • Difference between Byte Code and Machine Code
  • How is Java platform independent?
  • Decision Making in Java (if, if-else, switch, break, continue, jump)
  • Java if statement with Examples
  • Java if-else
  • Java if-else-if ladder with Examples
  • Loops in Java
  • For Loop in Java
  • Java while loop with Examples
  • Java do-while loop with Examples
  • For-each loop in Java
  • Continue Statement in Java
  • Break statement in Java
  • Usage of Break keyword in Java
  • return keyword in Java
  • Object Oriented Programming (OOPs) Concept in Java
  • Why Java is not a purely Object-Oriented Language?
  • Classes and Objects in Java
  • Naming Conventions in Java
  • Java Methods
  • Access Modifiers in Java
  • Java Constructors
  • Four Main Object Oriented Programming Concepts of Java
  • Inheritance in Java
  • Abstraction in Java
  • Encapsulation in Java
  • Polymorphism in Java
  • Interfaces in Java
  • ‘this’ reference in Java

Источник

Loops in Java

Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Java provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.

java provides Three types of Conditional statements this second type is loop statement .

  • while loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

Java

while loop

  • Flowchart:
    • While loop starts with the checking of Boolean condition. If it evaluated to true, then the loop body statements are executed otherwise first statement following the loop is executed. For this reason it is also called Entry control loop
    • Once the condition is evaluated to true, the statements in the loop body are executed. Normally the statements contain an update value for the variable being processed for the next iteration.
    • When the condition becomes false, the loop terminates which marks the end of its life cycle.
  • for loop: for loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping.

for (initialization condition; testing condition;increment/decrement)

Java

  • Flowchart:
    • Initialization condition: Here, we initialize the variable in use. It marks the start of a for loop. An already declared variable can be used or a variable can be declared, local to loop only.
    • Testing Condition: It is used for testing the exit condition for a loop. It must return a boolean value. It is also an Entry Control Loop as the condition is checked prior to the execution of the loop statements.
    • Statement execution: Once the condition is evaluated to true, the statements in the loop body are executed.
    • Increment/ Decrement: It is used for updating the variable for next iteration.
    • Loop termination:When the condition becomes false, the loop terminates marking the end of its life cycle.
  • do while: do while loop is similar to while loop with only difference that it checks for condition after executing the statements, and therefore is an example of Exit Control Loop.

Java

  • Flowchart:
    • do while loop starts with the execution of the statement(s). There is no checking of any condition for the first time.
    • After the execution of the statements, and update of the variable value, the condition is checked for true or false value. If it is evaluated to true, next iteration of loop starts.
    • When the condition becomes false, the loop terminates which marks the end of its life cycle.
    • It is important to note that the do-while loop will execute its statements atleast once before any condition is checked, and therefore is an example of exit control loop.

Pitfalls of Loops

  • Infinite loop: One of the most common mistakes while implementing any sort of looping is that it may not ever exit, that is the loop runs for infinite time. This happens when the condition fails for some reason. Examples:
  • Infinite for loop :

Java

infinite while loop:

Java

Java

Another pitfall is that you might be adding something into you collection object through loop and you can run out of memory. If you try and execute the below program, after some time, out of memory exception will be thrown.

Java

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) at java.util.Arrays.copyOf(Unknown Source) at java.util.ArrayList.grow(Unknown Source) at java.util.ArrayList.ensureCapacityInternal(Unknown Source) at java.util.ArrayList.add(Unknown Source) at article.Integer1.main(Integer1.java:9)

Nested Loop:

Nested loop means a loop statement inside another loop statement.

There are different combinations of loop using for loop, while loop, do-while loop.

Источник

Читайте также:  Javascript массив очистить массив
Оцените статью