OOPR Midterms Reviewer Module 5 Conditional Statements PDF

Document Details

EntertainingBandoneon

Uploaded by EntertainingBandoneon

Tags

java programming conditional statements control structures exception handling

Summary

This document provides a review of conditional statements in Java, including if statements, if-else statements, nested if statements, switch case statements, and looping statements. It also covers exception handling with different types of exceptions and the try-catch block. It explains the purpose and use of assertions.

Full Transcript

OOPR MIDTERMS REVIEWER IF STATEMENT MODULE 5 The if statement executes a block of code CONDITIONAL STATEMENTS only if a specified condition is true. JAVA STATEMENT If the expression is...

OOPR MIDTERMS REVIEWER IF STATEMENT MODULE 5 The if statement executes a block of code CONDITIONAL STATEMENTS only if a specified condition is true. JAVA STATEMENT If the expression is true, the statement inside A Java statement is one or more lines of code the if block is executed; if false, it is skipped. ending with a semi-colon (;). It generally contains expressions that have a value. IF-ELSE STATEMENT The if-else statement is an extension of the if statement that provides an alternative block to execute when the condition is false CONTROL STRUCTURE Control structures in Java allow programmers to change the order of execution in a program. They include: DECISION CONTROL STRUCTURES Used for executing specific blocks of code while skipping others. REPETITION CONTROL STRUCTURES Used for executing a specific section of code multiple times. NESTED IF STATEMENT BREAK Nested if Statement A nested if statement is Defines the end of a case. an if statement inside another if or else statement, allowing for more complex DEFAULT STATEMENT conditional logic Used in defining message to be displayed that is outside of the given case. LOOPING STATEMENTS MODULE 6 SWITCH CASE STATEMENT LOOPING STATEMENT The switch-case statement is a multi-way A looping statement allows executing a branch statement that is easier to implement statement or group of statements multiple than a series of if-else statements when times, following a general format common checking against multiple fixed values. The across programming languages. break statement defines the end of each case. FOR LOOP A for loop is a repetition control structure that efficiently writes a loop to be executed a specific number of times. It is especially useful when the number of repetitions is known. DO-WHILE LOOP The do-while loop is similar to the while loop but guarantees to execute at least once since it checks the condition after executing the loop body. OUTPUT: WHILE LOOP The while loop repeatedly executes a target statement as long as a given condition is true. It tests the condition before executing the loop body, meaning it may never execute if the condition is initially false. MODULE 7 COMMON EXCEPTIONS EXCEPTION HANDLING EXCEPTION EXCEPTION HIERARCHY TYPES OF EXCEPTION  Error: Represents serious issues in the Java runtime system.  Checked Exception: Direct subclasses of the Throwable class, excluding RuntimeException and Error. These are normal errors that can occur during program execution.  Runtime Exception: Includes all exceptions that typically occur at runtime CREATING EXCEPTIONS GENERATING EXCEPTIONS THE TRY BLOCK {} THE CATCH BLOCK {} TRY, CATCH. AND FINALLY HANDLING EXCEPTION SYNTAX KEY ASPECTS  the block notion is mandatory  for each try block, there can be one or more catch blocks, but at mostly only one finally block.  the catch blocks and finally blocks must always appear in conjunction with the try block, and in the above order  a try block must be followed by atleast one catch block or one finally block  each catch block defines an exception handle. the header of the catch block takes exactly one argument. which is the ASSERTION IN JAVA exception it will handle. the exception must be one of the throwable class or one of it’s subclasses. THROW CLAUSE WHY USE ASSERTION? WHERE TO USE ASSERTIONS? WHERE NOT TO USE ASSERTIONS? ASSERTION VS NORMAL EXCEPTION HANDLING

Use Quizgecko on...
Browser
Browser