Control Statement in Java.
100% Working Code and Projects.
if you need any help about project and Application Contact Shelly at:+917888361589.
Control Statements :-
Control Statements are the statements which are used to take some decision by the machine. Moreover it take decision that whether the code or statements should be executed or not.
1) if statement :-
In this, the code inside the if statement will be executed only when the condition is true.
Syntax :
if(condition){
//code to be executed
}
//code to be executed
}
See the code below :-
Java Code :-
}
package CodingStartupAbheer;
import java.util.Scanner;
public class IfStatement {
public static void main(String[] Abheer) {
Scanner Abheer1 = new Scanner(System.in);
System.out.println("Enter any number ");
int a = Abheer1.nextInt();
int b = Abheer1.nextInt();
if (a > b) {
System.out.println("A is larger");
}
}
}import java.util.Scanner;
public class IfStatement {
public static void main(String[] Abheer) {
Scanner Abheer1 = new Scanner(System.in);
System.out.println("Enter any number ");
int a = Abheer1.nextInt();
int b = Abheer1.nextInt();
if (a > b) {
System.out.println("A is larger");
}
Output 1 :-
Enter any number
9
Enter any number
2
A is larger
Output 2 :-
Enter any number
1
Enter any number
5
Explanation :-
In first output, first number is larger than second number so that's why it gives the answer that A is larger. But in second output it does not give any output because condition is false that's why it does not print any output. To overcome this , if else is created.
2) if-else statement :-
In this , if the condition is true then code which is inside the If column will be executed otherwise else part will be executed.
Syntax :-
if(condition){
//code if condition is true
}else{
//code if condition is false
}
//code if condition is true
}else{
//code if condition is false
}
Java Code :-
package CodingStartupAbheer;
import java.util.Scanner;
public class IfStatement {
public static void main(String[] Abheer) {
Scanner Abheer1 = new Scanner(System.in);
System.out.println("Enter any number ");
int a = Abheer1.nextInt();
System.out.println("Enter any number ");
int b = Abheer1.nextInt();
if (a > b) {
System.out.println("A is larger");
} else {
System.out.println("B is larger");
}
}
}
Output 1:-
Enter any number
12
Enter any number
34
B is larger
Output 2:-
Enter any number
45
Enter any number
12
A is larger
0 Comments:
Post a Comment