The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java.
Java if Statement
The Java if statement tests the condition. It executes the if block if condition is true.
Syntax:
Example:
Output:
Java if-else Statement
The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.
Syntax:
Example:
Output:
Leap Year Example:
A year is leap, if it is divisible by 4 and 400. But, not by 100.
Output:
Using Ternary Operator
We can also use ternary operator (? :) to perform the task of if..else statement. It is a shorthand way to check the condition. If the condition is true, the result of ? is returned. But, if the condition is false, the result of : is returned.
Example:
Output:
Java if-else-if ladder Statement
The if-else-if ladder statement executes one condition from multiple statements.
Syntax:
Example:
Output:
Program to check POSITIVE, NEGATIVE or ZERO:
Output:
Java Nested if statement
The nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.
Syntax:
Example:
Output:
Example 2:
Output:
Next TopicJava Switch Statement
A forstatement in Java creates loops in which a counter variable is automatically maintained. The for statement lets you set an initial value for the counter variable, the amount to be added to the counter variable on each execution of the loop, and the condition that’s evaluated to determine when the loop should end.
A for statement follows this basic format:
The three expressions in the parentheses following the keyword for control how the for loop works:
Here’s a simple for loop that displays the numbers 1 to 10 on the console:
Run this code, and you’ll see the following on the console:
If you declare the counter variable in the initialization statement (as in the previous example), the scope of the counter variable is limited to the for statement itself. Thus, you can use the variable in the other expressions that appear within the parentheses and in the body of the loop, but you can’t use it outside the loop,
If you want, you can declare the counter variable outside the for loop. Then, you can use the counter variable after the loop finishes. For example:
You can exit out of the middle of a loop by using abreak statement. You can also use a continue statement to skip an execution of the loop.
There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages −
Java programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Java supports the following control statements. Click the following links to check their detail.
Enhanced for loop in Java
As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements including arrays.
Syntax
Following is the syntax of enhanced for loop −
ExampleLive Demo
This will produce the following result −
OutputWhat is Next?
In the following chapter, we will be learning about decision making statements in Java programming.
In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in java.
Java For Loop vs While Loop vs Do While Loop
The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
There are three types of for loops in java.
Java Simple For Loop
A simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value. It consists of four parts:
For Loop Example Java 8
Syntax:
Jan 16, 2013 Phoenix v.2012.50.000.49146 - is an utility for phone settings adjustment, flashing and testing tool for Nokia CDMA, GSM, TDMA, WCDMA mobile phones. Phoenix Phoenix Service Software. Download phoenix service software latest version. Phoenix Service Software allows you to flash several Nokia phones without having any Box. Phoenix service software is a free and amazing tool for flashing Nokia phones using a USB data cable. Phoenix software free download. Aug 23, 2017 Nokia Phoenix Service Software 2018 full cracked free download for PC Windows XP, Vista, 7, 8, 8.1, and Windows 10. It is a best flashing tool for all Nokia phones by using USB data cable. This mobile tool has a very simple and easy method of flashing.
Flowchart:
Example:
Output:
Java Nested For LoopFor Loop Java Example Program
If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes.
Example:
Output:
Pyramid Example 1:
Output:
Pyramid Example 2:
Output:
Java for-each Loop
The for-each loop is used to traverse array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.
It works on elements basis not index. It returns element one by one in the defined variable.
Syntax:
Example:
Output:
Java Labeled For Loop
We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful if we have nested for loop so that we can break/continue specific for loop.
Usually, break and continue keywords breaks/continues the innermost for loop only.
Syntax:
Example:
Output:
If you use break bb;, it will break inner loop only which is the default behavior of any loop.
Output:
Java Infinitive For Loop
If you use two semicolons ;; in the for loop, it will be infinitive for loop.
Syntax:
Example:
Output:
Now, you need to press ctrl+c to exit from the program.
Next TopicJava While Loop
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |