• 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

if & switch

Conditional Statements in Java

  • Categories if & switch, if & switch, Java

Here we are going to learn above Java Conditional Statements. You can watch our video on Conditional Statements in Java- Click Here

Q. 1 What is a block or compound statement?
Ans. A block is a group of one or more than one statement enclosed within the curly braces. The group statement is also a closed block and enclosed between pairs of curly braces’{}’.
Example:

{    // beginning of compound block
int a=5;
a++;
System.out.println(a);
a= a+2;
System.out.println(y);
}  // close of compound block

Q. 2 What is a selection statement? Name the selection statements provided by Java.
Ans.
‘Selection’ means it is the test condition given with conditional statement which decides that which statement should be executed next.
    Example:

  • if()
  • if()-else
  • switch-case
  • conditional or ternary operators (? and : )

Q. 3 What is if statement?
Ans. The if statement executes a block of code only if the specified expression is true. If the value is false, then the if block is skipped and execution continues with the rest of the program. You can either have a single statement or a block of code within an if statement.

  • Conditional expression must be a Boolean expression.
  • There is no ; after conditional expression

if/else statement is an extension of the if statement. If the condition is false, the statements in the else block are executed.
Syntax

if (<conditional expression>)
{
    <statement/s>
}
else
{
    <statement/s>
}

Q. 4 What is switch statement?
Ans. It is also called case or multiple choice statements. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

  • The variable used in a switch statement can only be a byte, short, int, or char. (NOT FLOAT)
  • break statement is commonly used at end of case statements so that flow of control jumps to the next line following the switch statement.
  • If no break appears, the flow of control will fall through to subsequent cases until a break is reached.

A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Syntax:

switch(expression)
{
     case value :
           //Statements
           break; //optional
     case value :
           //Statements
           break; //optional
     //You can have any number of case statements.
     default : //Optional
               //Statements
}

Q. 5 What is difference between if else and switch statement?
Ans.

If elseSwitch
Can compare with range of valuesCan set condition to compare a variable with value
Can be used for floating type nosCannot be used for floating point nos
Can compare 2 variable valuesCan compare a variable with constant only
Logical expressions can be usedLogical expressions cannot be used

Q. 6 Define fall-through with one example.
Ans. The situation which arises in the absence of break statement within switch-case block is known as fall-through situation. In this situation the control falls on all the cases till it gets a break statement otherwise after executing all the cases the control goes out of switch- case block.
Example:

int m, x= 0;
switch( m)
{
     case 0:
        x+= 10;
     case 1:
        x += 20;
     case 2:
        x +=30;
        System.out.println("X=" + x );
        break;
     case 3:
        x=x+6;
        System.out.println('"X=" +x);
        break;
 }
  • Share:
author avatar
Simply Coding

Previous post

Java Class Libraries and Packages
June 16, 2021

Next post

Java Tokens & Data Types
June 16, 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