0% found this document useful (0 votes)
35 views2 pages

Ifsample: Void Int

This document contains code examples demonstrating if statements, for loops, and code blocks in Java. The IfSample.java example prints statements comparing values of x and y as x is incremented by multiplication. ForTest.java uses a for loop to print the increasing value of x from 0 to 10. BlockTest.java includes a for loop whose body is a block of code that prints the values of x and y on separate lines, with y decrementing by 2 each iteration.

Uploaded by

otarVEVO
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Ifsample: Void Int

This document contains code examples demonstrating if statements, for loops, and code blocks in Java. The IfSample.java example prints statements comparing values of x and y as x is incremented by multiplication. ForTest.java uses a for loop to print the increasing value of x from 0 to 10. BlockTest.java includes a for loop whose body is a block of code that prints the values of x and y on separate lines, with y decrementing by 2 each iteration.

Uploaded by

otarVEVO
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 2 listing 3 /* Demonstrate the if. Call this file "IfSample.java".

*/ class IfSample { public static void main(String args[]) { int x, y; x = 10; y = 20; if(x < y) System.out.println("x is less than y"); x = x * 2; if(x == y) System.out.println("x now equal to y"); x = x * 2; if(x > y) System.out.println("x now greater than y"); // this won't display anything if(x == y) System.out.println("you won't see this"); } } listing 4 /* Demonstrate the for loop. Call this file "ForTest.java". */ class ForTest { public static void main(String args[]) { int x; for(x = 0; x<10; x = x+1) System.out.println("This is x: " + x); } } listing 5 /* Demonstrate a block of code. Call this file "BlockTest.java" */ class BlockTest { public static void main(String args[]) { int x, y; y = 20; // the target of this loop is a block for(x = 0; x<10; x++) { System.out.println("This is x: " + x); System.out.println("This is y: " + y); y = y - 2; }

1/2

} }

2/2

You might also like