Showing posts with label Control Statements. Show all posts
Showing posts with label Control Statements. Show all posts

Tuesday, November 10, 2020

While Loop Print Prime Numbers In Java

1. Overview


In this article, You'll learn how to use a while loop to print prime numbers in java. This is a very basic programming interview question for the freshers. First, we'll talk about what is prime number?.

A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. 

Note:

A natural number greater than 1 that is not prime is called a composite number.




Wednesday, August 12, 2020

Java For loop Examples - Nested For Loop Examples

For Loop in Java:

In java, for-loop has two versions.

1) Basic for loop (Old style)
2) Enhanced for loop (For-each or for-in loop)


First, we will discuss basic for loop then next discuss Enhanced loop.
The enhanced loop is designed mainly for Arrays or Collection. We will discuss this in the next tutorial.

Enhanced for loop and Examples.

Java For loop, Examples

Basic for loop:

The basic loop has the flexibility to use it as per our needs. This is very useful if we know how many times it needs to be executed.

It has three sections.

1) Variable declaration and its initialization
2) Condition: Untill condition is true, the loop will be executed.
3) Variable increment/decrement

Tuesday, April 9, 2019

Java While Loop - While loop Example Programs, Nested While Loop Examples

While Loop In Java:

In this tutorial, we will discuss about while loop, its syntax, example, how to make infinite loop.

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.

Java While loop is an iterative loop and used to execute set of statements for a specified number of times.


Java While Loop - While loop Example Programs, Nested While Loop Examples


We will see now

1. While loop syntax
2. While flowchart
3. Infinite while loop
4. Nested while loop

If, If Else, nested Condition - Statement in java with Examples

If Statement in java 

In this post, We will discuss about If statement, If Else, Multiple if-else, nested Statement or condition in java with examples.



We will learn
  1. If statement
  2. If-Else statement
  3. Multiple if else statement
  4. Nested if else statement
If Statement in java


Java If Statement:

This is very basic topic in every programming language and simple to learn, use it in your programs.
First we see the syntax of If Statement.

Wednesday, February 6, 2019

Java Switch Statement



In this tutorial, We will learn switch statement.
Switch statement is similar to If else statement. If and If-Else statements can be used to support both simple and complex decision logic. In many cases, the switch statement provides a cleaner way to handle complex decision(business) logic.
switch statements depends on the expression "matching" or being "equal" to one of the cases.


We will learn 1) Switch Statement, syntax, example
2) Default case
3) Java 8 Switch statement
4) Break and Fall down/through switch statement

Read more about "If, If Else, nested Condition - Statement in java".

Saturday, September 9, 2017

Java Enhanced For loop, Examples

Enhanced for loop in java, Java enhanced for loop, Java for-each loop, Enhanced for loop example, Advantageous of for-each loop. Iterating through basic for loop. Iterating through Arrays, Iterating through Collection

Enhanced for loop to overcome the drawbacks of basic for loop and this best suitable for iterating Arrays and Collections.

Syntax:


for ( varaible_declaration : Array or Collection)
{
    //
}

Java Enhanced For loop, Examples

Java Continue Statement, Label loop

Java Continue Statement, Continue Statement in Java, Continue Examples in java, Java continue in inner loop, inner loop continue example, Continue Statement Examples.

Continue statement is used to skip the current loop iteration when a condition is satisfied.
Mainly used in while or for loop.

Syntax:

continue;

Example Flow chart:

Java Continue Statement, Label loop

Tuesday, September 5, 2017

Java Break Statement With Examples


Java break statement is used in a loop or switch case. If it encounters inside a loop then it terminates the loop and continues execution after the loop.

While loops in Java

It can be used to terminate a case in the switch statement.

Switch statement in java

Syntax:

break;

Flow Chart:

Java Break statement