If Statements in Java Decision Making
Decision
making means make a suitable decision and execute that particular part of the
program, so there is use True/False to getting decision. If condition true, If body will be executed otherwise else body will be executed. If Statements in Java
IF
statements
When you
want to execute the particular code then you can use IF statement, is true if statement? then block of if statements will execute.
Syntax
If (some conditions) {
Statements
Statements
}
Example
package com.demo;
public class If Demo {
public stactic void main(String[] args) {
int age=19;
if (age>18){
System .out. println("You well come");
}
}
}
IF Else statements
If “if
statement” is true then if statement block will execute otherwise else block
will execute
Syntax
If (some condition) {
Statements
} else {
Statements
}
Example
public class IfDemo{
public stactic void main(String[] args) {
int age=17;
if (age>18){
System.out.println("You well come");
}else {
system.out.println("sorry you are child");
}
}
}
IF ElseIf
Else If Statements in Java
It contains
multiple if conditions, if any if statement is true the if that respected if
block will execute otherwise else block will execute.
Syntax
If (some condition) {
Statements
} elseIf (some condition) {
Statements
} else {
Statements
}
Example
System.out.println("hello,you are younger");
Switch
Statement
Switch
statement is also used for decision making. It works same like “If Elseif Else”
statement. In switch statement multiple case are used, if any case match
with user input then that matched case will be execute. If none of case will
match then default (keyword) will execute. Default work like else block. If Statements in Java
Syntax:
Switch (input) {
Case n 1:
Statements;
break;
Case n 2:
Statements;
break;
Case n 3:
Statements;
break;
. . . . . .
default:
Statements;
}
Example
No comments:
Post a Comment