0% found this document useful (0 votes)
4 views27 pages

Java While

The document provides an overview of various looping constructs in Java, including while loops, nested while loops, do-while loops, for loops, and nested for loops, along with their syntax and examples. It also covers arithmetic and logical operations, if-else conditions, and how to determine student grades based on average marks. Each section includes algorithms, flowcharts, and sample programs demonstrating the concepts discussed.

Uploaded by

mhys2827
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)
4 views27 pages

Java While

The document provides an overview of various looping constructs in Java, including while loops, nested while loops, do-while loops, for loops, and nested for loops, along with their syntax and examples. It also covers arithmetic and logical operations, if-else conditions, and how to determine student grades based on average marks. Each section includes algorithms, flowcharts, and sample programs demonstrating the concepts discussed.

Uploaded by

mhys2827
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/ 27

While Loop:

A while loop in Java is a control flow statement that repeatedly executes a block of code as long
as a given condition is true. The syntax of a while loop
initializationn while
(condition)
{ statement; inc/dec;
}

The condition is a Boolean expression. If the condition evaluates to true, the code block is
executed. After the code block is executed, the condition is evaluated again. If the condition
evaluates to true again, the code block is executed again. This process continues until the
condition evaluates to false Flow chart:

1.Sum of n numbers using While Loop.


Aim:java program for Sum of n numbers using while loop.
Algoritham:
1. Start
2. Declare I , n , sum
3. To assign the values i=1 , n=3 ,sum=0
4. I=1 , sum=0 , n=3
While(i<=n)
{
Sum=sum+I;
I++;
}
4.1: i=2 , sum=1 , n=3
While(i<=n)
{
Sum=sum+I;
I++;
}
4.2: i=3 , sum=3 , n=3
While(i<=n)
{
Sum=sum+I;
i++;
}
4.3:i=4 , sum=6 , n=3
While(i<=n)
{
Skip
}
Step5:display the sum of n natural numbers is 6.
Step6:stop.

Flow chart:-
Program:

Output:
Nested while:
A nested while loop in Java is a while loop inside another while loop. The inner
loop is executed repeatedly for each iteration of the outer loop
Syntax:
while(condition)
{ while(condition) { //
statement of inside loop
}
// statement of outer loop
}

Flow chart:

Example:
Aim:-To write a java program using Nested While Algorithm:

Flow chart:
Output:

Do While Loop:
Do-while loops are useful when you need to execute a block of code at least
once, even if the condition is false. For example, you might use a do-while loop
to read input from the user until they enter a valid value.
Syntax:
do
{
// Loop Body
Update_expression
}
// Condition check while
(test_expression);

Flow chart:

Example :
Aim :To write a java programming using Do While Algorithm:
Flow chart:
Program:

Output:
Infinite While loop:
Syntax:
while (true) {
// Statements to be executed infinitely
}
Flow chart:

Example:
Aim:To write a java program using Infinite while loop.
Algorithm:
Flow chart:

Program:
Output:

For Loop:
A for loop is a programming construct that allows us to execute a block of
code repeatedly, until a certain condition is met. The initialization statement is
executed once, before the loop starts. The condition statement is evaluated before
each iteration of the loop. If the condition is true, the code block is executed.
After the code block is executed, the increment statement is executed. The loop
then repeats, starting with the condition statement.
Syntax:
for (initialization; condition; increment) {
// code block to be executed
}
Example:
Aim: To write a java program using for loop.
Algorithm:
Flowchart:
Program:

Output:
Nested for :
A nested loop has one loop inside of another. These are typically used for
working with two dimensions such as printing stars in rows and columns as
shown below. When a loop is nested inside another loop, the inner loop is runs
many times inside the outer loop.

Syntax:
for (initialization; condition; increment) { for(initialization;
condition; increment) {
Satament
}
}
Flowchart:
Example:
Output:

Infinite For loop:


Syntax:
for (;;) {
// statements to be executed repeatedly
}
Example:
Aim:To write a java program using infinite while loop Algorithma:

Flowchart:
Algorithm:
Program:

Output:
Q.Welcome to java using Java Program
Aim :- To write a java program to display welcome to Java.
Algoritham:-
Step1-start
Step2-Display welcome to Java
Step3-Stop

Flow chart:-
Program:-

Output:

Q.Arithmetic operations using Java Programing

Arithmetic operator:

Symbol Description Example

+ Adds together two values A +B

- Substracts one value from another A -B


* Multiply two values A*B
/ Divides one value by another A/B
% Returnd the division remainder A%B

Addition Operator (+): The addition operator is used to add two numbers
together. For example, the code `int a = 10; int b = 2; int c = a + b;` will assign
the value 12 to the variable c.
Subtraction Operator (-): The subtraction operator is used to subtract one number
from another. For example, the code `int a = 10; int b = 2; int c = a - b;` will
assign the value 8 to the variable c.
Multiplication Operator (*): The multiplication operator is used to multiply two
numbers together. For example, the code `int a = 10; int b = 2; int c = a * b;` will
assign the value 20 to the variable c.
Division Operator (/):The division operator is used to divide one number by
another. For example, the code `int a = 10; int b = 2; int c = a / b;` will assign the
value 5 to the variable c.
Modulus Operator (%): The modulus operator is used to find the remainder of a
division operation. For example, the code `int a = 10; int b = 2; int c = a % b;`
will assign the value 0 to the variable c.

Aim:-To write a Java program to perform Airthmetic operations.


Algorithm:
Step1.Start
Step2.Declare a variable a,b
Step3.Assign the values a=5,b=3
Step4.Display addition of a+b
5+3=8
Display substraction of a-b
5-3=2
Display multiplicaton of a*b
5*3=15
Display divison of a/b
5/3=1
Display modulas of a%b
5%3= Step5.Stop.

Flow chart:
Program:

Output:
Q.Logical operation using java program.

Logical Operators:
Operator Description
&& Logical AND
|| Logical OR
! Logical NOT

The three logical operators in Java are:


&& (AND): The AND operator returns true if both of its operands are true.
Otherwise, it returns false.
|| (OR): The OR operator returns true if either of its operands is true. Otherwise, it
returns false.
! (NOT): The NOT operator returns the opposite of its operand. If the operand is
true, the NOT operator returns false. If the operand is false, the NOT operator
returns true.
Aim: To write Java program to perform logical operations Algorith:
Step1.Start
Step2.Initialize variables a,b,c
Step3.Read the values of a,b,c
Step4.Disply AND operation result
(a==b)&&(a==c)
Display OR operation result
(a==b)||(a==c)
Display NOT operation result
!(a>b)
Step5.stop

Flow chart:

Program:
Output:

Q. IF ELSE Condition
Syntax :-

if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}

The if-else statement allows you to execute different code blocks based on whether a
specified condition is true or false.
Aim: To write a Java program to perform If else condition Algoritham:
Step1.Start
Step2.Declare a variable n
Step3.Read the value of n
Step4.Check if the entered number n is divisible by 2 without a remainder,
indicating it is an even number
Step5.If it is even, print "n is a Even Number".
Step6. it has a remainder when divided by 2 it is considered odd: Print "n is a
Odd Number".
Step7.Stop

Flow chart:

Program:
Output:

Q. How to find student grade in java programming.


The else if ladder condition is a set of conditions which is based on “Nested if
condition”. The if-else ladder condition performs the expressions from top to
down. Once the condition is true, the statement for the same is executed, and the
balance ladder rules are skipped.
Aim:To write a java program to find the grade of a student Algorithm:
1.Start
2. Initialize a variables to store the marks: English, Math, Science, and Social.
3. Read the marks of English, Math, Science, and Social.
4.Calculate the total marks obtained by adding the marks of all subjects Total=
English+Math+ Science+ Social.
5. Calculate the average and store in avg
Avg= Total/4
6. Use conditional statements (`if-else`) to determine the grade - If the
average is greater than or equal to 90 Display"A grade".
- If the average is between 80 and 89 Display "B grade".
- If the average is between 70 and 79 Display "C grade".
- If the average is between 60 and 69 Display"D grade".
- If the average is between 50 and 59 Display"E grade".
- If none of the above conditions are met Display "Fail".
7.End
Flow chart:

Program:

You might also like