In this example, We will discuss about the use and implementation of the if-else statement in the java programs.
Code For if-else Statement
In this example, We will discuss about the use and implementation of the if-else statement in the java programs. The if-else statement is a decision making statement , If the Boolean expression evaluates true value then if block executed otherwise the else block is executed.
The syntax for if-else statement is as-
if(Boolean-Expression){ //Executes if Boolean expression is true } else{ //Executes if Boolean expression is false }
In the given example, We get the input an integer value at run time and checks it to be a positive number or not as folowing.
Example: IfElseExample.java
package com.devmanuals2; import java.io.*; public class IfElseExample { public static void main(String[] args) throws IOException { BufferedReader buffread = new BufferedReader(new InputStreamReader( System.in)); System.out.println("Enter integer number"); String str = buffread.readLine(); int number = Integer.parseInt(str); if (number < 0) { System.out.println("The number " + number + " is negative"); } else { System.out.println("The number " + number + " is positive"); } } }
Output:
Enter integer number 5412 The number 5412 is positive |
[ 0 ] Comments