0% found this document useful (0 votes)
17 views

Conditional Logic and Block Statements Slides

The document discusses different types of conditional statements in Java including if-else statements, if-else if-else statements, logical operators, the ternary operator, and switch statements. It provides code examples to demonstrate how to use each statement and operator to evaluate conditions and execute different blocks of code based on the condition being true or false.

Uploaded by

Gayathri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Conditional Logic and Block Statements Slides

The document discusses different types of conditional statements in Java including if-else statements, if-else if-else statements, logical operators, the ternary operator, and switch statements. It provides code examples to demonstrate how to use each statement and operator to evaluate conditions and execute different blocks of code based on the condition being true or false.

Uploaded by

Gayathri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

-

-
int value1 = 7;

int value2 = 5;

int maxValue = value1 > value2 ? value1 : value2 ;

System.out.println(maxValue); // displays 7

result = condition ? true-value : false-value ;


int value1 = 10;

int value2 = 4;

if (value1 > value2)

System.out.println(“value 1 is bigger”);

else

System.out.println(“value 1 is not bigger”);

if ( condition )
true-statement ;
else
- false-statement ;
-
-

if ( condition-1 )
true-statement-1 ;
else if ( condition-2 )
true-statement-2 ;
.
.
.
else if ( condition-N )
true-statement-N ;
else
false-statement ;
int value1 = 10;

int value2 = 40;

if (value1 > value2)

System.out.println(“value 1 is bigger”);

else if (value1 < value2)

System.out.println(“value 2 is bigger”);

else

System.out.println(“value 1 and value 2 are equal”);


-

-
-
int a = 20, b = 14, c = 5;

true

true true

if ( a > b & b > c )

System.out.println(“a is greater than c”);


boolean done = false;

true

false

if ( ! done )

System.out.println(“Keep going!”);
-
-
-
-
-

{
statement-1;
statement-2;
.
.
.
statement-N;
}
int v1 = 10, v2 = 4;

final int diff;

if (v1 > v2) {

diff = v1 – v2;

System.out.println(“v1 is bigger than v2, diff = “ + diff);


}

else {

diff = v2 – v1;

System.out.println(“v1 is not bigger than v2, diff = “ + diff);


}
-

-
-
double students = 30.0d, rooms = 4.0d;

if(rooms > 0.0d) {

System.out.println(students);

System.out.println(rooms);

double avg = students / rooms

System.out.println(avg);

System.out.println(avg);
-
-

switch (value-to-test) {
case matching-value-1:
statements
break;
.
.
.
case matching-value-N:
statements
break;
default:
statements
}
-

-
-
-
-

-
-
-
-

-
-
-

You might also like