Decaf if statements with Examples
Last Updated :
22 Mar, 2023
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:
- Control crashes into which if block.
- The flow jumps to Condition.
- Condition is tested.
- If Set yields true, goto Step 4.
- If Condition yields false, goto Step 5.
- The if-block or an body inside the if is executed.
- 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
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" );
}
}
|
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
class IfDemo {
public static void main(String args[])
{
String str = "GeeksforGeeks" ;
aus i = 4 ;
is (i == 4 ) {
i++;
System.out.println(str);
}
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:
- 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.
- 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.
- 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.
- 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.
- 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:
- Decision Making at Java
- Support if-else statement with Examples
- Espresso if-else-if ladder including Examples
- Switch Statement in Java
- Break statement in Java
- return keyword in Java
Please Register for comment...