Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Loops In Java

Rating
-
Sold
-
Pages
8
Uploaded on
25-01-2025
Written in
2024/2025

This document goes through the three loops in java, the for loop, while loop and do while loop, including their explanation, syntax, flowchart, and their example codes

Institution
Course

Content preview

What is a Loop?

A loop is a programming construct that allows you to execute a block of code repeatedly based on a
condition. Loops reduce redundancy and improve code efficiency, especially when the same
operation needs to be performed multiple times.



1. For Loop

Detailed Explanation:

 Purpose: Used when the number of iterations (repetitions) is known beforehand.

 How It Works:

1. The initialization step runs once before the loop starts. It sets up the loop control
variable (e.g., int i = 0).

2. The condition is checked before each iteration:

 If true, the loop body executes.

 If false, the loop stops.

3. The increment/decrement step runs after the loop body to update the loop control
variable.



Syntax:

for (initialization; condition; increment/decrement) {

// Code to execute repeatedly

}



Flowchart:

1. Initialize the loop variable.

2. Check the condition:

o If true, execute the loop body.

o If false, exit the loop.

3. Update the loop variable (increment or decrement).

4. Repeat steps 2 and 3 until the condition becomes false.



Example 1: Print Numbers from 1 to 5

public class ForLoopExample {

, public static void main() {

for (int i = 1; i <= 5; i++) {

System.out.println("Number: " + i);

}

}

}

Output:

Number: 1

Number: 2

Number: 3

Number: 4

Number: 5



Example 2: Calculate the Sum of First 10 Natural Numbers

public class SumExample {

public static void main() {

int sum = 0;

for (int i = 1; i <= 10; i++) {

sum += i; // sum = sum + i

}

System.out.println("Sum of first 10 numbers: " + sum);

}

}

Output:

Sum of first 10 numbers: 55



Practical Uses of For Loop:

 Printing multiplication tables.

 Calculating the factorial of a number.

 Repeating an operation a fixed number of times.

Written for

Course

Document information

Uploaded on
January 25, 2025
Number of pages
8
Written in
2024/2025
Type
Class notes
Professor(s)
Denzi
Contains
All classes

Subjects

$4.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sharmsrizzler12307

Get to know the seller

Seller avatar
sharmsrizzler12307
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions