AI May 15, 2023 5 min read

Module 3: Control Flow

BR

Bhargava Ram Pantina

Web Developer & AI Enthusiast

🔄 Conditional Statements

Control the flow of your program using if-else and switch statements.

                    int number = 10;
                    if (number > 0) {
                        System.out.println("Positive number");
                    } else {
                        System.out.println("Non-positive number");
                    }
                    
                    // Switch example
                    int day = 3;
                    switch(day) {
                      case 1: System.out.println("Monday"); break;
                      case 2: System.out.println("Tuesday"); break;
                      case 3: System.out.println("Wednesday"); break;
                      default: System.out.println("Another day");
                    }
                    

🔁 Looping Statements

Java provides several ways to repeat a block of code.

  • for loop
  • while loop
  • do-while loop
                    // for loop
                    for (int i = 1; i <= 5; i++) {
                        System.out.println("i = " + i);
                    }
                    
                    // while loop
                    int j = 1;
                    while (j <= 5) {
                        System.out.println("j = " + j);
                        j++;
                    }
                    
                    // do-while loop
                    int k = 1;
                    do {
                        System.out.println("k = " + k);
                        k++;
                    } while (k <= 5);
                    

⏹ Break and Continue

Use break to exit a loop, and continue to skip an iteration.

                    for (int i = 1; i <= 10; i++) {
                        if (i == 6) break;      // exits loop when i is 6
                        if (i % 2 == 0) continue; // skips even numbers
                        System.out.println(i);
                    }
                    
Artificial Intelligence Web Development Technology Trends Machine Learning UX Design

Share this article

About the Author

BR

Bhargava Ram Pantina

Bhargava Ram is a web developer and AI enthusiast with over 8 years of experience in the tech industry. He specializes in creating innovative web solutions that leverage the latest technologies to solve real-world problems.

Related Articles

AI Mar 15, 2023

Implementing ChatGPT in Your Web Application: A Step-by-Step Tutorial

Learn how to integrate OpenAI's ChatGPT into your web application to create intelligent chatbots and enhance user interactions.

Read More →
Web Development Apr 22, 2023

Building Accessible Web Forms: A Complete Guide

Learn how to create web forms that are accessible to all users, including those with disabilities. This comprehensive guide covers ARIA attributes, keyboard navigation, and more.

Read More →

Subscribe to My Newsletter

Get the latest articles and resources delivered straight to your inbox.

No spam, just valuable content. Unsubscribe anytime.