Menu
×

HTML Reference

Complete HTML tags reference

HTML Tags Reference
'; var sectionHeading = document.querySelector(".refnav-section-heading"); if (sectionHeading) sectionHeading.style.display = ""; var refHeader = document.querySelector(".refnav-header"); if (refHeader) refHeader.classList.remove("filter-only"); container.innerHTML = cardHtml; } else if (pathSeg1 === "js" || pathSeg1 === "javascript") { if (mainHeading) { mainHeading.style.display = ""; mainHeading.innerHTML = '

JAVASCRIPT REFERENCE

'; } var cardHtml = '

JavaScript Reference

Complete JavaScript reference

JavaScript Reference
'; var sectionHeading = document.querySelector(".refnav-section-heading"); if (sectionHeading) sectionHeading.style.display = ""; var refHeader = document.querySelector(".refnav-header"); if (refHeader) refHeader.classList.remove("filter-only"); container.innerHTML = cardHtml; } else { if (mainHeading) mainHeading.style.display = "none"; var w3Svg = ''; var certUrl2 = "https://campus.w3schools.com/collections/course-catalog"; var previewSvg2 = '' + '' + '' + '' + '' + 'CERTIFICATE OF COMPLETION' + 'This certifies that' + 'Your Name' + 'has passed the W3Schools Certification exam and is hereby declared a' + '' + w3Svg + "" + 'Certified Developer' + 'The candidate has passed the exam at the Professional level.' + 'Verify at' + 'verify.w3schools.com' + 'Stale Refsnes' + 'for w3schools.com' + ""; var cardHtml = '
' + '

Become W3Schools Certified

' + '

Get certified, and earn a credential that proves your skills.

' + '

From $95

' + '

Includes exam, course materials, exercises, and one year of the Hero plan.

' + 'Get W3Schools Certified' + '
' + previewSvg2 + "
"; var sectionHeading = document.querySelector(".refnav-section-heading"); if (sectionHeading) { sectionHeading.style.display = ""; sectionHeading.textContent = "W3SCHOOLS REFERENCES:"; } var refHeader = document.querySelector(".refnav-header"); if (refHeader) refHeader.classList.remove("filter-only"); container.innerHTML = cardHtml; } } var refCertSlugMap = { html: "html-course", css: "css-course", javascript: "javascript-course", sql: "sql-course", python: "python-course", php: "php-course", java: "java-course", c: "learn-c", "c++": "c++-course", "c#": "c-course", bootstrap3: "bootstrap-3-certificate", bootstrap4: "bootstrap-4-certificate", react: "react-js-course", mysql: "mysql-course", jquery: "jquery-course", typescript: "typescript-course", nodejs: "nodejs-course", vue: "vue-js-course", django: "learn-django", numpy: "numpy-course", dsa: "dsa-course", excel: "learn-excel", xml: "xml-course", cybersecurity: "cyber-security-course", accessibility: "accessibility-course", git: "git-course", kotlin: "kotlin-course", mongodb: "mongodb-course", postgresql: "postgresql-course", r: "r-course", scipy: "scipy-course", angularjs: "angularjs-certificate", pandas: "pandas-course", w3css: "w3-css-certificate", angular: "angular-course", asp: "asp-certificate", sass: "sass-certificate", json: "json-certificate", svg: "svg-certificate", canvas: "canvas-certificate", bash: "bash-certification-exam" }; var refCertFullUrlMap = { pandas: "https://campus.w3schools.com/collections/course-catalog/products/pandas-course", c: "https://campus.w3schools.com/collections/course-catalog/products/learn-c", "c++": "https://campus.w3schools.com/collections/course-catalog/products/cpp-course", "c#": "https://campus.w3schools.com/collections/course-catalog/products/c-course", react: "https://campus.w3schools.com/collections/course-catalog/products/react-js-course", mysql: "https://campus.w3schools.com/collections/course-catalog/products/learn-mysql-1", jquery: "https://campus.w3schools.com/collections/course-catalog/products/jquery-course", excel: "https://campus.w3schools.com/collections/course-catalog/products/learn-excel", xml: "https://campus.w3schools.com/collections/course-catalog/products/xml-course", django: "https://campus.w3schools.com/collections/course-catalog/products/learn-django", numpy: "https://campus.w3schools.com/collections/course-catalog/products/numpy-course", nodejs: "https://campus.w3schools.com/collections/course-catalog/products/learn-node-js", dsa: "https://campus.w3schools.com/collections/course-catalog/products/learn-data-structures-and-algorithms", typescript: "https://campus.w3schools.com/collections/course-catalog/products/learn-typescript", git: "https://campus.w3schools.com/collections/course-catalog/products/learn-git", postgresql: "https://campus.w3schools.com/collections/course-catalog/products/learn-postgresql", mongodb: "https://campus.w3schools.com/collections/course-catalog/products/learn-mongodb", kotlin: "https://campus.w3schools.com/collections/course-catalog/products/learn-kotlin", vue: "https://campus.w3schools.com/collections/course-catalog/products/learn-vue-js", scipy: "https://campus.w3schools.com/collections/course-catalog/products/learn-scipy", cybersecurity: "https://campus.w3schools.com/collections/course-catalog/products/cyber-security-course", accessibility: "https://campus.w3schools.com/collections/course-catalog/products/accessibility-course", bootstrap3: "https://campus.w3schools.com/collections/course-catalog/products/bootstrap-course" }; function showRefTopicCard(topicName) { var mainHeading = document.getElementById("refnav_main_heading"); var card = document.querySelector('#nav_references .refnav-card[data-name="' + topicName + '"]'); var label = card ? card.querySelector(".refnav-card-title").textContent : topicName; if (mainHeading) { mainHeading.style.display = ""; mainHeading.innerHTML = '

' + label.toUpperCase() + " REFERENCE

"; } var url = refUrlMap[topicName] || ""; var container = document.getElementById("refnav_quick_actions"); var iconEl = card ? card.querySelector(".refnav-icon-wrap") : null; var iconHtml = ""; if (iconEl) { var cloned = iconEl.cloneNode(true); var svg = cloned.querySelector("svg"); if (svg) { svg.removeAttribute("width"); svg.removeAttribute("height"); iconHtml = svg.outerHTML; } } var cardHtml = '
'; if (url) { cardHtml += '
' + iconHtml + '

' + label + ' Reference

Complete reference for ' + label + '

' + label + " Reference
"; } var certSlug = refCertSlugMap[topicName]; if (certSlug) { var certUrl = refCertFullUrlMap[topicName] || "https://campus.w3schools.com/collections/certifications/products/" + certSlug; var certIconSvg = (window.iconSvgs && window.iconSvgs[topicName]) || iconHtml; var certIconSvg48 = certIconSvg ? certIconSvg.replace(/width="[^"]*"/, 'width="48"').replace(/height="[^"]*"/, 'height="48"') : ""; var certPreview = '' + '' + '' + '' + '' + 'CERTIFICATE OF COMPLETION' + 'This certifies that' + 'Your Name' + 'has passed the W3Schools ' + label + " Certification exam and is hereby declared a" + '' + certIconSvg48 + "" + 'Certified ' + label + " Developer" + 'The candidate has passed the exam at the Professional level.' + 'Verify at' + 'verify.w3schools.com' + 'Stale Refsnes' + 'for w3schools.com' + ""; cardHtml += '
"; cardHtml += '
'; cardHtml += '

Become ' + label + " Certified

"; cardHtml += '

Get certified with our ' + label + " exam, includes a professional study kit to guide you from beginner to exam-ready.

"; cardHtml += '

$95one-time

'; cardHtml += 'Get ' + label + " Certified"; cardHtml += "
"; cardHtml += '
' + certPreview + "
"; cardHtml += "
"; } cardHtml += "
"; container.innerHTML = cardHtml; var prev = document.querySelector("#nav_references .refnav-card-active"); if (prev) prev.classList.remove("refnav-card-active"); if (card) card.classList.add("refnav-card-active"); var scrollEl = document.querySelector("#nav_references .refnav-topics-scroll"); if (scrollEl) scrollEl.scrollTop = 0; } // Clone icons from Exercises cards (lazy � runs on first open) var iconsCloned = false; function cloneExerciseIcons() { if (iconsCloned) return; var refCards = document.querySelectorAll("#nav_references .refnav-card"); var anyCloned = false; for (var i = 0; i < refCards.length; i++) { var rc = refCards[i]; var dn = rc.getAttribute("data-name"); if (!dn) continue; var exCard = document.querySelector('#nav_exercises .exnav-card[data-name="' + dn + '"]'); if (!exCard) continue; var exIcon = exCard.querySelector(".exnav-icon-wrap svg"); if (!exIcon) continue; var clonedSvg = exIcon.cloneNode(true); var grads = clonedSvg.querySelectorAll("[id]"); for (var g = 0; g < grads.length; g++) { grads[g].id = "ref-" + grads[g].id; } var fills = clonedSvg.querySelectorAll('[fill^="url("]'); for (var f = 0; f < fills.length; f++) { var fv = fills[f].getAttribute("fill"); fills[f].setAttribute("fill", fv.replace("url(#", "url(#ref-")); } var iconWrap = rc.querySelector(".refnav-icon-wrap"); if (iconWrap) { iconWrap.innerHTML = ""; iconWrap.appendChild(clonedSvg); anyCloned = true; } } if (anyCloned) { iconsCloned = true; if (activeRefTopic) showRefTopicCard(activeRefTopic); else showDefaultRefCard(); } } if (activeRefTopic) showRefTopicCard(activeRefTopic); else showDefaultRefCard(); var nav = document.getElementById("nav_references"); if (nav) { // Clone icons when modal becomes visible var observer = new MutationObserver(function () { if (nav.style.display === "block") cloneExerciseIcons(); }); observer.observe(nav, { attributes: true, attributeFilter: ["style"] }); nav.addEventListener("click", function (e) { var card = e.target.closest(".refnav-card"); if (!card) return; var name = card.getAttribute("data-name"); if (!name) return; var url = refUrlMap[name]; if (url) { window.location.href = url; } }); } // Populate references card icons from shared iconSvgs library var refCards = document.querySelectorAll("#nav_references .refnav-card"); refCards.forEach(function (card) { var topicName = card.getAttribute("data-name"); var iconWrap = card.querySelector(".refnav-icon-wrap"); if (iconWrap && window.iconSvgs && window.iconSvgs[topicName]) { iconWrap.innerHTML = window.iconSvgs[topicName]; } }); })();
"; // Show the "SELECT ANOTHER TOPIC:" heading when displaying specific exercise topics var sectionHeading = document.querySelector("#nav_exercises .exnav-section-heading"); if (sectionHeading) sectionHeading.style.display = ""; // Remove filter-only class when heading is shown var exHeader = document.querySelector("#nav_exercises .exnav-header"); if (exHeader) exHeader.classList.remove("filter-only"); var qaHtml = '
'; for (var q = 0; q < qaOptions.length; q++) { var qo = qaOptions[q]; var dis = qo.url ? "" : "data-disabled"; var cardTitle = qo.title; var cardDesc = qo.url ? qo.desc : "Coming soon"; var pLabel = qo.total > 0 ? (qo.label === "score" ? "Score: 0 / " + qo.total : "0 / " + qo.total + " completed") : ""; qaHtml += '"; } qaHtml += "
"; qaContainer.innerHTML = qaHtml; var qaTip = document.getElementById("exnav_quick_tip"); if (qaTip) qaTip.style.display = "flex"; if (currentHighlight) currentHighlight.style.background = ""; var prevActive = document.querySelector("#nav_exercises .exnav-card-active"); if (prevActive) prevActive.classList.remove("exnav-card-active"); if (qaCard) { qaCard.classList.add("exnav-card-active"); currentHighlight = qaCard; } var scrollEl = document.querySelector("#nav_exercises .exnav-topics-scroll"); if (scrollEl) scrollEl.scrollTop = 0; var listEl = document.getElementById("exercises_list"); if (listEl) listEl.scrollTop = 0; } if (activeTopic) { showTopicQuickActions(activeTopic); } else { // Show default exercises heading for folders not in exercise list var mainHeading = document.getElementById("exnav_main_heading"); var quickActions = document.getElementById("exnav_quick_actions"); var sectionHeading = document.querySelector("#nav_exercises .exnav-section-heading"); if (mainHeading) mainHeading.innerHTML = '

W3SCHOOLS EXERCISES

'; if (sectionHeading) { sectionHeading.style.display = ""; sectionHeading.textContent = "Select a topic:"; } var exHeader = document.querySelector("#nav_exercises .exnav-header"); if (exHeader) exHeader.classList.remove("filter-only"); // Build multi-icon SVG: 2x2 grid of small topic icons var pyIcon = (window.iconSvgs["python"] || "") .replace(/id="tut-py-b"/g, 'id="def-py-b"') .replace(/id="tut-py-y"/g, 'id="def-py-y"') .replace(/url\(#tut-py-b\)/g, "url(#def-py-b)") .replace(/url\(#tut-py-y\)/g, "url(#def-py-y)"); var defaultCards = [ { title: "Exercises", desc: "Practice with interactive exercises", url: "/exercises/index.php", icon: qaIcons.exercises, btnText: "Browse Exercises", subtitle: "What is an Exercise?", subtitleUrl: "/exercises/index.php" }, { title: "Quizzes", desc: "Test your knowledge", url: "/quiztest/default.asp", icon: qaIcons.quiz, btnText: "Browse Quizzes", subtitle: "What is a Quiz?", subtitleUrl: "/quiztest/default.asp" }, { title: "Challenges", desc: "Solve coding challenges", url: "/challenges/index.php", icon: qaIcons.challenges, btnText: "Browse Challenges", subtitle: "What is a Challenge?", subtitleUrl: "/challenges/index.php" }, { title: "Practice Problems", desc: "Solve real-life coding problems", url: "/practice/index.php", icon: qaIcons.problems, btnText: "Browse Problems", subtitle: "What is a Practice Problem?", subtitleUrl: "/practice/index.php" }, ]; var defHtml = '
'; for (var d = 0; d < defaultCards.length; d++) { var dc = defaultCards[d]; defHtml += '"; } defHtml += "
"; if (quickActions) quickActions.innerHTML = defHtml; } var nav = document.getElementById("nav_exercises"); nav.addEventListener("click", function (e) { var card = e.target.closest(".exnav-card"); if (!card) return; var name = card.getAttribute("data-name"); if (!name) return; var urls = urlMap[name]; if (urls && urls.exercises) { window.location.href = urls.exercises; } }); // Populate exercises card icons from shared iconSvgs library var exCards = document.querySelectorAll("#nav_exercises .exnav-card"); exCards.forEach(function (card) { var topicName = card.getAttribute("data-name"); var iconWrap = card.querySelector(".exnav-icon-wrap"); if (iconWrap && window.iconSvgs && window.iconSvgs[topicName]) { var svgHtml = window.iconSvgs[topicName]; if (topicName === "python") { svgHtml = svgHtml .replace(/id="tut-py-b"/g, 'id="ex-py-b"') .replace(/id="tut-py-y"/g, 'id="ex-py-y"') .replace(/url\(#tut-py-b\)/g, "url(#ex-py-b)") .replace(/url\(#tut-py-y\)/g, "url(#ex-py-y)"); } iconWrap.innerHTML = svgHtml; } }); })();
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Java Tutorial

Java HOME Java Intro Java Get Started Java Syntax Java Output Java Comments Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Math Java Booleans Java If...Else Java Switch Java While Loop Java For Loop Java Break/Continue Java Arrays

Java Methods

Java Methods Java Method Challenge Java Method Parameters Java Method Overloading Java Scope Java Recursion

Java Classes

Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Class Challenge Java Constructors Java this Keyword Java Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java super Keyword Java Inner Classes Java Abstraction Java Interface Java Anonymous Java Enum Java User Input Java Date

Java Errors

Java Errors Java Debugging Java Exceptions Java Multiple Exceptions Java try-with-resources

Java File Handling

Java Files Java Create Files Java Write Files Java Read Files Java Delete Files

Java I/O Streams

Java I/O Streams Java FileInputStream Java FileOutputStream Java BufferedReader Java BufferedWriter

Java Data Structures

Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator Java Algorithms

Java Advanced

Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting

Java Projects

Java Projects

Java Cert

Java Certificate

Java How To's

Java How Tos

Java Reference

Java Reference Java Keywords Java String Methods Java Math Methods Java Output Methods Java Arrays Methods Java ArrayList Methods Java LinkedList Methods Java HashMap Methods Java Scanner Methods Java File Methods Java FileInputStream Java FileOutputStream Java BufferedReader Java BufferedWriter Java Iterator Methods Java Collections Methods Java System Methods Java Errors & Exceptions

Java Examples

Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A


Java For Loop


Java For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.


Print Numbers

The example below will print the numbers 0 to 4:

Example

for (int i = 0; i < 5; i++) {
  System.out.println(i);
}

Try it Yourself »

Example explained

  • Statement 1 sets a variable before the loop starts: int i = 0
  • Statement 2 defines the condition for the loop to run: i < 5. If the condition is true, the loop will run again; if it is false, the loop ends.
  • Statement 3 increases a value each time the code block has run: i++

Print Even Numbers

This example prints even values between 0 and 10:

Example

for (int i = 0; i <= 10; i = i + 2) {
  System.out.println(i);
}

Try it Yourself »



Sum of Numbers

This example calculates the sum of numbers from 1 to 5:

Example

int sum = 0;
for (int i = 1; i <= 5; i++) {
  sum = sum + i;
}
System.out.println("Sum is " + sum);

Try it Yourself »


Countdown

This example prints a countdown from 5 to 1:

Example

for (int i = 5; i > 0; i--) {
  System.out.println(i);
}

Try it Yourself »


For Loop With False Condition

Just like a while loop, a for loop may also never run. If the condition is false right from the start, the code inside the loop will be skipped entirely:

Example

for (int i = 10; i < 5; i++) {
  System.out.println("This will never be printed");
}

Try it Yourself »

In this example, the loop starts with i = 10. The condition i < 5 is already false, so the loop body is skipped, and nothing is printed.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->