Day : 32 "Byte by Byte: The Programmer’s Journey"

Java programs are typically structured into classes, which are blueprints for objects. Objects are instances of classes and contain data (fields) and methods (actions) that operate on that data. A simple "Hello, World!" program in Java demonstrates the basic structure: public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } } In this program: public class Main declares a class named "Main". public static void main(String[] args) is the main method, the entry point of the program. System.out.println("Hello, World!"); prints the text "Hello, World!" to the console. Normal Statement Conditional Statement Control Flow statement why if and else statement in programming in java In Java programming, the if and else statements are fundamental control flow structures used for decision-making. They allow a program to execute different blocks of code based on whether a specified condition is true or false. The if statement evaluates a boolean expression. If the expression is true, the code block within the if statement is executed. If the expression is false, the code block is skipped. Some examples of using this keywords If, Else if and Else Example 01 package demo_programs; public class Example1 { public static void main(String[] args) { // TODO Auto-generated method stub int no1= 6; int no2 = 6; if (no1>no2) { System.out.println(no1); }else if (no2>no1) { System.out.println(no2); }else {System.out.println("Equal"); } } } //output is Equal Example 02 package demo_programs; public class Example2 { public static void main(String[] args) { if (20 > 18) { System.out.println("20 is greater than 18"); } } } //output is 20 is greater than 18 Example 03 package demo_programs; public class Example3 { public static void main(String[] args) { // TODO Auto-generated method stub int x = 20; int y = 18; if (x > y) { System.out.println("x is greater than y"); } } } //x is greater than y Example 04 (TBH) package demo_programs; public class Example4{ public static void main(String[] args) { // TODO Auto-generated method stub String fav1 ="Biryani"; String fav2 ="Chicken fried rice"; if (fav1.equals("Biryani")){ System.out.println("Loves to eat biryani"); }else if (fav2.equals("Chicken fried rice")) { System.out.println("Loves to eat Chicken fried rice"); }else { System.out.println("take some different food"); } } } //output Loves to eat biryani Nested if Nested if refers to an if statement within an if statement. When we write an inner if condition within an outer if condition, then it is referred to as a nested if statement in java. Nested if is a decision-making statement that works similar to other decision-making statements such as if, else, if..else, etc. Example 05 package demo_programs; public class Example2 { public static void main(String[] args) { boolean yamakadhagi = false; if(yamakadhagi==true) { boolean horror = true; if (horror==true) { boolean interested = false; if (interested==true) { System.out.println("watching movie with families"); }else if (interested==false) { System.out.println("watch the movie alone"); } } } else if (yamakadhagi==false) { System.out.println("Dragon movie"); } } } //output is Dragon movie

Apr 29, 2025 - 05:10
 0
Day : 32 "Byte by Byte: The Programmer’s Journey"

Java programs are typically structured into classes, which are blueprints for objects. Objects are instances of classes and contain data (fields) and methods (actions) that operate on that data. A simple "Hello, World!" program in Java demonstrates the basic structure:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

In this program:

  1. public class Main declares a class named "Main".
  2. public static void main(String[] args) is the main method, the entry point of the program.
  3. System.out.println("Hello, World!"); prints the text "Hello, World!" to the console.
  • Normal Statement
  • Conditional Statement
  • Control Flow statement

why if and else statement in programming in java

  • In Java programming, the if and else statements are fundamental control flow structures used for decision-making. They allow a program to execute different blocks of code based on whether a specified condition is true or false.
  • The if statement evaluates a boolean expression. If the expression is true, the code block within the if statement is executed. If the expression is false, the code block is skipped.

Some examples of using this keywords If, Else if and Else

Example 01

package demo_programs;

public class Example1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
     int no1= 6;
     int no2 = 6;

     if (no1>no2) {
   System.out.println(no1);  
     }else if (no2>no1) {
         System.out.println(no2);
     }else {System.out.println("Equal");
    }
} }
//output is Equal

Example 02

package demo_programs;

public class Example2 {
    public static void main(String[] args) {
        if (20 > 18) {
              System.out.println("20 is greater than 18");
            }

    }

}
//output is 20 is greater than 18

Example 03

package demo_programs;

public class Example3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x = 20;
        int y = 18;
        if (x > y) {
          System.out.println("x is greater than y");
        }

    }

}
//x is greater than y

Example 04 (TBH)

package demo_programs;

public class Example4{

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String fav1 ="Biryani";
            String fav2 ="Chicken fried rice";
            if (fav1.equals("Biryani")){
                System.out.println("Loves to eat biryani");
            }else if (fav2.equals("Chicken fried rice")) {
                System.out.println("Loves to eat Chicken fried rice");
            }else {
                System.out.println("take some different food");
            }

    }

}
//output Loves to eat biryani

Nested if
Nested if refers to an if statement within an if statement. When we write an inner if condition within an outer if condition, then it is referred to as a nested if statement in java. Nested if is a decision-making statement that works similar to other decision-making statements such as if, else, if..else, etc.

Example 05

package demo_programs;

public class Example2 {
    public static void main(String[] args) {
        boolean yamakadhagi = false;
        if(yamakadhagi==true) {
             boolean horror = true;
        if (horror==true) {
             boolean interested = false;
        if (interested==true) {
            System.out.println("watching movie with families");
        }else if (interested==false) {
            System.out.println("watch the movie alone");
        }
            }
        }   else if (yamakadhagi==false) {
            System.out.println("Dragon movie");
        }
    }
}
//output is Dragon movie