If-else Statement
Java if statement is used to test the condition. It checks boolean condition: true or false.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
- //Java Program to demonstate the use of if statement.
- public class IfExample {
- public static void main(String[] args) {
- //defining an 'age' variable
- int age=20;
- //checking the age
- if(age>18){
- System.out.print("Age is greater than 18");
- }
- }
- }
Age is greater than 18
- //A Java Program to demonstrate the use of if-else statement.
- //It is a program of odd and even number.
- public class IfElseExample {
- public static void main(String[] args) {
- //defining a variable
- int number=13;
- //Check if the number is divisible by 2 or not
- if(number%2==0){
- System.out.println("even number");
- }else{
- System.out.println("odd number");
- }
- }
- }
odd number
- //Java Program to demonstrate the use of If else-if ladder.
- //It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.
- public class IfElseIfExample {
- public static void main(String[] args) {
- int marks=65;
- if(marks<50){
- System.out.println("fail");
- }
- else if(marks>=50 && marks<60){
- System.out.println("D grade");
- }
- else if(marks>=60 && marks<70){
- System.out.println("C grade");
- }
- else if(marks>=70 && marks<80){
- System.out.println("B grade");
- }
- else if(marks>=80 && marks<90){
- System.out.println("A grade");
- }else if(marks>=90 && marks<100){
- System.out.println("A+ grade");
- }else{
- System.out.println("Invalid!");
- }
- }
- }
C grade
- //Java Program to demonstrate the use of Nested If Statement.
- public class JavaNestedIfExample {
- public static void main(String[] args) {
- //Creating two variables for age and weight
- int age=20;
- int weight=80;
- //applying condition on age and weight
- if(age>=18){
- if(weight>50){
- System.out.println("You are eligible to donate blood");
- }
- }
- }}
You are eligible to donate blood
- //Java Program to demonstrate the use of Nested If Statement.
- public class JavaNestedIfExample2 {
- public static void main(String[] args) {
- //Creating two variables for age and weight
- int age=25;
- int weight=48;
- //applying condition on age and weight
- if(age>=18){
- if(weight>50){
- System.out.println("You are eligible to donate blood");
- } else{
- System.out.println("You are not eligible to donate blood");
- }
- } else{
- System.out.println("Age must be greater than 18");
- }
- } }
You are not eligible to donate blood
Switch Statement
Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you can use strings in the switch statement.Points to Remember
- There can be one or N number of case values for a switch expression.
- The case value must be of switch expression type only. The case value must be literal or constant. It doesn't allow variables.
- The case values must be unique. In case of duplicate value, it renders compile-time error.
- The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums and string.
- Each case statement can have a break statement which is optional. When control reaches to the break statement, it jumps the control after the switch expression. If a break statement is not found, it executes the next case.
- The case value can have a default label which is optional.
- public class SwitchExample {
- public static void main(String[] args) {
- //Declaring a variable for switch expression
- int number=20;
- //Switch expression
- switch(number){
- //Case statements
- case 10: System.out.println("10");
- break;
- case 20: System.out.println("20");
- break;
- case 30: System.out.println("30");
- break;
- //Default case statement
- default:System.out.println("Not in 10, 20 or 30");
- }
- }
- }
20
- //Java Program to demonstrate the example of Switch statement
- //where we are printing month name for the given number
- public class SwitchMonthExample {
- public static void main(String[] args) {
- //Specifying month number
- int month=7;
- String monthString="";
- //Switch statement
- switch(month){
- //case statements within the switch block
- case 1: monthString="1 - January";
- break;
- case 2: monthString="2 - February";
- break;
- case 3: monthString="3 - March";
- break;
- case 4: monthString="4 - April";
- break;
- case 5: monthString="5 - May";
- break;
- case 6: monthString="6 - June";
- break;
- case 7: monthString="7 - July";
- break;
- case 8: monthString="8 - August";
- break;
- case 9: monthString="9 - September";
- break;
- case 10: monthString="10 - October";
- break;
- case 11: monthString="11 - November";
- break;
- case 12: monthString="12 - December";
- break;
- default:System.out.println("Invalid Month!");
- }
- //Printing month of the given number
- System.out.println(monthString);
- }
- }
7 - July
- //Java Switch Example where we are omitting the
- //break statement
- public class SwitchExample2 {
- public static void main(String[] args) {
- int number=20;
- //switch expression with int value
- switch(number){
- //switch cases without break statements
- case 10: System.out.println("10");
- case 20: System.out.println("20");
- case 30: System.out.println("30");
- default:System.out.println("Not in 10, 20 or 30");
- }
- }
- }
20 30 Not in 10, 20 or 30
- //Java Program to demonstrate the use of Java Switch
- //statement with String
- public class SwitchStringExample {
- public static void main(String[] args) {
- //Declaring String variable
- String levelString="Expert";
- int level=0;
- //Using String in Switch expression
- switch(levelString){
- //Using String Literal in Switch case
- case "Beginner": level=1;
- break;
- case "Intermediate": level=2;
- break;
- case "Expert": level=3;
- break;
- default: level=0;
- break;
- }
- System.out.println("Your Level is: "+level);
- }
- }
Your Level is: 3
- //Java Program to demonstrate the use of Java Nested Switch
- public class NestedSwitchExample {
- public static void main(String args[])
- {
- //C - CSE, E - ECE, M - Mechanical
- char branch = 'C';
- int collegeYear = 4;
- switch( collegeYear )
- {
- case 1:
- System.out.println("English, Maths, Science");
- break;
- case 2:
- switch( branch )
- {
- case 'C':
- System.out.println("Operating System, Java, Data Structure");
- break;
- case 'E':
- System.out.println("Micro processors, Logic switching theory");
- break;
- case 'M':
- System.out.println("Drawing, Manufacturing Machines");
- break;
- }
- break;
- case 3:
- switch( branch )
- {
- case 'C':
- System.out.println("Computer Organization, MultiMedia");
- break;
- case 'E':
- System.out.println("Fundamentals of Logic Design, Microelectronics");
- break;
- case 'M':
- System.out.println("Internal Combustion Engines, Mechanical Vibration");
- break;
- }
- break;
- case 4:
- switch( branch )
- {
- case 'C':
- System.out.println("Data Communication and Networks, MultiMedia");
- break;
- case 'E':
- System.out.println("Embedded System, Image Processing");
- break;
- case 'M':
- System.out.println("Production Technology, Thermal Engineering");
- break;
- }
- break;
- }
- }
- }
Data Communication and Networks, MultiMedia
- //Java Program to demonstrate the use of Enum
- //in switch statement
- public class JavaSwitchEnumExample {
- public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
- public static void main(String args[])
- {
- Day[] DayNow = Day.values();
- for (Day Now : DayNow)
- {
- switch (Now)
- {
- case Sun:
- System.out.println("Sunday");
- break;
- case Mon:
- System.out.println("Monday");
- break;
- case Tue:
- System.out.println("Tuesday");
- break;
- case Wed:
- System.out.println("Wednesday");
- break;
- case Thu:
- System.out.println("Thursday");
- break;
- case Fri:
- System.out.println("Friday");
- break;
- case Sat:
- System.out.println("Saturday");
- break;
- }
- }
- }
- }
Sunday Monday Twesday Wednesday Thursday Friday Saturday
- //Java Program to demonstrate the use of Wrapper class
- //in switch statement
- public class WrapperInSwitchCaseExample {
- public static void main(String args[])
- {
- Integer age = 18;
- switch (age)
- {
- case (16):
- System.out.println("You are under 18.");
- break;
- case (18):
- System.out.println("You are eligible for vote.");
- break;
- case (65):
- System.out.println("You are senior citizen.");
- break;
- default:
- System.out.println("Please give the valid age.");
- break;
- }
- }
- }
You are eligible for vote.
Loops in Java
In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in java.
- for loop
- while loop
- do-while loop
- //Java Program to demonstrate the example of for loop
- //which prints table of 1
- public class ForExample {
- public static void main(String[] args) {
- //Code of Java for loop
- for(int i=1;i<=10;i++){
- System.out.println(i);
- }
- }
- }
1 2 3 4 5 6 7 8 9 10
- //Java For-each loop example which prints the
- //elements of the array
- public class ForEachExample {
- public static void main(String[] args) {
- //Declaring an array
- int arr[]={12,23,44,56,78};
- //Printing array using for-each loop
- for(int i:arr){
- System.out.println(i);
- }
- }
- }
12 23 44 56 78
- //A Java program to demonstrate the use of labeled for loop
- public class LabeledForExample {
- public static void main(String[] args) {
- //Using Label for outer and for loop
- aa:
- for(int i=1;i<=3;i++){
- bb:
- for(int j=1;j<=3;j++){
- if(i==2&&j==2){
- break aa;
- }
- System.out.println(i+" "+j);
- }
- }
- }
- }
1 1 1 2 1 3 2 1
If you use break bb;, it will break inner loop only which is the default behavior of any loop
- public class LabeledForExample2 {
- public static void main(String[] args) {
- aa:
- for(int i=1;i<=3;i++){
- bb:
- for(int j=1;j<=3;j++){
- if(i==2&&j==2){
- break bb;
- }
- System.out.println(i+" "+j);
- }
- }
- }
- }
1 1 1 2 1 3 2 1 3 1 3 2 3 3
Output:infinitive loop infinitive loop infinitive loop infinitive loop infinitive loop ctrl+cNow, you need to press ctrl+c to exit from the program.Java While Loop
Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.
- public class WhileExample {
- public static void main(String[] args) {
- int i=1;
- while(i<=10){
- System.out.println(i);
- i++;
- }
- }
- }
1 2 3 4 5 6 7 8 9 10If you pass true in the while loop, it will be infinitive while loop.
- public class WhileExample2 {
- public static void main(String[] args) {
- while(true){
- System.out.println("infinitive while loop");
- }
- }
- }
infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop ctrl+cNow, you need to press ctrl+c to exit from the programJava do-while Loop
Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.The Java do-while loop is executed at least once because condition is checked after loop body.
Syntax:
- do{
- //code to be executed
- }while(condition);
- public class DoWhileExample {
- public static void main(String[] args) {
- int i=1;
- do{
- System.out.println(i);
- i++;
- }while(i<=10);
- }
- }
1 2 3 4 5 6 7 8 9 10If you pass true in the do-while loop, it will be infinitive do-while loop.
Syntax:
- do{
- //code to be executed
- }while(true);
- public class DoWhileExample2 {
- public static void main(String[] args) {
- do{
- System.out.println("infinitive do while loop");
- }while(true);
- }
- }
infinitive do while loop infinitive do while loop infinitive do while loop ctrl+cNow, you need to press ctrl+c to exit from the program.Java Break Statement
When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.The Java break is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop.We can use Java break statement in all types of loops such as for loop, while loop and do-while loop.Syntax:
- //Java Program to demonstrate the use of break statement
- //inside the for loop.
- public class BreakExample {
- public static void main(String[] args) {
- //using for loop
- for(int i=1;i<=10;i++){
- if(i==5){
- //breaking the loop
- break;
- }
- System.out.println(i);
- }
- }
- }
1 2 3 4It breaks inner loop only if you use break statement inside the inner loop
- //Java Program to illustrate the use of break statement
- //inside an inner loop
- public class BreakExample2 {
- public static void main(String[] args) {
- //outer loop
- for(int i=1;i<=3;i++){
- //inner loop
- for(int j=1;j<=3;j++){
- if(i==2&&j==2){
- //using break statement inside the inner loop
- break;
- }
- System.out.println(i+" "+j);
- }
- }
- }
- }
1 1 1 2 1 3 2 1 3 1 3 2 3 3We can use break statement with a label. This feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is outer loop or inner.
- //Java Program to illustrate the use of continue statement
- //with label inside an inner loop to break outer loop
- public class BreakExample3 {
- public static void main(String[] args) {
- aa:
- for(int i=1;i<=3;i++){
- bb:
- for(int j=1;j<=3;j++){
- if(i==2&&j==2){
- //using break statement with label
- break aa;
- }
- System.out.println(i+" "+j);
- }
- }
- }
- }
1 1 1 2 1 3 2 1
- /Java Program to demonstrate the use of break statement
- //inside the while loop.
- public class BreakWhileExample {
- public static void main(String[] args) {
- //while loop
- int i=1;
- while(i<=10){
- if(i==5){
- //using break statement
- i++;
- break;//it will break the loop
- }
- System.out.println(i);
- i++;
- }
- }
- }
1 2 3 4
- //Java Program to demonstrate the use of break statement
- //inside the Java do-while loop.
- public class BreakDoWhileExample {
- public static void main(String[] args) {
- //declaring variable
- int i=1;
- //do-while loop
- do{
- if(i==5){
- //using break statement
- i++;
- break;//it will break the loop
- }
- System.out.println(i);
- i++;
- }while(i<=10);
- }
- }
1 2 3 4Java Break Statement with Switch
To understand the example of break with switch statement, please visit here: Java Switch Statement.Java Continue Statement???
No comments:
Post a Comment