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.

Similar documents
DECISION CONTROL CONSTRUCTS IN JAVA

CS1150 Principles of Computer Science Midterm Review

CS1150 Principles of Computer Science Loops

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

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

COP2800 Homework #3 Assignment Spring 2013

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

CS1150 Principles of Computer Science Introduction (Part II)

CS5530 Mobile/Wireless Systems Swift

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

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

Programming Project: Building a Web Server

OOP JAVA UNIT IV. Java is a high level, robust, secured and object-oriented programming language.

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

Lab 0: Compiling, Running, and Debugging

CS1150 Principles of Computer Science Final Review

Lab 4. Name: Checked: Objectives:

CS1150 Principles of Computer Science Methods

Project #1 - Fraction Calculator

C++ Reference Material Programming Style Conventions

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

Normally, an array is a collection of similar type of elements that have a contiguous memory location.

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

Enterprise Chat and Developer s Guide to Web Service APIs for Chat, Release 11.6(1)

Introduction to Eclipse

Creating a TES Encounter/Transaction Entry Batch

CREATING A DONOR ACCOUNT

Reading and writing data in files

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

CS1150 Principles of Computer Science Methods

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

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

CS4500/5500 Operating Systems Synchronization

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

Data Miner Platinum. DataMinerPlatinum allows you to build custom reports with advanced queries. Reports > DataMinerPlatinum

Structure Query Language (SQL)

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

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

Contents. I. A Simple Java Program. Topic 02 - Java Fundamentals. VIII. Conversion and type casting

Querying Data with Transact SQL

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

Getting Started with the Web Designer Suite

Paraben s Phone Recovery Stick

Ascii Art Capstone project in C

IAB MRAID 2 TEST AD: RESIZE WITH ERRORS AD. Resize with Errors Ad. URL for this Creative. Goal of Ad. This Creative Tests:

CSE 3320 Operating Systems Synchronization Jia Rao

Uploading Files with Multiple Loans

Export and Import Course Package

Laboratory #13: Trigger

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Relius Documents ASP Checklist Entry

Working With Audacity

Assignment #5: Rootkit. ECE 650 Fall 2018

Design Patterns. Collectional Patterns. Session objectives 11/06/2012. Introduction. Composite pattern. Iterator pattern

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

ECE 545 Project Deliverables

Create Your Own Report Connector

Principles of Programming Languages

APPLY PAGE: LOGON PAGE:


The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

AP Computer Science A

Computer Organization and Architecture

InformationNOW Elementary Scheduling

GPA: Plugin for Prerequisite Checks With Solution Manager 7.1

Second Assignment Tutorial lecture

The Login Page Designer

SPAR. Workflow for Office 365 User Manual Ver ITLAQ Technologies

Darshan Institute of Engineering & Technology for Diploma Studies Unit 2

Assignment 10: Transaction Simulation & Crash Recovery

TUTORIAL --- Learning About Your efolio Space

B ERKELEY. Homework 7: Homework 7 JavaScript and jquery: An Introduction. Part 1:

DIGITAL NOTES ON JAVA PROGRAMMING B.TECH II YEAR - II SEM ( )

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

TRAINING GUIDE. Lucity Mobile

UiPath Automation. Walkthrough. Walkthrough Calculate Client Security Hash

McGill University School of Computer Science COMP-206. Software Systems. Due: September 29, 2008 on WEB CT at 23:55.

CS1150 Principles of Computer Science Introduction

EASTERN ARIZONA COLLEGE Java Programming I

Using the Swiftpage Connect List Manager

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

CSE 361S Intro to Systems Software Lab #2

One reason for controlling access to an object is to defer the full cost of its creation and initialization until we actually need to use it.

ROCK-POND REPORTING 2.1

Telecommunication Protocols Laboratory Course

Faculty Textbook Adoption Instructions

/

INSTALLING CCRQINVOICE

Drupal Profile Sites for Faculty, Staff, and/or Administrators WYSIWIG Editor How-To

Purchase Order Approvals Workflow Guide

Step- by- Step Instructions for Adding a HotPot Activity 1. Click the Turn editing on button on the course home page.

Importing data. Import file format

Municode Website Instructions

Lab 5 Sorting with Linked Lists

Properties detailed info There are a few properties in Make Barcode to set for the output of your choice.

Profiling & Debugging

HOW TO REGISTER FOR THE TEAS ASSESSMENT 1. CREATE A NEW ACCOUNT. How to Register for the TEAS Assessment 1

InformationNOW Elementary Scheduling

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

STORM Advisor Center How To

Transcription:

Java If-else Statement The Java if statement is used t test the cnditin. It checks Blean cnditin: true r false. There are varius types f if statement in java. if statement if-else statement if-else-if ladder nested if statement Java if Statement The Java if statement tests the cnditin. It executes the if blck if cnditin is true. if(cnditin){ //cde t be executed //Java Prgram t demnstate the use f if statement. public class If1 { public static vid main(string[] args) { //defining an 'age' variable int age=20; //checking the age if(age>18){ System.ut.print("Age is greater than 18"); Age is greater than 18

Java if-else Statement The Java if-else statement als tests the cnditin. It executes the if blck if cnditin is true therwise else blck is executed. if(cnditin){ //cde if cnditin is true else{ //cde if cnditin is false //A Java Prgram t demnstrate the use f if-else statement. //It is a prgram f dd and even number. public class If2 { public static vid main(string[] args) { //defining a variable int number=13; //Check if the number is divisible by 2 r nt if(number%2==0){ System.ut.println("even number"); else{ System.ut.println("dd number"); dd number Java if-else-if ladder Statement The if-else-if ladder statement executes ne cnditin frm multiple statements. if(cnditin1){ //cde t be executed if cnditin1 is true else if(cnditin2){ //cde t be executed if cnditin2 is true else if(cnditin3){ //cde t be executed if cnditin3 is true... else{

//cde t be executed if all the cnditins are false //Java Prgram t demnstrate the use f If else-if ladder. //It is a prgram f grading system fr fail, D grade, C grade, B grade, A grade and A+. public class If3 { public static vid main(string[] args) { int marks=65; if(marks<50){ System.ut.println("fail"); else if(marks>=50 && marks<60){ System.ut.println("D grade"); else if(marks>=60 && marks<70){ System.ut.println("C grade"); else if(marks>=70 && marks<80){ System.ut.println("B grade"); else if(marks>=80 && marks<90){ System.ut.println("A grade"); else if(marks>=90 && marks<100){ System.ut.println("A+ grade"); else{ System.ut.println("Invalid!"); C grade

Java Nested if statement The nested if statement represents the if blck within anther if blck. Here, the inner if blck cnditin executes nly when uter if blck cnditin is true. if(cnditin){ //cde t be executed if(cnditin){ //cde t be executed //Java Prgram t demnstrate the use f Nested If Statement. public class If4 { public static vid main(string[] args) { //Creating tw variables fr age and weight int age=20; int weight=80; //applying cnditin n age and weight if(age>=18){ if(weight>50){ System.ut.println("Yu are eligible t dnate bld"); Yu are eligible t dnate bld Example 2: //Java Prgram t demnstrate the use f Nested If Statement. public class If5 { public static vid main(string[] args) { //Creating tw variables fr age and weight int age=25; int weight=48; //applying cnditin n age and weight if(age>=18){ if(weight>50){ System.ut.println("Yu are eligible t dnate bld"); else{ System.ut.println("Yu are nt eligible t dnate bld");

else{ System.ut.println("Age must be greater than 18"); Yu are nt eligible t dnate bld Java Switch Statement The Java switch statement executes ne statement frm multiple cnditins. It is like if-else-if ladder statement. The switch statement wrks with byte, shrt, int, lng, enum types, String and sme wrapper types like Byte, Shrt, Int, and Lng. Since Java 7, yu can use strings in the switch statement. In ther wrds, the switch statement tests the equality f a variable against multiple values. Pints t Remember There can be ne r N number f case values fr a switch expressin. The case value must be f switch expressin type nly. The case value must be literal r cnstant. It desn't allw variables. The case values must be unique. In case f duplicate value, it renders cmpile-time errr. The Java switch expressin must be f byte, shrt, int, lng (with its Wrapper type), enums and string. Each case statement can have a break statement which is ptinal. When cntrl reaches t the break statement, it jumps the cntrl after the switch expressin. If a break statement is nt fund, it executes the next case. The case value can have a default label which is ptinal.

switch(expressin){ case value1: //cde t be executed; break; //ptinal case value2: //cde t be executed; break; //ptinal... default: cde t be executed if all cases are nt matched; public class Switch1 { public static vid main(string[] args) { //Declaring a variable fr switch expressin int number=20; //Switch expressin switch(number){ //Case statements case 10: System.ut.println("10"); break; case 20: System.ut.println("20"); break; case 30: System.ut.println("30"); break; //Default case statement default:system.ut.println("nt in 10, 20 r 30"); 20

Lps in Java In prgramming languages, lps are used t execute a set f instructins/functins repeatedly when sme cnditins becme true. There are three types f lps in java. fr lp while lp d-while lp Java Fr Lp The Java fr lp is used t iterate a part f the prgram several times. If the number f iteratin is fixed, it is recmmended t use fr lp. There are three types f fr lps in java. Simple Fr Lp Fr-each r Enhanced Fr Lp Labeled Fr Lp

Java Simple Fr Lp A simple fr lp is the same as C/C++. We can initialize the variable, check cnditin and increment/decrement value. It cnsists f fur parts: 1. Initializatin: It is the initial cnditin which is executed nce when the lp starts. Here, we can initialize the variable, r we can use an already initialized variable. It is an ptinal cnditin. 2. Cnditin: It is the secnd cnditin which is executed each time t test the cnditin f the lp. It cntinues executin until the cnditin is false. It must return blean value either true r false. It is an ptinal cnditin. 3. Statement: The statement f the lp is executed each time until the secnd cnditin is false. 4. Increment/Decrement: It increments r decrements the variable value. It is an ptinal cnditin. fr(initializatin;cnditin;incr/decr){ //statement r cde t be executed //Java Prgram t print 1 t 10 public class Fr1 { public static vid main(string[] args) { //Cde f Java fr lp fr(int i=1;i<=10;i++){ System.ut.println(i); 1 2 3 4 5 6 7 8 9 10

Java Labeled Fr Lp We can have a name f each Java fr lp. T d s, we use label befre the fr lp. It is useful if we have nested fr lp s that we can break/cntinue specific fr lp. Usually, break and cntinue keywrds breaks/cntinues the innermst fr lp nly. 1. labelname: 2. fr(initializatin;cnditin;incr/decr){ 3. //cde t be executed 4. //A Java prgram t demnstrate the use f labeled fr lp public class Fr2 { public static vid main(string[] args) { //Using Label fr uter and fr lp aa: fr(int i=1;i<=3;i++){ bb: fr(int j=1;j<=3;j++){ if(i==2&&j==2){ 1 1 1 2 1 3 2 1 break aa; System.ut.println(i+" "+j); If yu use break bb;, it will break inner lp nly which is the default behavir f any lp.

public class Fr3 { public static vid main(string[] args) { aa: fr(int i=1;i<=3;i++){ bb: fr(int j=1;j<=3;j++){ if(i==2&&j==2){ break bb; System.ut.println(i+" "+j); 1 1 1 2 1 3 2 1 3 1 3 2 3 3

Java While Lp The Java while lp is used t iterate a part f the prgram several times. If the number f iteratin is nt fixed, it is recmmended t use while lp. while(cnditin){ //cde t be executed public class While1 { public static vid main(string[] args) { int i=1; while(i<=10){ System.ut.println(i); i++; 1 2 3 4 5 6 7 8 9 10

Java d-while Lp The Java d-while lp is used t iterate a part f the prgram several times. If the number f iteratin is nt fixed and yu must have t execute the lp at least nce, it is recmmended t use d-while lp. The Java d-while lp is executed at least nce because cnditin is checked after lp bdy. d{ //cde t be executed while(cnditin); public class DWhile1{ public static vid main(string[] args) { int i=1; d{ i++; System.ut.println(i); while(i<=10); 1 2 3 4 5 6 7 8 9 10

Java Break Statement When a break statement is encuntered inside a lp, the lp is immediately terminated and the prgram cntrl resumes at the next statement fllwing the lp. The Java break is used t break lp r switch statement. It breaks the current flw f the prgram at specified cnditin. In case f inner lp, it breaks nly inner lp. We can use Java break statement in all types f lps such as fr lp, while lp and d-while lp. jump-statement; break; Java Break Statement with Lp public class Break1 { public static vid main(string[] args) { //using fr lp fr(int i=1;i<=10;i++){ if(i==5){ //breaking the lp break; System.ut.println(i); 1 2 3 4

Java Cntinue Statement The cntinue statement is used in lp cntrl structure when yu need t jump t the next iteratin f the lp immediately. It can be used with fr lp r while lp. The Java cntinue statement is used t cntinue the lp. It cntinues the current flw f the prgram and skips the remaining cde at the specified cnditin. In case f an inner lp, it cntinues the inner lp nly. We can use Java cntinue statement in all types f lps such as fr lp, while lp and d-while lp. jump-statement; cntinue; Java Cntinue Statement Example //Java Prgram t demnstrate the use f cntinue statement //inside the fr lp. public class Cntinue1 { public static vid main(string[] args) { //fr lp fr(int i=1;i<=10;i++){ if(i==5){ //using cntinue statement cntinue;//it will skip the rest statement System.ut.println(i); 1 2 3 4 6 7 8

9 10 Java Cmments The java cmments are statements that are nt executed by the cmpiler and interpreter. The cmments can be used t prvide infrmatin r explanatin abut the variable, methd, class r any statement. It can als be used t hide prgram cde fr specific time. Types f Java Cmments There are 3 types f cmments in java. 1. Single Line Cmment 2. Multi Line Cmment 3. Dcumentatin Cmment 1) Java Single Line Cmment The single line cmment is used t cmment nly ne line.

//This is single line cmment public class CmmentExample1 { public static vid main(string[] args) { int i=10;//here, i is a variable System.ut.println(i); 10 2) Java Multi Line Cmment The multi line cmment is used t cmment multiple lines f cde. /* This is multi line cmment */ public class CmmentExample2 { public static vid main(string[] args) { /* Let's declare and print variable in java. */ int i=10; System.ut.println(i); 10

3) Java Dcumentatin Cmment The dcumentatin cmment is used t create dcumentatin API. T create dcumentatin API, yu need t use javadc tl. /** This is dcumentatin cmment */ /** The Calculatr class prvides methds t get additin and subtractin f given 2 numbers.*/ public class Calculatr { /** The add() methd returns additin f given numbers.*/ public static int add(int a, int b){return a+b; /** The sub() methd returns subtractin f given numbers.*/ public static int sub(int a, int b){return a-b; Cmpile it by javac tl: javac Calculatr.java Create Dcumentatin API by javadc tl: javadc Calculatr.java