Oop’s Using java


The syntax to use the switch The syntax to use the switch switch(argument){


Download 0.74 Mb.
bet4/12
Sana15.11.2023
Hajmi0.74 Mb.
#1775298
1   2   3   4   5   6   7   8   9   ...   12
Bog'liq
OOP’s Using JAVA Module 2

The syntax to use the switch

  • The syntax to use the switch
  • switch(argument){

    case value1: Statements;

     break; //optional

    case value2: Statements;

     break; //optional

    ......

    default:

    code to be executed if all cases are not matched;

    }

Example of “Switch” statement Program 5

EX:

class SwitchExample {

public static void main(String[] args) {

//Declaring a variable for switch expression

int a=20;

//Switch expression

switch(a){

//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");

}

}

}

EX:

class SwitchExample2 {

public static void main(String[] args) {

int a=20;

//switch expression with int value

switch(a){

//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");

}

}

}

Loop Statements

Loop Statements for

  • In Java, for loop is similar to C and C++
  • It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code.
  • We use the for loop only when we exactly know the number of times, we want to execute the block of code.
  • Syntax

  • Download 0.74 Mb.

    Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   12




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling