0% found this document useful (0 votes)
17 views9 pages

PQ

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

PQ

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
2arii2023 11:18 Java ll. Else 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools Java If ... Else [crv] Looe | Java Conditions and If Statements You already know that Java supports the usual logical conditions from mathematics: + Less than: a < b + Less than or equal to: a <= b * Greater than: a > b * Greater than or equal to: a * Equal to a == b + Not Equal to: a I= b You can use these conditions to perform different actions for different decisions. Java has the following conditional statements: * Use if to specify a block of code to be executed, if a specified condition is true * Use else to specify a block of code to be executed, if the same condition is false * Use else if to specify a new condition to test, if the first condition is false + Use switch to specify many alternative blocks of code to be executed The if Statement Use the if statement to specify a block of Java code to be executed if a condition is true. nitps:wwwawschools.comfavaljava_conditions.asp 9 2arii2023 11:18 Java ll. Else 3 WW tutoriaisy Exercises» Servicessy§ Q 0 schools SQL PYTHON PHP WTO if (condition) { // block of code to be executed if the condition is true } Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error. In the example below, we test two values to find out if 20 is greater than 18. If the condition is true , print some text: Example if (28 > 18) { system.out.printIn("20 is greater than 18"); We can also test variables: Example int x 20; int y = 18; if (x>y) { system.out.println("x is greater than y"); + nitps:wwwawschools.comfavaljava_conditions.asp 219 2arii2023 11:18 Java ll. Else 3 WW tutoriaisy Exercises» Servicessy§ Q 0 schools SQL. PYTHON PHP than 18, we print to the screen that "x is greater than y". The else Statement Use the else statement to specify a block of code to be executed if the condition is false. Syntax if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false Example int time = 20; if (time < 18) { system.out.print1n("Good day."); hitpssvwwaschools.comfavaljava_condtions.asp a9 2arii2023 11:18 Java ll. Else 3 WW tutoriaisy Exercises» Servicessy§ Q 0 schools sQ PYTHON PHP Example explained In the example above, time (20) is greater than 18, so the condition is false. Because of this, we move on to the else condition and print to the screen "Good evening", If the time was less than 18, the program would print "Good day", The else if Statement Use the else if statement to specify a new condition if the first condition is false Syntax if (condition) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 i } else { // block of code to be executed if the condition1 is false and condition2 i } Example int time = 225 if (time < 10) { system.out.print1n("Good morning. system.out.printIn("Good day. } else { system.out.println("Good evening } hitpssvwwaschools.comfavaljava_condtions.asp 49 2arii2023 11:18 Java ll. Else 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools Example explained In the example above, time (22) is greater than 10, so the first condition is false The next condition, in the else if statement, is also false, so we move on to the else condition since condition1 and condition2 is both false - and print to the screen "Good evening". However, if the time was 14, our program would print "Good day." Exercise: Print "Hello World” if x is greater than y int x = 50; int y = 10; (xy) f system.out.print1n("Hello World") ; Start the Exercise nitps:wwwawschools.comfavaljava_conditions.asp 59 2arii2023 11:18 Java ll. Else 3 w Tutorials Exercisesw Servicesey§ QO Login schools SN aN SHOP NOW COLOR PICKER © 2arive023 14:18 Java lf. Else VY" Tutorials» —Exercises~ Services Q @ SignUp Login CSS JAVASCRIPT SQL. PYTHON. «= JAVA. PHP, «= « HOWTO W3,CSS_—C BHU Learn Front-end Development High Demand: Front-end skills are in demand! Secure your future. 3 w SPACES UPGRADE NEWSLETTER schools GET CERTIFIED REPORT ERROR Top Tutorials htipsifinew.w3schools.comijavaljava_conditions.asp 719 2arive023 14:18 w schools Tutorials css Java lf. Else Exercises» Services JAVASCRIPT SQL. PYTHON W3.cSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial Top References HTML Reference SS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference ‘Angular Reference Query Reference Top Examples HTML Examples SS Examples JavaScript Examples How To Examples SQL Examples Python Examples Wa.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples JQuery Examples G@ @ ® © Forum asour W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning Q 0 JAVA PHP. Get Certified HTML Certificate css certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate Query Certificate Java Certificate c+ Certificate i Certificate XML Certificate Sign Up How TO Login W3.css Tutorials, references, and examples are constantly reviewed to avold errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by htipsifinew.w3schools.comijavaljava_conditions.asp c as 2arive023 14:18 Java lf. Else 3 w Tutorials~ Exercisesw Serviesey§ QO SignUp Login = CSS _JAVASCR SQL PYTHON JAVA. PHP, «= HOWTO.-—W3.CSS_—C ipsiimewsn schools comfovalava_contons asp 39

You might also like