Without running the program, figure out the values of n and k in the end of the program. Then test yourself by running the program.

// This program demonstrates if/else statement

public class IfElseQuestion {

    public static void main (String [] args) {
	// For simplicity the numbers are fixed, 
	// not obtained as input
	int n = 5, k = 3;
	
	if (n < 5 || n == k + 2) {
	    n = 3;
	}

	if (n < 5 && k % 2 == 1) {
	    k = k + 1;
	} else {
	    n = n - 1;
	}

	System.out.println("n = " + n + " k = " + k);
    }
}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.