60 views2 pages
The “if” Statement
1. Basic forms
if (boolean-expression)
statement-if-true;
if(boolean-expression)
statement-if-true;
else
statement-if-false;
2. Blocks and Braces
{} to group a sequence of staements into a single unit
if (boolean-expression) {
list-of-statements-if-true;
}
3. Problem: Swapper
Write a program that, given 2 values in variables x and y, ensures that y is not less than x
public class Swapper {
public static void main(String[] args) {
Scanner int = new Scanner(System.in);
int x = in.nextInt();
int y = int.nextInt();
System.out.printf(“BEFORE: x = %d, y = %d\n”, x, y);
if int(x > y) {
int t = x;
x = y;
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

The if statement if (boolean-expression) statement-if-true; if(boolean-expression) statement-if-true; else statement-if-false: blocks and braces. {} to group a sequence of staements into a single unit if (boolean-expression) { list-of-statements-if-true: problem: swapper. Write a program that, given 2 values in variables x and y, ensures that y is not less than x public class swapper { public static void main(string[] args) { Scanner int = new scanner(system. in); int x = in. nextint(); int y = int. nextint(); System. out. printf( before: x = %d, y = %d\n , x, y); if int(x > y) { int t = x; x = y; System. out. printf( after: x = %d, y = %d\n , x, y): file extensions. String findextension(string fname) { int dot; if (fname == null) return ; if (dot == -1) return , String extension = f. substring(dot, fname. length()); return extension: short-circuit evaluation. The boolean operators && (and) and || (or) abandon evaluation if the result is determined with certainty.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions