0% found this document useful (0 votes)
38 views19 pages

Java Control Structures Explained

Chapter 6 covers decision control structures in Java, including selection statements, looping control structures, and the flow of control. It provides fill-in-the-blank questions, true/false statements, application-based questions, multiple-choice questions, and programming exercises to reinforce understanding. Key concepts include if statements, for and while loops, switch statements, and the importance of break and default statements.

Uploaded by

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

Java Control Structures Explained

Chapter 6 covers decision control structures in Java, including selection statements, looping control structures, and the flow of control. It provides fill-in-the-blank questions, true/false statements, application-based questions, multiple-choice questions, and programming exercises to reinforce understanding. Key concepts include if statements, for and while loops, switch statements, and the importance of break and default statements.

Uploaded by

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

Chapter 6

Decision Control Structure


Class 8 - Logix Kips ICSE Computer with BlueJ

Fill in the blanks

Question 1

The Selection statements cause the program control to be transferred to a specific location
depending upon the outcome of the conditional expression.

Question 2

The Step value determines the increment or decrement of the loop control variable unless the
test condition becomes false.

Question 3

Java provides three basic looping control structures.

Question 4

The for loop is also called an Entry controlled loop.

Question 5

The do while first executes the block of statements and then checks the condition.

Question 6

The order in which statements are executed in a running program is called the Flow of
control.
State True or False

Question 1

The break statement takes the flow of control out of the switch statement.
True

Question 2

To execute the while loop, the condition must be false in the beginning.
False

Question 3

Default is the first statement of the switch case.


False

Question 4

While writing programs, the statements should be indented properly for better readability.
True

Question 5

The number of iteration refers to the number of times the condition is met.
True
Application Based Questions

Question 1

Radhika is writing a program in Java to print her name 10 times. Suggest her the appropriate
condition for the same.

Answer

Radhika can print her name using a for loop in Java like this:

for (int i = 1; i <= 10; i++) {


[Link]("Radhika");
}

Question 2

Naina has written a program in which she has entered one number. She wants to check
whether the entered number is divisible by 2 or not. Suggest her the appropriate condition for
the same.

Answer

Naina can use the below if condition:

if (number % 2 == 0)
[Link]("Number is divisible by 2");
else
[Link]("Number is not divisible by 2");
Multiple Choice Questions

Question 1

The .......... is a logical situation where either of the two actions are to be performed
depending on certain condition.

1. if else ✓
2. if
3. if else if

Question 2

Name the expression that is used to initialize a loop variable.

1. Initialization expression ✓
2. Step Value
3. Control Variable

Question 3

The unusual execution of more than one case at a time is termed as

1. Infinite loop
2. Time delay
3. Fall through ✓

Question 4

How many times the following body of loop will execute?

for(int a = 1; a <= 10; a++)


{
[Link](a);
}

1. 0
2. 9
3. 10 ✓

Question 5

How many times the following message will be printed?


do
{
[Link]("Hello");
}
ch++;
while(ch <= 1);

1. 1
2. Error in Code ✓
3. 2

Question 6

If none of the case matches, the compiler executes the statements written in the .......... case.

1. for
2. default ✓
3. break

Question 7

Which control structure is used when there is a requirement to check multiple conditions in a
program?

1. if else
2. switch ✓
3. for
Answer the following

Question 1

Explain the use of if control structure.

Answer

The if control structure is used to execute a statement or block of statements if the condition
is true, otherwise it ignores the condition. The general syntax of if statement is as follows:

if (Boolean expression)
{
//Java statements
..
..
}

Question 2

What are the unique features of for loop?

Answer

for loop is an entry-controlled loop. Its general syntax is as follows:

for (initialization; conditional; increment/decrement)


{
//Java Statements
..
..
}

1. The initialization expression initializes the loop control variable and is executed
only once when the loop starts. It is optional and can be omitted by just putting a
semicolon.
2. The conditional expression is tested at the start of each iteration of the loop.
Loop will iterate as long as this condition remains true.
3. The increment/decrement expression updates the loop control variable after
each iteration.
4. The body of the loop that consists of the statements that needs to be repeatedly
executed.

Question 3

Explain the switch statement with an example.

Answer
switch statement in a program, is used for multi-way branch. It compares its expression to
multiple case values for equality and executes the case whose value is equal to the expression
of switch. If none of the cases match, default case is executed. If default case is absent and no
case values match then none of the statements from switch are executed. The below program
makes use of the switch statement to print the value of a number if the number is 0, 1 or 2:

class SwitchExample {
public static void demoSwitch(int number) {
switch (number) {

case 0:
[Link]("Value of number is zero");
break;

case 1:
[Link]("Value of number is one");
break;

case 2:
[Link]("Value of number is two");
break;

default:
[Link]("Value of number is greater than
two");
break;

}
}
}

Question 4

What is the importance of break and default statements in switch?

Answer

1. break statement — The break statement takes the flow of control out of the
switch statement. Without the break statement, execution would simply
continue to the next case.
2. default statement — When none of the case values are equal to the expression
of switch statement then default case is executed. Default case is optional. If
default case is absent and no case values match then none of the statements
from switch are executed.

Question 5

Give one difference between while and do while loop.

Answer
while do-while

It is an entry- It is an exit-
controlled loop. controlled loop.

It is helpful in
It is suitable when
situations where
we need to display a
number of iterations
menu to the user.
are not known.
Lab Session

Question 1

Write a program in Java using for loop to print all the odd and even number upto 30 terms.

public class KboatEvenOdd


{
public static void main(String args[]) {
[Link]("Odd numbers: ");
for (int i = 1; i <= 60; i += 2) {
[Link](i + " ");
}

[Link]();
[Link]("Even numbers: ");
for (int i = 2; i <= 60; i += 2) {
[Link](i + " ");
}
}
}

Output
Question 2

Write a program to print all the factors of 20.

public class KboatFactors


{
public static void main(String args[]) {
int n = 20;
[Link]("Factors of 20 are:");
for (int i = 1; i <= n; i++) {
if (n % i == 0)
[Link](i);
}
}
}

Output
Question 3

Write a program using while loop to generate the first 10 natural numbers and their sum.

public class KboatNaturalNumbers


{
public static void main(String args[]) {
int n = 1;
int sum = 0;
while (n <= 10) {
[Link](n);
sum = sum + n;
n = n + 1;
}
[Link]("Sum = " + sum);
}
}

Output
Question 4

Program to check whether the product of two numbers is a buzz number or not.
[A number that ends with 7 or is divisible by 7, is called a buzz number]

public class KboatBuzzCheck


{
public static void buzzNumCheck(int a, int b) {
int p = a * b;
[Link]("Product = " + p);
if (p % 10 == 7 || p % 7 == 0)
[Link]("Product is a Buzz Number");
else
[Link]("Product is not a Buzz Number");
}
}

Output

Question 5

Generate the following series upto 10 terms:


a) 1, 8, 27, 64, 125 ........

public class KboatSeries


{
public static void main(String args[]) {
int a = 1;
for (int i = 1; i <= 10; i++) {
int t = a * a * a;
[Link](t + " ");
a++;
}
}
}

Output

b) 0, 3, 8, 15, 24, 35 .....

public class KboatSeries


{
public static void main(String args[]) {
for (int i = 1; i <= 10; i++) {
int t = i * i - 1;
[Link](t + " ");
}
}
}
Output

c) 1, 4, 7,10 ......

public class KboatSeries


{
public static void main(String args[]) {
int t = 1;
for (int i = 1; i <= 10; i++) {
[Link](t + " ");
t += 3;
}
}
}

Output
Question 6

Write a program to print all the prime numbers between 1 and 100.

public class KboatPrime


{
public static void main(String args[]) {
for (int i = 1; i <= 100; i++) {
int c = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0)
c++;
}
if (c == 2)
[Link](i);
}
}
}

Output

Question 7

Write a Java program using the switch case to print the corresponding days of numbers.
For example: 1= Monday.... 7 = Sunday

public class KboatDayName


{
public static void dayName(int n) {
switch (n) {
case 1:
[Link]("Monday");
break;

case 2:
[Link]("Tuesday");
break;

case 3:
[Link]("Wednesday");
break;

case 4:
[Link]("Thursday");
break;

case 5:
[Link]("Friday");
break;

case 6:
[Link]("Saturday");
break;

case 7:
[Link]("Sunday");
break;

default:
[Link]("Incorrect day.");
}
}
}

Output
Question 8

Write a program to print the largest of three numbers.

public class KboatLargestNumber


{
public static void largestNumber(int a, int b, int c) {
[Link]("The three numbers are "
+ a + ", " + b + ", " + c);

[Link]("Largest Number: ");


if (a > b && a > c)
[Link](a);
else if (b > a && b > c)
[Link](b);
else
[Link](c);
}
}

Output
Question 9

Write a program to print the Fibonacci series upto 10 terms.


[A series of numbers in which each number is the sum of the two preceding numbers. For
example: 0,1,1,2,3, ............... n].

public class KboatFibonacci


{
public static void main(String args[]) {
int a = 0;
int b = 1;
[Link](a + " " + b);
/*
* i is starting from 3 below
* instead of 1 because we have
* already printed 2 terms of
* the series. The for loop will
* print the series from third
* term onwards.
*/
for (int i = 3; i <= 10; i++) {
int term = a + b;
[Link](" " + term);
a = b;
b = term;
}
}
}

Output
Question 10

Create a program to display whether the entered character is in uppercase or lowercase.

public class KboatLetterCheck


{
public static void checkLetter(char ch) {
[Link]("Entered character: " + ch);
if (ch >= 65 && ch <= 90)
[Link]("Uppercase");
else if (ch >= 97 && ch <= 122)
[Link]("Lowercase");
else
[Link]("Not a letter");
}
}

Output

Common questions

Powered by AI

Not using a 'break' statement results in fall-through, where subsequent case blocks are executed regardless of whether their case values match. This can lead to bugs if unintended code executes, altering program behavior and causing unexpected results .

Entry-controlled loops (like 'for' and 'while') evaluate the condition before entering the loop body. If the condition is false initially, the loop body may never execute. Exit-controlled loops (like 'do-while') execute the loop body at least once before checking the condition at the end of the loop iteration .

Logical fall-through can be beneficial for executing shared code among multiple cases without repeating code. However, it can cause unintended behavior and bugs if 'break' statements are missing, leading to execution of unintended case blocks .

A 'do-while' loop is preferable when the loop body must execute at least once regardless of the condition. For example, when displaying a menu to a user at least once before checking if they want to exit based on their input .

The 'switch' statement evaluates a single expression and compares it against multiple case labels. The 'break' statement is used to exit the switch block after executing the matching case to prevent fall-through, and the 'default' case is executed if no other cases match, providing a catch-all option .

The initialization expression is optional in a 'for' loop because it allows flexibility in the loop's starting conditions. Omitting it can lead to infinite loops if not handled correctly, as it requires manual initialization of the loop control variable outside the loop .

Proper indentation improves the readability and maintainability of code by visually separating different blocks and logical structures, making it easier to follow program flow and debugging processes .

A 'for' loop is ideal when the number of iterations is known beforehand, such as iterating over arrays or collections, where initialization, condition checking, and iteration updates are logically related and can be succinctly expressed within the loop's declaration .

Using modular expressions allows checking conditions with minimal computational resources, like checking divisibility in mathematical functions. It optimizes logic checks by reducing complex calculations to simple arithmetic, increasing execution efficiency and readability .

Loops enable systematic calculation and iteration over a sequence to form a mathematical series by repeatedly applying the logic and accumulating results. For example, a 'for' loop can generate a sequence of cube numbers like: `for (int i = 1; i <= 10; i++) { System.out.print(i*i*i + ' '); }` producing cubes 1, 8, 27,..., 1000 .

You might also like