Assignment 35; Else And If
Code
// Donovan Rich
// Period 6
// Else And If
// ElseAndIf
// 10/7/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 ) // the above statement moves to the else if statement below if its conditions are not met.
// the program then checks to see if the conditions within "else if" are met, and if they are not,
// the program moves to "else" and does whatever is braced by the else statement.
{
System.out.println( "We should not take the cars." );
}
else
{
System.out.println( "We can't decide." );
}
if ( buses > cars )
{
System.out.println( "That's too many buses." );
}
if ( buses < cars ) // Removing the "else" will cause the program to check this line regardless of whether or not the above if statement's
// conditions are met.
{
System.out.println( "Maybe we could take the buses." );
}
else
{
System.out.println( "We still can't decide." );
}
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