If Statement #35 (If, Else If, Else)

Code

       /// Name: Amir Salehi
    /// Period: 7
    /// Program name: ElseAndIf
    /// File name: ElseAndIf.java
    /// Date: Finished 10/5/2015
public class ElseAndIf
{
	public static void main( String[] args )
	{
		int people = 30;
		int cars = 40;
		int buses = 15;

		if ( cars > people )
		{
			System.out.println( "We should take the cars." );
		}
		else if ( cars < people )
		{
			System.out.println( "We should not take the cars." );
		}
		else
		{
			System.out.println( "We can't decide." );
		}
//Remove the else part at the beginning of one of the else if statements. What difference does that make? None. Why? I think that the code thinks that the "We can't decide" function is part of the else if function also.

		if ( buses > cars )
		{
			System.out.println( "That's too many buses." );
		}
		else if ( buses < cars )
		{
			System.out.println( "Maybe we could take the buses." );
		}
		else
		{
			System.out.println( "We still can't decide." );
		}
//What do you think else if and else are doing? I think it's saying if this If statements doesn't work the else statements will counter act on that a a vice versa equation.

		if ( people > buses )
		{
			System.out.println( "All right, let's just take the buses." );
		}
		else
		{
			System.out.println( "Fine, let's stay home then." );
		}

	}
}
    

Picture of the output

Assignment 7

Back to Homepage