0% found this document useful (0 votes)
40 views60 pages

MOD1 PPT - Chap 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views60 pages

MOD1 PPT - Chap 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

Module 2

Control Statements
Java’s program control statements can be put into the
following categories:
selection, iteration, and jump.
• Selection statements allow your program to choose
different paths of execution based upon the outcome
of an expression or the state of a variable(if,if-else,if,-
else-if ladder.
• Iteration statements enable program execution to
repeat one or more statements (that is, iteration
statements form loops)while,do-while,for,nested loop.
• Jump statements allow your program to execute in a
non-linear fashion(break,continue,return).
Java’s Selection Statements
• Java supports two selection statements: if and
switch.
If: Switch
• Simple if Nested switch
• If else
• Nested if
• If else if laddder
The if statement
• The if statement executes a block of code only
if the specified expression is true.
• If the value is false, then the if block is skipped
and execution continues with the rest of the
program.
• You can either have a single statement or a
block of code within an if statement.
Note that the conditional expression must be a
Boolean expression.
Example: public class Example
{
public static void main(String[] args)
{
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
if (a < b)
System.out.println("b > a");
}
}
The if else statement
• The if statement is Java’s conditional branch statement.
• It can be used to route program execution through two
different paths.
• Here is the general form of the if statement
• Here, each statement may be a single
statement or a compound statement enclosed
in curly braces (that is, a block).
• The condition is any expression that returns a
Boolean value.
• The if works like this: If the condition is true,
then statement1 is executed. Otherwise,
statement2 (if it exists) is executed.
Example: public class Example
{
public static void main(String[] args)
{
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
Else
System.out.println("b > a");
}
}
Nested if
• A nested if is an if statement that is the target
of another if or else.
• When you nest ifs, the main thing to
remember is that an else statement always
refers to the nearest if statement that is
within the same block as the else and that is
not already associated with an else.
• One if statement worked within another if
statement
Syntax:
Here is an example:
class example
{
public static void main(String args[])
{
int a=10;
int b=20;

if(a==10){
if(b==20){
System.out.println(“nested if statement");
}
}
}
}
The if-else-if Ladder
• A common programming construct that is based upon a
sequence of nested if’s is the if-else-if ladder.
Syntax:
• The if statements are executed from the top
down.
• As soon as one of the conditions controlling
the if is true, the statement associated with
that if is executed, and the rest of the ladder is
bypassed.
• If none of the conditions is true, then the final
else statement will be executed.
// Java program to illustrate if-else-if
ladder
// condition 3
import java.io.*;
else if (i == 20)
class sample
{
System.out.println("i is 20\n");
public static void main(String[] args) else
{ System.out.println("i is not
// initializing expression present\n");
int i = 20; System.out.println("Outside if-
else-if");
// condition 1 }
if (i == 10) }
System.out.println("i is 10\n");

// condition 2
else if (i == 15)
System.out.println("i is 15\n");
The switch statement
• The switch case statement is a multi-way branch with several
choices. A switch is easier to implement than a series of if/else
statements.
• Switch statement executes one statement from multiple
conditions

Structure of Switch:
• The switch statement begins with a keyword, followed by an
expression.
• Following the controlling expression is a code block that
contains zero or more labeled cases.
• Each label must equate to an integer constant and each must
be unique.
syntax
ex

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(yes,"10");
break;
case 20: System.out.println(“yes,20");
break;
case 30: System.out.println(“yes,30");
break;
//Default case statement
default:System.out.println("Not in 10, 20 or 30");
}
}
}
• o/p: yes 20
// case 6: monthString="6 - June";
Java Program to demonstrate the example break;
of Switch statement
case 7: monthString="7 - July";
//
break;
where we are printing month name for th
e given number case 8: monthString="8 - August";
public class SwitchMonthExample { break;
public static void main(String[] args) { case 9: monthString="9 - September";
//Specifying month number break;
int month=7; case 10: monthString="10 - October";
String monthString=""; // break;
Switch statement case 11: monthString="11 - November";
switch(month){
//case statements within the switch block break;
case 1: monthString="1 - January"; case 12: monthString="12 - December";
break;
case 2: monthString="2 - February"; break;
break; default:System.out.println("Invalid Mont
case 3: monthString="3 - March"; h!");
break; }
case 4: monthString="4 - April"; //Printing month of the given number
break; System.out.println(monthString);
case 5: monthString="5 - May"; }
• o/p:7 - July
• The break statement is optional. If you omit
the break, execution will continue on into the
next case.
• It is sometimes desirable to have multiple
cases without break statements between
them.
// In a switch, break case 5:
statements are optional. case 6:
class MissingBreak { case 7:
public static void main(String case 8:
args[]) { for(int i=0; i<12; case 9:
i++) switch(i) {
case 0:
System.out.println("i is less
case 1: than 10"); break;
case 2: default:
case 3: System.out.println("i is 10 or
more");
case 4:
}
System.out.println("i is less
}
than 5"); break;
}
Iteration Statements
The while loop
The while statement is a looping construct control
statement that executes a block of code while a
condition is true.
You can either have a single statement or a block of
code within the while loop.
The loop will never be executed if the testing
expression evaluates to false.
The loop condition must be a Boolean expression.
Example:
public class Example {
public static void main(String[] args) {
int count = 1;
System.out.println("Printing Numbers from 1 to 10");
while (count <= 10) {
System.out.println(count++);
}
}
}
The do-while loop
• The do-while loop is similar to the while loop,
except that the test is performed at the end of
the loop instead of at the beginning.
• This ensures that the loop will be executed at
least once.
ex

public class DoWhileExample {


public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}
The for loop
The for loop is a looping construct which can
execute a set of instructions a specified
number of times. It’s a counter controlled
loop.
Example:
public class Example
{
public static void main(String[] args)
{
System.out.println("Printing Numbers from 1 to 10");
for (int count = 1; count <= 10; count++)
{
System.out.println(count);
}
}
}
For each
• For-each is another array traversing technique like
for loop, while loop, do-while loop
• It starts with the keyword for like a normal for-loop.
• Instead of declaring and initializing a loop counter
variable, you declare a variable that is the same type
as the base type of the array, followed by a colon,
which is then followed by the array name.
• In the loop body, you can use the loop variable you
created rather than using an indexed array element.
• It’s commonly used to iterate over an array or a
Collections class (eg, ArrayList)
Syntax
for (type var : array)
{
statements using var;
}
/*package whatever //do not write package name
here */
import java.io.*;
class foreach
{
public static void main(String[] args)
{
// array declaration
int ar[] = { 10, 50, 60, 80, 90 };
for (int element : ar)
System.out.print(element + " ");
}
Output:
10 50 60 80 90
Jump Statements
Java supports three jump statements: break,
continue, and return.
These statements transfer control to another
part of your program.
The break statement
• You use a break statement when you want to jump
immediately to the statement following the
enclosing control structure.
• You can also provide a loop with a label, and then
use the label in your break statement.
• The break statement is used to terminate the loop
immediately.
• break keyword is used to indicate break statements
in java programming.
• The break statement terminates the whole loop
early.
Example for break:
public class Example
{
public static void main(String[] args)
{
System.out.println("Numbers 1 - 10");
for (int i = 1; i < 10; ++i)
{
if (i == 8)
break;
System.out.println(i + "\n");
}
}
}
output
1
2
3
4
5
6
7
The continue statement
• You use a continue statement when you do not want
to execute the remaining statements in the loop, but
you do not want to exit the loop itself.
• You can also provide a loop with a label and then use
the label in your continue statement.
• The continue statement is used to skip the current
iteration of the loop.
• continue keyword is used to indicate continue
statement in java programming.
• The continue statement brings the next iteration early.
Example for continue:
public class Example {
public static void main(String[] args)
{
System.out.println("Odd Numbers");
for (int i = 1; i <= 10; ++i)
{
if (i==5)
continue;
System.out.println(i + "\n");
}
}
}
output
0
1
2
3
4
6
7
8
9
10
The return statement
• The return keyword finishes the execution of a method,
and can be used to return a value from a method.
• used for returning a value when the execution of the
block is completed..
Example:
public class Example {
public static void main(String[] args) {
int res = sum(10, 20);
System.out.println(res);
}
private static int sum(int a, int b) {
return (a + b);
}
}
output
• 30
Conditional Operator in Java
• In Java, conditional operators check the condition
and decides the desired result on the basis of
both conditions.
• In this section, we will discuss the conditional
operator in Java.
There are three types of the conditional operator
• Conditional AND
• Conditional OR
• Ternary Operator
Conditional AND
• The operator is applied between two Boolean
expressions. It is denoted by the two AND
operators (&&).
• It returns true if and only if both expressions
are true, else returns false.

• Truth table:
Conditional OR
• The operator is applied between two Boolean
expressions. It is denoted by the two OR
operator (||).
• It returns true if any of the expression is true,
else returns false.
• Truth table:
example
public class ConditionalOperatorExample
{
public static void main(String args[])
{
int x=5, y=4, z=7;
System.out.println(x>y && x>z || y<z);
System.out.println((x<z || y>z) && x<y);
}
}
output
• true
• false
Ternary Operator
• The meaning of ternary is composed of three
parts.
• The ternary operator (? :) consists of three
operands. It is used to evaluate Boolean
expressions.
• Java includes a special ternary (three way)
operator that can replace certain types of if-
then-else statements.
• The ? has this general form:

expression1 ? expression2 : expression3


• Here, expression1 can be any expression that
evaluates to a boolean value.
• If expression1 is true, then expression2 is
evaluated;
• otherwise, expression3 is evaluated.
example,
int a=10, b=2, c ;
……….
c= (a>b)?a:b;

Output:?

You might also like