Control Statements counter controls and sentinel control loop

MsPariyalNituLaxman 18 views 58 slides Sep 30, 2024
Slide 1
Slide 1 of 58
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58

About This Presentation

Control Statements in java with Examples


Slide Content

Chapter 5

2Organization of Programming Languages-Cheng (Fall 2004)
Contents

3Organization of Programming Languages-Cheng (Fall 2004)

4Organization of Programming Languages-Cheng (Fall 2004)

5Organization of Programming Languages-Cheng (Fall 2004)
output

6Organization of Programming Languages-Cheng (Fall 2004)

7Organization of Programming Languages-Cheng (Fall 2004)

8Organization of Programming Languages-Cheng (Fall 2004)

9Organization of Programming Languages-Cheng (Fall 2004)

10Organization of Programming Languages-Cheng (Fall 2004)

11Organization of Programming Languages-Cheng (Fall 2004)

12Organization of Programming Languages-Cheng (Fall 2004)

13Organization of Programming Languages-Cheng (Fall 2004)

14Organization of Programming Languages-Cheng (Fall 2004)

15Organization of Programming Languages-Cheng (Fall 2004)

16Organization of Programming Languages-Cheng (Fall 2004)

17Organization of Programming Languages-Cheng (Fall 2004)

18Organization of Programming Languages-Cheng (Fall 2004)

19Organization of Programming Languages-Cheng (Fall 2004)

20Organization of Programming Languages-Cheng (Fall 2004)

21Organization of Programming Languages-Cheng (Fall 2004)

22Organization of Programming Languages-Cheng (Fall 2004)
switch (variable or an integer expression)
{
case constant: //Java code ;
case constant: //Java code ;
default: //Java code ;
}

23Organization of Programming Languages-Cheng (Fall 2004)
public class SwitchCaseExample2
{
public static void main(String args[])
{
int i=2;
switch(i)
{
case 1:
System.out.println("Case1 ");
case 2:
System.out.println("Case2 ");
case 3:
System.out.println("Case3 ");
case 4:
System.out.println("Case4 ");
default:
System.out.println("Default "); } } }

24Organization of Programming Languages-Cheng (Fall 2004)
Output:
Case2
Case3
Case4
Default

25Organization of Programming Languages-Cheng (Fall 2004)
public class SwitchCaseExample2
{ public static void main(String args[])
{ int i=2;
switch(i) {
case 1:
System.out.println("Case1 ");
break;
case 2:
System.out.println("Case2 ");
break;
case 3:
System.out.println("Case3 ");
break;
case 4: System.out.println("Case4 ");
break;
default: System.out.println("Default "); } } }
Output:
Case2

26Organization of Programming Languages-Cheng (Fall 2004)

27Organization of Programming Languages-Cheng (Fall 2004)

28Organization of Programming Languages-Cheng (Fall 2004)

29Organization of Programming Languages-Cheng (Fall 2004)

30Organization of Programming Languages-Cheng (Fall 2004)

31Organization of Programming Languages-Cheng (Fall 2004)

32Organization of Programming Languages-Cheng (Fall 2004)

33Organization of Programming Languages-Cheng (Fall 2004)

34Organization of Programming Languages-Cheng (Fall 2004)

35Organization of Programming Languages-Cheng (Fall 2004)

36Organization of Programming Languages-Cheng (Fall 2004)

37Organization of Programming Languages-Cheng (Fall 2004)

38Organization of Programming Languages-Cheng (Fall 2004)

39Organization of Programming Languages-Cheng (Fall 2004)

40Organization of Programming Languages-Cheng (Fall 2004)

41Organization of Programming Languages-Cheng (Fall 2004)

42Organization of Programming Languages-Cheng (Fall 2004)

43Organization of Programming Languages-Cheng (Fall 2004)

44Organization of Programming Languages-Cheng (Fall 2004)

45Organization of Programming Languages-Cheng (Fall 2004)

46Organization of Programming Languages-Cheng (Fall 2004)
static Methods, static Fields and Class Math
To execute any method, that method should be called/invoked.
There are two ways to invoke any method:
By using the object of a class of that mehod
By using the class-name of that method  making/declaring the method
static.
It’s common for classes to contain convenient static methods to perform
common tasks.
Static method is invoked as follows:
ClassName.methodName (arguments)
We use various Math class methods here to present the concept of static
methods. Class Math provides a collection of methods that enable you to
perform common mathematical calculations. For example, you can
calculate the square root of 900.0 with the static method call
Math.sqrt(900.0)

47Organization of Programming Languages-Cheng (Fall 2004)
Note  Class Math is part of the java.lang package, which is
implicitly imported by the compiler, so it’s not necessary to import
class Math to use its methods.
Math Class Constants PI and E
Class Math declares two fields that represent commonly used
mathematical constants — Math.PI and Math.E.
Math.PI (3.141592653589793) is the ratio of a circle’s
circumference to its diameter.
Math.E (2.718281828459045) is the base value for natural
logarithms (calculated with static Math method log).
These fields are declared in class Math with the modifiers
public, final and static.

48Organization of Programming Languages-Cheng (Fall 2004)

49Organization of Programming Languages-Cheng (Fall 2004)
Why Is Method main Declared static?
When you execute the Java Virtual Machine (JVM) with the java
command, the JVM attempts to invoke the main method of the class.
when no objects of the class have been created.
Declaring main as static allows the JVM to invoke main without
creating an instance of the class.
When you execute your program, specify its class name as an
argument to the command java, as in
java ClassName
The JVM loads the class specified by ClassName and uses that class
name to invoke method main.
In the preceding command, ClassName is a command-line
argument to the JVM

50Organization of Programming Languages-Cheng (Fall 2004)
The keyword static enables the main method (another static
method) to call maximum without qualifying the method name
with the class name MaximumFinder
static methods in the same class can call each other directly.
Any other class that uses maximum must fully qualify the
method name with the class name.

51Organization of Programming Languages-Cheng (Fall 2004)
Declaring Methods with
Multiple Parameters
import java.util.Scanner;
public class MaximumFinder {
public static void main (String[] mohseen) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 3 floating-point values separated by spaces: " );
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double result = maximum(number1, number2, number3);
System.out.println("Maximum is: " + result);
}
public static double maximum(double x, double y, double z) {
double maximumValue = x;
if (y > maximumValue)
maximumValue = y;
if (z > maximumValue)
maximumValue = z;
return maximumValue;
}
}

52Organization of Programming Languages-Cheng (Fall 2004)
Using max() method of built-in Math class
import java.util.Scanner;
public class MaximumFinder1 {
public static void main (String[] mohseen) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three floating-point values separated by spaces: " );
double n1= input.nextDouble();
double n2= input.nextDouble();
double n3= input.nextDouble();
double result = Math.max(n1, Math.max(n2,n3));
System.out.println("Maximum is: " + result);
}
}

53Organization of Programming Languages-Cheng (Fall 2004)
Scope of Declarations
The basic scope rules are as follows:
1.The scope of a parameter declaration is the body of the method in which
the declaration appears.
2.The scope of a local-variable declaration is from the point at which the
declaration appears to the end of that block.
3.The scope of a local-variable declaration that appears in the initialization
section of a for statement’s header is the body of the for statement and
the other expressions in the header.
4.A method or field’s scope is the entire body of the class. This enables
non-static methods of a class to use the fields and other methods of the
class.
shadowing  If a local variable or parameter in a method has the same
name as a field/variale of the class, the field/variable is “hidden” until the
block terminates execution—this is called shadowing.

54Organization of Programming Languages-Cheng (Fall 2004)
public class Scope {
private static int x = 1;
public static void main(String[] mohseen) {
int x = 5;
System.out.println("local x in main is " + x);
useLocalVariable();
useField();
useLocalVariable();
useField();
System.out.println("\nlocal x in main is " + x);
}
public static void useLocalVariable() {
int x = 25;
System.out.println(“\nlocal x on entering method useLocalVariable is" + x);
++x;
System.out.println("local x before exiting method useLocalVariable is" +x);
}
public static void useField() {
System.out.println("\nfield x on entering method useField is " + x);
x *= 10;
System.out.println("field x before exiting method useField is " + x);
}
}

55Organization of Programming Languages-Cheng (Fall 2004)
public static void useField() {
System.out.println("\nfield x on entering method useField is " + x);
x *= 10;
System.out.println("field x before exiting method useField is " + x);
}
}

56Organization of Programming Languages-Cheng (Fall 2004)

57Organization of Programming Languages-Cheng (Fall 2004)
Method Overloading
Methods of the same name can be declared in the same class, as long as they
have different sets of parameters (determined by the number, types and order
of the parameters) is called method overloading.
When an overloaded method is called, the compiler selects the appropriate
method by examining the number, types and order of the arguments in the call.
Method overloading is commonly used to create several methods with the same
name that perform the same or similar tasks, but on different types or different
numbers of arguments.
For example, Math methods abs, min and max are overloaded with four
versions each:
1. One with two double parameters.
2. One with two float parameters.
3. One with two int parameters.
4. One with two long parameters.

58Organization of Programming Languages-Cheng (Fall 2004)
public class MethodOverload {
public static void main(String[] args) {
System.out.println("Square of integer 7 is " + square(7));
System.out.println("Square of double 7.5 is " + square(7.5));
}
public static int square(int intVal) {
System.out.println("\nCalled square with int argument: " + intVal);
return intVal * intVal;
}
public static double square(double doubleVal) {
System.out.println("Called square with double argument:”+ doubleVal);
return doubleVal * doubleVal;
}
}