DECISION CONTROL CONSTRUCTS IN JAVA

Similar documents
CS1150 Principles of Computer Science Boolean, Selection Statements (Part II)

The Java if statement is used to test the condition. It checks Boolean condition: true or false. There are various types of if statement in java.

CS1150 Principles of Computer Science Loops

CS1150 Principles of Computer Science Boolean, Selection Statements (Part II)

CS1150 Principles of Computer Science Midterm Review

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture2 Functions

$ARCSIGHT_HOME/current/user/agent/map. The files are named in sequential order such as:

- Replacement of a single statement with a sequence of statements(promotes regularity)

COP2800 Homework #3 Assignment Spring 2013

C++ Reference Material Programming Style Conventions

CS5530 Mobile/Wireless Systems Swift

CS1150 Principles of Computer Science Methods

Reading and writing data in files

EASTERN ARIZONA COLLEGE Java Programming I

Overview of OPC Alarms and Events

Announcement. VHDL in Action. Review. Statements. Process Execution with no sensitivity list. Sequential Statements

Lab 1 - Calculator. K&R All of Chapter 1, 7.4, and Appendix B1.2 Iterative Code Design handout Style Guidelines handout

CS1150 Principles of Computer Science Final Review

Configuring Database & SQL Query Monitoring With Sentry-go Quick & Plus! monitors

In-Class Exercise. Hashing Used in: Hashing Algorithm

Iteration Part 2. Review: Iteration [Part 1] Flow charts for two loop constructs. Review: Syntax of loops. while continuation_condition : statement1

Relational Operators, and the If Statement. 9.1 Combined Assignments. Relational Operators (4.1) Last time we discovered combined assignments such as:

Lab 1 - Calculator. K&R All of Chapter 1, 7.4, and Appendix B1.2

Using CppSim to Generate Neural Network Modules in Simulink using the simulink_neural_net_gen command

Project #1 - Fraction Calculator

Querying Data with Transact SQL

Lab 4. Name: Checked: Objectives:

Dashboard Extension for Enterprise Architect

Computer Organization and Architecture

Infrastructure Series

It has hardware. It has application software.

Test Pilot User Guide

Create Your Own Report Connector

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Extensible Query Processing in Starburst

ROCK-POND REPORTING 2.1

Laboratory #13: Trigger


Preparation: Follow the instructions on the course website to install Java JDK and jgrasp on your laptop.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

CS1150 Principles of Computer Science Introduction (Part II)

MIPS Architecture and Assembly Language Overview

LAB 7 (June 29/July 4) Structures, Stream I/O, Self-referential structures (Linked list) in C

Program Control Structures. C++ Control Structures Logical Operators Selection Repetition

Programming Project: Building a Web Server

Exercises: Plotting Complex Figures Using R

FIT 100. Lab 10: Creating the What s Your Sign, Dude? Application Spring 2002

Making spreadsheets machine-readable. With Dave from ScraperWiki

DesignScript summary:

D e s i g n S c r i p t L a n g u a g e S u m m a r y P a g e 1

CS1150 Principles of Computer Science Methods

Lab 0: Compiling, Running, and Debugging

Purchase Order Approvals Workflow Guide

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

ISTE-608 Test Out Written Exam and Practical Exam Study Guide

Update Panle Usuage: Enables sections of a page to be partially rendered without a postback.

Primitive Types and Methods. Reference Types and Methods. Review: Methods and Reference Types

from DDS on Mac Workstations

Renewal Reminder. User Guide. Copyright 2009 Data Springs Inc. All rights reserved.

Common Language Runtime

TRAINING GUIDE. Lucity Mobile

Tutorial 5: Retention time scheduling

Principles of Programming Languages

Darshan Institute of Engineering & Technology for Diploma Studies Unit 2

TRAINING GUIDE. Overview of Lucity Spatial

But for better understanding the threads, we are explaining it in the 5 states.

Lesson 4 Advanced Transforms

History of Java. VM (Java Virtual Machine) What is JVM. What it does. 1. Brief history of Java 2. Java Version History

PAGE NAMING STRATEGIES

STIDistrict AL Rollover Procedures

To over come these problems collections are recommended to use. Collections Arrays

ECE 545 Project Deliverables

Access the site directly by navigating to in your web browser.

Entering an NSERC CCV: Step by Step

To start your custom application development, perform the steps below.

Municode Website Instructions

wait on until ' for ECE 4514 Martin 2002 ECE 4514 Martin 2002 ECE 4514 Martin 2002 architecture begin : process begin end process;

HP MPS Service. HP MPS Printer Identification Stickers

CS1150 Principles of Computer Science Introduction

Uploading Files with Multiple Loans

NVIDIA S KEPLER ARCHITECTURE. Tony Chen 2015

OVAL Language Design Document

Element Creator for Enterprise Architect

The programming for this lab is done in Java and requires the use of Java datagrams.

Report Writing Guidelines Writing Support Services

FIREWALL RULE SET OPTIMIZATION

TaskCentre v4.5 Save As File Tool White Paper


Quick Start Guide. Basic Concepts. DemoPad Designer - Quick Start Guide

Due Date: Lab report is due on Mar 6 (PRA 01) or Mar 7 (PRA 02)

Groovy Programming Language. Duration : 5 days. Groovy Getting Started. Groovy Big Picture. Groovy Language Spec. Syntax.

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

10 hours create the college model. Data Definition Commands. Data Manipulation commands, Data Control commands

Proper Document Usage and Document Distribution. TIP! How to Use the Guide. Managing the News Page

EASTERN ARIZONA COLLEGE Visual Basic Programming I

Recommended Minimum Requirements for Cisco Meeting Application Web RTC Use

Introduction to Eclipse

Access 2000 Queries Tips & Techniques

Model WM100. Product Manual

CITI Technical Report 08-1 Parallel NFS Block Layout Module for Linux

Transcription:

DECISION CONTROL CONSTRUCTS IN JAVA Decisin cntrl statements can change the executin flw f a prgram. Decisin cntrl statements in Java are: if statement Cnditinal peratr switch statement If statement The if statement is fllwed by a bdy blck. The if statement evaluate lgical expressins and make a decisin whether t execute the blck r nt based n whether the lgical expressin returns a true r false. Syntax: if if(cnditin) {//d smething If the cnditin is true the if blck is executed, else nt. The cnditin shuld be a blean variable r a lgical expressin that will evaluate t blean values true r false. If we specify anything ther than a blean variable r an expressin that desn't evaluate t true r false, yu will get cmpilatin errr. Even if yu pass a String variable with a value f true r false, yu will still get a cmpilatin errr. Curly braces { are nt required when nly ne statement is there fr the if, "else if" r else clause, it is always a gd practice t use curly braces { t enclse if bdy fr better clarity, and t avid accidental mistakes. if, else-if, else The if statement blck can be fllwed by any number f ptinal else if statement blcks and ne ptinal else blck. Syntax: if, else-if, else if (cnd1) { //d smething

else if (cnd2) { //d smething else if (cnd3) { //d smething else { //d smething Only ne f the if r else if blcks will be executed fr the first true cnditin. If n if r else if cnditins are true, then the else blck is executed (if ne is available). Aviding accidental mistakes Usage f curly braces { fr enclsing if bdy Curly braces { are nt required when nly ne statement is there fr the if, "else if" r else clause. if(cnditin) statement1; Hwever, it is always a gd practice t use curly braces { t enclse if bdy fr better clarity, and t avid accidental mistakes like belw: if(cnditin) statement1; statement2; else statement3; Here, the if statement has nly a single statement, statement1. The secnd statement, statement2 is actually nt part f "if" blck and hence statement1 will end the if lp.

This is fllwed by the else clause, which is nw nt part f any if blck and hence yu will get a cmpilatin errr. A mre serius prblem ccurs when the extra statement is in the else blck as n exceptin is thrwn, but the statement will always get executed irrespective f the else blck. Anther prblem with nt using a blck statement is the dangling else prblem. In a cmplex nested if scenari, an else might be assciated with a different if than what we intended. The else keywrd is paired with the clsest if. Using the assignment peratr = instead f equality peratr == 1. If yu use the assignment peratr = instead f equality peratr ==, an assignment will happen instead f evaluatin. 2. When yu say if(x=y), y is assigned t x and x is evaluated. If x and y are blean, value f x is verwritten with the value f y and then x is evaluated. If the types cmpared are nt blean, yu will get a cmpile time errr as yu are evaluating smething which is nt a blean. Example: assignment peratr = instead f equality peratr == blean iscmpleted=false; if(iscmpleted=true){ System.ut.println("Executed!!!"); This will print "Executed!!!" Here, iscmpleted=true will assign true t iscmpleted verwriting its current value and then iscmpleted is evaluated; hence the evaluatin will always return true irrespective f the value f iscmpleted. The true is actually nt necessary in this case and we culd have just used if(iscmpleted) and avided the abve mistake. Cnditinal peratr The cnditinal peratr can be cnsidered as a limited frm f "if" statement. It is als called the ternary peratr as it has three cmpnents.

Syntax: cnditinal peratr LgicalExpressin? ThenExpressin : ElseExpressin If the LgicalExpressin evaluates t true, then the result f the ThenExpressin is returned. Otherwise the result f the ElseExpressin is returned. Example:cnditinal peratr int a = 1; int b = 5; int r; r= a>b?a : b; System.ut.println(r); This will print 5. Here the expressin (a>b) evaluate t false and hence the value f b is assigned t r. [Tip] The use f the cnditinal peratr is discuraged ver if-else due t its readability issues. Switch statement The switch statement prvide multi-branch selectins based upn integer, enumeratin, r Stringexpressin. Syntax: switch statement switch (expressin) { case value: statements; case value: statements; default: statements

Example:switch statement int abc = 7; switch (abc) { case 1: System.ut.println("ONE"); case 5: System.ut.println("FIVE"); default: System.ut.println("DEFAULT"); case 7: System.ut.println("SEVEN"); Imprtant prperties f switch statement in Java 1. Any f byte, char, shrt, and int data types (and their wrapper classes) can be used with an integer switch statement; lng and duble are nt allwed. 1. Crrespnding wrapper classes such as Character, Byte, Shrt, and Integer are als allwed. 2. Prir t Java 7, nly integer variables culd be used with a switch statement. 2. In switch statement, the value f a cnstant expressin is checked and then the cntrl is passed t a case that matches the cnstant expressin. 1. If n case matches the value f the expressin, cntrl is passed t the default clause, if present. 3. A default can cme in between the cases in the switch statement and desn't have t be always the last. But usually it is written in the end. 4. The break keywrd is used t end the cde sequence fr a case, and t exit the switch statement.

1. If there is n break fr a case, all the remaining cases (including default) will be executed until a break is fund r the end f switch is reached. 2. Break is nt always needed at the end f the default clause if default is the last case, but, it is usually included, especially if default is nt the last case. 5. With a String switch statement, 1. a null value assigned t a string variable used within a switch statement will lead t java.lang.nullpinterexceptin exceptin. 2. the cmparisn made within a switch statement is case-sensitive. Surce : http://javajee.cm/decisin-cntrl-cnstructs-in-java