Thursday, November 4, 2021

# 16. control statements

 Control statements:

In java, by using control statements we can tell the flow of execution. in which order the statements should execute.

different types of control statements:

1. selection statements

    1.1. if

    1.2. if else

    1.3. nested if if else

    1.4. switch

2. looping/repetitive/iterative statements

    2.1 while

    2.2 do while

    2.3 for

    2.4 nested loops

    2.5 for each

3. transfer statements

    3.1 break

    3.2 continue

    3.3 throw

    3.4 throws

    3.5 finally

;;;;;;;;;etc


if (condition)

{

;;;;;

}

;;;;;;;;

if the condition is true it will execute the statements under the condition

if the condition is false it will execute the statements outside this 

for if the condition should be always boolean


if (condition)

{

;;;;;  //many statements with open braces

// without open braces we can take only one statement

// curly braces are optional

}

else

{

;;;;;;;;

}


package com.rameshsoft.automation.corejava;

public class SelectionStmtsDemo{

public static void main(String [] args){

int a1=45,a2=45;

if(a1<a2){

 System.out.println("Equl");

}

else{

System.out.println("noteql");

}

System.out.println(




Relational operatiors:

>

<

>=

<=

==

!=

nested if:

If inside another if called nested if

if (condition)

{

;;;;;;;;;;;;;;;

if (condition)

{

;;;;;;;;;;;;;;;;;;;;;;

}

else

{

;;;;;;;;;

}

}

else

{

;;;;;;;;;;;;;;;;;;;;;;;;;;

}


Switch:

to specify multiple conditions and to execute them based on choice-based or option based then we can go for the switch statement.

Syntax:

switch is a predefined keyword to convey it as a switch, as a part of switch condition we can specify byte, short, int, long, enums ,Strings otherwise we will get compile time error

every switch must and should open and close

case is a fixec keyword inorder to convey it as a keyword

case name is always should be of condition type

each case contain some statements

break 

All cases are going to be executed based on condition value

if no case is matched the default case is going to be executed


switch(condition)

{

case casename: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

              break;

case casename: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

              break;

case casename: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

              break;

;;;;;;;;;;;;;;;;;;;;;;;

default: ;;;;;;;;;;;;;;;;

                       break;

}


String browser="chrome";


switch(browser)

{

case "ie": System.out.println("IE browser");

        break;

case "chrome":System.out.println("chrome browser");

        break';

case "edge": System.out.println(edge browser");

        break;

case "firefox":System.out.println("firefox browser");

        break';

default: System.out.println("no case matched");

        break;

}

}

}


duplicate case names are not allowed


No comments:

Post a Comment

# 16. control statements

 Control statements: In java, by using control statements we can tell the flow of execution. in which order the statements should execute. d...