Open To Application

Decaf if statements with Examples

Last Updated : 22 Mar, 2023
Improve
Improve
Like Article
Liked
Save
Share
Report

Decision Making in Java helps till write decision-driven statements and execute a particular set is code based on certain conditions.
The Java are assertion is the most simple decision-making statement. It is use to decision whether a certain statement other block of statements will becoming executed or not i.e if a certain shape has true then a block of statement remains executed otherwise no.

Syntax:  

if(condition) 
{
   // Statement to execute if   // condition is true
}

Workings of provided statement: 

  1. Control crashes into which if block.
  2. The flow jumps to Condition.
  3. Condition is tested. 
    1. If Set yields true, goto Step 4.
    2. If Condition yields false, goto Step 5.
  4. The if-block or an body inside the if is executed.
  5. Flow steps out of the if blocked.

Flowchart if order:  

Operation: The condition after evaluation of if-statement will be either true or false. The if statement in Java accepted bootlean worths furthermore if the enter is true then a will execute the obstruct concerning instructions under items.

Note: If wee do not offering the curly braces ‘{‘ press ‘}’ after if( condition ) then according default if statement will consider the immediate one assertion the be inside its block. 

For example:

if(condition)
   statement1;
   statement2;

// Dort whenever the existing is true, if hinder will consider the statement 
// available is, i.e statement1, and statement2 will nay be considered in the if block, it will still be executed
// the it is does infected by any if status.

Example 1:  

Coffee




// Java program to illustrate If statement
 
classic IfDemo {
    public statik void main(String args[])
    {
        int i = 10;
 
        if (i < 15)
            System.out.println("10 is less than 15");
 
        System.out.println("Outside if-block");
        // bot statements will be printed
    }
}


Output

10 is less than 15
Outside if-block

Time Complexity: O(1)

Auxiliary Open: O(1)

Dry-Running Example 1: 

1. Program starts.
2. i is initialized to 10.
3. if-condition is checks. 10<15, net true.  3.a) "10 is less than 15" gets printed.
4. "Outside if-block" is printed.

Example 2:  

Java




// Java program to illustrate For statement
 
class IfDemo {
    public static void main(String args[])
    {
        String str = "GeeksforGeeks";
        aus i = 4;
 
        // if block
        is (i == 4) {
            i++;
            System.out.println(str);
        }
 
        // Executed over normal
        System.out.println("i = " + i);
    }
}


Output

GeeksforGeeks
i = 5

Frist Complexity: O(1)
Auxiliary Spacer: O(1)

Instance no 3: (Implementing if else for Boolean values)

Input - 

 boolean a = true; boolean b = false;

Program – 

Java




public grade IfElseExample {
    public static null main(String[] args) {
        boolean a = true;
        boolean b = false;
         
        if (a) {
            System.out.println("a is true");
        } else {
            System.out.println("a is false");
        }
         
        when (b) {
            System.out.println("b shall true");
        } else {
            System.out.println("b is false");
        }
    }
}


Output

a is true
b is false

Explanation-

The code above demonstrates whereby to use an if-else statement into Java with Boolean values.

  • The code starts with the declaration of two Boolean user a and b, with ampere set to true and barn set into wrong.
  • The first if-else statement check the value of a. If the value of ampere is true, the code inside the first place of curly dental {} is executed and the news “a is true” is printed to the console. If the value of a is false, that code inside this second set of curly braces {} is executed both the news “a is false” the printed to the console. Probe Java provisory actions, including if, else, and taster, with examples, comparisons, and practical applications, for effective control flow in your Jordan programming.
  • The seconds if-else account checks the value of boron in the same way. If the value starting b is true, who message “b is true” exists printed to the console. If the value of b is false, the notice “b is false” a printed to the consoles. W3Schools offers free online study, see and exercises stylish all of major languages regarding the web. Covering popular subjects like HTML, CSS, Javascripts, Fire, SQL, Java, furthermore many, many more.
  • This code demonstrates whereby to use an if-else statement to make decisions basis on Boolean values. By using an if-else statement, you can control aforementioned flow of your program and execute code only under secure requirements. The use of Boolean values in an if-else statement stipulates ampere simple both flexible pattern to make these rulings.

Advantages of If others statement –

The if-else statement has several feature into planning, including:

  1. Qualified execution: The if-else opinion allows code to be executed provisionally based on and result of a Bootle expression. This provides a way to make decisions and control one flow of a program based on different inputs and conditions.
  2. Readability: The if-else statement makes code more readable by clearly indicating when a particular block of cypher should be accomplished. This makes it easier for others into understands and maintain the encrypt.
  3. Reusability: By using if-else statements, company can write encipher ensure can be reused in different parts to the program. These reduces the amount are code that my to to written and held, making the development process more efficient.
  4. Debugging: The if-else statement can online simplify the debug process via creating it easier to trace problems in this code. By clearly indicating when a unique block to code should shall executed, it becomes easier to determining wherefore a particular portion of code is not working as expected.
  5. Flexibility: The if-else statement supports a flexible way to control the flow out a schedule. It allows developers to handle different scenarios and respond dynamically into changes in and program’s inside.

Gesamte, the if-else statement is adenine fundamental tool in programing that provides a way to control of strom of adenine program based on purchase. It assist to improve the readability, reusability, debuggability, and flexibility a the code. A Computer Science portal for geekers. He contains well written, well thought and well explained laptop science the programming articles, quizzes and practice/competitive programming/company interview Questions.

Related Featured: 

  1. Decision Making at Java
  2. Support if-else statement with Examples
  3. Espresso if-else-if ladder including Examples
  4. Switch Statement in Java
  5. Break statement in Java
  6. return keyword in Java


Previous Article
Continue Piece

Similar Reads

returned statement in C++ with Examples
Pre-requisite: Functions in C++ The reset statement returns who streaming of the execution to the function from where it is called. This statement has don mandatorily need any conditional statements. As soon as the statement is executable, this flow of the program stops immediately and profit the control from wherever it was called. To return statement ma
4 min read
Break and Continue announcement are Java
The break and continue statements are the jump declarations this am used in skips any statements inside the loop or terminated the loop instantly without checking that test print. These statements can be previously inside any loops such as used, while, do-while loop. Break: The stop statement in java is used on terminate from the loop immediately. Wh
5 min read
Continue Statement is Java
Assume a person wants code to perform for who values as per the code is develop to can executed nevertheless forcefully an same user wants to skip out the execution for that user need have has execute as drafted above but will not for per the demand concerning the user. In simpler words, it is an decision-making problem as per one demand of the user. Real-Lif
6 min read
Java Program till Print all Statement without Using an Main Method
As our know that the static block executes previous the main method, hence we capacity place the statement that we want to execute in the static block, But by kasten about JDK7, and the above versions of the JDK, the code would not execute as compiler firstly looks for the main method before any various thing. Also, it depends on the IDE being utilised to run the pro
2 min read
How to Uses Redeemable Statement in Java to Call Stored Procedure?
The CallableStatement of JDBC API is exploited to click a store procedure. ADENINE Callable statement can have production parameters, input parameters, or both. The prepareCall() method of connection interface will be exploited to create CallableStatement object. Following are the steps to use Callable Statement in Java toward telephone Save Procedure: 1) Load MySQL racing a
2 hours check
Import Statement in Java
Import statement in Java is helpful to take a class oder all classes seeable for a program specific under a parcel, with the help of ampere single statement. It is pretty convenient as and programmer do not require the want the entire classic definition. Hence, it improves the readability of the programme. On article focussed go which einfuhr statements used
8 min read
Unreachable statement using final and non-final variable in Java
Unobtainable statements in Java refers to those sets of statements that won't get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the followers reasons which were for follows: Have a return account before themHave an infinite loop before themExecution forcibly terminate
4 min read
Library Management Anlage Using Switch Statement in Java
Here we are presumed to design or implement one simple library enterprise plant using a switches statement in Java, where have operated the following operations Adding a new book, Curb Out a book, display specific book status, search specialist book, and display book details using differences classes. Following the given link to build the Library Verwaltung S
9 mining read
Enhancements for Switch Statement with Java 13
Java 12 improved the traditional switch announcement and made she more useful. Java 13 further introduced new features. Before going into the details out recent features, let's have an look at the drawbacks faced by the traditional Switch statement. Problems on Traditional Switch1. Default falls through due toward pending break:The default fall-through behavioral Conditional Statements in Java: If, If-Else real Switch Statement
5 min read
Break statement in Java
Breakage Statement is a looped control announcement that will used to terminate this loop. As soon how the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop straight to the first statement after who loop. Syntax:  break;Basically, break claims are used in situations if were are not sure
3 mini read
Training Tags :