• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Iterative Statements

Loops in Java

  • Categories Iterative Statements, For & While, Java

In this post we will cover loops in java. You can watch our videos on Java For Loop – Click Here

Q.1 What is iteration? Give any 2 iteration statements in Java?
Ans. Iteration means looping. When we need to execute a block of code several number of times, it is often referred to as a iteration or loop. Java has very flexible three looping mechanisms. You can use one of the following three loops:

  • while Loop
  • do..while Loop
  • for Loop

Q.2 What is for loop in Java?
Ans. 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, with 3 parts

  • Starting initialization, which executes once before the loop begins. The section can also be a comma-separated list of expression statements.
  • Test expression. As long as the expression is true, the loop will continue. If this expression is evaluated as false the first time, the loop will never be executed.
  • Incr/decr expression automatically executes after each repetition of the loop body.
  • Syntax
for (starting initialization ; test expression ; <incr/decr expression>)
{

 

}

Q.3 State the difference between entry controlled loop and exit controlled loop.
Ans.

entry controlled loopexit controlled loop
1. The loop condition is checked before executing the body of the loop1. The loop condition is checked after executing the body of the loop.
2. Example: While loop and for loop
2. Example: do-while loop

In an entry controlled loop, the loop condition is checked before executing the body of the loop. While loop and for loop are the entry controlled loops in Java. In exit controlled loops, the loop condition is checked after executing the body of the loop. do-while loop is the exit controlled loop in Java

Q.4 What is an infinite loop?
Ans. The loop which never ends and does not reach to the test condition is known as infinity loop. It is also known as unending loop.
Example: for(int y=1; y<=10; y–) ;

This loop ‘y’ starts from ‘1’ and goes upto 10 but the update statement ‘y- -‘ makes the loop infinite or unending because the condition y<=10 will always be true.

Q.5 What is the difference between while and do while loop?
Ans.

WHILEDO-WHILE
Condition is checked first then statement(s) is executed.Statement(s) is executed atleast once, thereafter condition is checked.
It might occur statement(s) is executed zero times, If condition is false.At least once the statement(s) is executed.
No semicolon at the end of while.
while(condition)
Semicolon at the end of while.
while(condition);
If there is a single statement, brackets are not required.Brackets are always required.
Syntax
while (expression)
{
statement(s)
}
do
{
statement(s)
} while (expression);

Q.6 What happens if we put a ; at the end of for loop?
Ans. If a semicolon is placed after the for() statement then semicolon is treated as the body of the loop and the for statement executes the null loop. Once the null loop is finished, it will execute the statement(s) following the for() statement.
Example:

for(i=0; i<3; i++);
{
   System.out.println(“i= ” +i);
}

Output: i=3

Q.7 What is difference between break and continue statement?
Ans.

BreakContinue
Break keyword is used to break or stop a loop executionContinue terminates the current iteration and passes the control to the next iteration of the loop
Control passes to next statement following the loopControl passes to next iteration of the loop.
Break can be used with switch statement and loopsContinue is used along with loops only
e.g.
for ( int x = 0;x < 10;x++) {
if(x < 5)
System.out.println (“Less than 5”);
else
break;
}
e.g.
for(int x = 0;x < 10;x++) {
if(x%2 == 0)
print (“Even”);
else
continue;
}

  • Share:
author avatar
Simply Coding

Previous post

Boolean Algebra
June 17, 2021

Next post

Constructors
June 17, 2021

You may also like

Questions on Encapsulation
Questions on Encapsulation
4 July, 2021
Questions on Library classes
Questions on Library classes
4 July, 2021
Questions on String 1
What is Java String?
3 July, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now