Professional Documents
Culture Documents
A
Lucknow
Digital Academic Services
CONTENT:
Decision making:
Java decision-making statements allow us to make a decision, based upon the result of a condition.
The statements like if, if-else, conditional operator and switch case carry out this procedure in a
program.
EXPLANATION:
Decision making structures have one or more conditions to be evaluated or tested by the program,
along with a statement or statements that are to be executed if the condition is determined to be true,
and optionally, other statements to be executed if the condition is determined to be false.
CONTENT:
if statement:
An if statement is a programming conditional statement that, if proved true, performs a function
or displays information.
EXPLANATION:
It is used to decide whether a certain statement or block of statements will be executed or not.
i. e. if a certain condition is true then a block of statement is executed otherwise not.
/* local variable
definition */ int p
= 100;
/* check the boolean condition using if
statement */ if( p< 200 ) {
/* if condition is true then print the
following */ printf("p is less than
200\n" );
}
OUTPUT: p is less than 200
CONTENT:
‘if-else’ statement:
This statement executes two different codes depending upon whether the test expression is true or
false.
The syntax ‘if’ specifies a block of code to be executed in case of the given condition is true and the
syntax ‘else’ specifies a block of code to be executed in case of the same given condition is false.
EXPLANATION:
The path of the program follows ‘if’ when a block of code is to be executed, and the given specified
condition is true. The path follows the syntax ‘else’ when the given condition is false.
Example-
//definin
ga
variable
int
n=20;
//Check if the number is divisible by 5 or not
class Main {
public static void
main(String[] args) { int
number = 44;
String size;
// switch statement
to check size switch
(number) {
case 29:
size
=
"S
mal
l";
bre
ak;
case 42:
size =
"Medi
um";
break;
// match the
value of week
case 44:
size
=
"La
rge
";
bre
ak;
case 48:
size =
"Extra
Large";
break;
default:
size =
"Unkno
wn";
break; }
System.out.println("Size: "
+ size); }} Output: Size:
Large
COMMAND AND LINE INPUT STATEMENTS IN JAVA:
CONTENT:
Syntax for String value :
String a= ar[0];
where ‘ar’ is the argument used in public static void main (String ar[0])
EXPLANATION:
There are two ways to create a String object:
By string literal : Java String literal is created by using double quotes. For Example: String
s=“Good”;
By new keyword : Java String is created by using a keyword “new”. For example: String s=new
String(“Good”);
Syntax for an Integer value:
int a = Integer.parseInt(ar[0]);
EXPLANATION:
A Java application can accept any number of arguments from the command line. This allows the
user to specify configuration information when the application is launched. When an application is
launched, the runtime system passes the command-line arguments to the application's main method
via an array of Strings.
Exercises for the Chapter Control Structures Module 2
Q. 1 Fill in the Blanks:
1. Branching based on a condition is called a branching.
2. When a specified condition is to be met then an statement is used for branching.
3. A operator can replace an if-else statement.
4. A operator is also known as the conditional operator.
5. In a switch case statement default is an case.
Q.2 State whether the following are True or False:
1. The break statement is compulsory in a switch case statement.
2. A conditional branching is not based on a condition.
3. The syntax for storing an integer value is ‘Integer.parseInt(ar[0])’.
4. The statement ‘if-else’ is a branching statement.
5. A ternary operator requires four operands.