You are on page 1of 7

La Martiniere College,

A
Lucknow
Digital Academic Services

Class: 8 Subject: Computer Studies

Name/Number of Chapter: Control Structures / 2

Module Number: 2 - Final

Learning Objectives of this Module:


1. This module would help in revising the concept of decision making and branching
statements.
2. This module contains illustrations of the branching statements such as if, if-else, ternary
operator and switch-case statement. The syntaxes of command and line input have been
discussed in this module.
Guidance Note:
1. Parents are requested to encourage the students to carefully read the content of the given
topic provided in this module so that they may understand what the module encapsulates.
2. The students must finally attempt the worksheet on their own. Parents must oversee this to
ensure that this system of education using the modern technology achieves its objective.
3. The student may be guided with his work wherever necessary but kept to a minimum.
The order of execution of the statements within a program changes only when there is any test
condition to be met. The use of decision making statements carry out this procedure as per the
requirement of the objective of the program.
Branching:
The process of repeating a group of statements under a certain given condition is called as
branching.
EXPLANATION:
The control of the flow of the program selects one of the two alternative paths based upon certain
specified condition in a program.
CONTENT:
Conditional Branching:
The flow of the data within a program based on a particular condition is termed as Conditional
branching.
EXPLANATION:
The normal flow of path is changed based upon a specific test condition in the process of conditional
branching.
CONTENT:
Unconditional Branching:
The change in the normal flow of path in the execution of the statements of a program without any
specified condition is called as an Unconditional branching.
EXPLANATION:
This branching procedure occurs without any specified condition. The coding of the program is made
to jump to another part of the program.

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

if(n%5==0) // ‘%’ symbol will check the divisibility by 5


{System.out.println("divisible by 5"); }
else{ System.out.println("not divisible by
5"); } OUTPUT: divisible by 5
CONTENT:
Conditional operator:
The ternary operator is also known as the conditional operator. This operator consists of three
operands and is used to evaluate boolean expressions. The goal of the operator is to decide, which
value should be assigned to the variable.
EXPLANATION:
A ternary operator evaluates the test condition and executes a block of code based on the result of
the condition. This statement helps in making two – way decisions. This operator is a combination
of ‘?’ and ‘:’.
It is used to perform the task of ‘if...else’ statement. The value after the ‘?’ is returned when the
condition is true and the value after the ‘:’ is returned in case of the test condition being false.
It's syntax is:
condition ? expression1 : expression2;
The ternary operator takes 3 operands (condition, expression1, and expression2). Hence, the name
ternary operator.
Example:
public static void
main(String[] args) { int
a=7;
//Using ternary operator
String output=(a%2==0)?"even
number":"odd number";
System.out.println(output); } }
OUTPUT: odd number
CONTENT:
Switch- case statement:
This statement is like if-else-if ladder statement. The use of a default label is optional.
EXPLANATION:
The switch expression is evaluated once. The value of the expression is compared with the
values of each case. If there is a match, the associated block of code is executed.
Example-

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]);

Syntax for Floating point value:


Float a= Float.parseFloat(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.

Q.3 Match the following:


COLUMN A COLUMN B
1. Three operands are used in a. for the false condition.
2. A default case can be used in b. the end of a particular case in a switch case
statement.
3. The ‘if-else’ statement has ‘else’ block c. a semicolon.
4. The break statement signals d. the conditional operator.
5. The statement ‘if’ does not end with e. the switch case statement.
Q.4 Short Answer Questions:
Q.1 What is a meant by branching in java programming?
Q.2 Briefly explain the use of the if statement in a java program?
Q.3 Which statement works like an ‘if-else’ statement in a java program?
Q.4 What is the utility of an if statement?
Q.5 Give the syntax for the following command line inputs in java -
(i) Syntax for the integer value.
(ii) Syntax for the float value.
Q.5 Long Answer Questions:
Q. 1 Write a java program to illustrate the use of ‘if-else’ statement.
Q. 2 Write a java program to display the sum of two given numbers.
Q.3 Write a java program to check if the given number is divisible by 2.
Q.4 Write a java program to demonstrate the use of a ternary operator.
Q.5 Write a java program to display the average of two given numbers.

You might also like