Skip to content

Java Conditional Statements

Adrian Patterson edited this page Nov 23, 2021 · 1 revision

Conditional Statements

  • A conditional statement checks if a conditions is met, and acts based on if it is met or not

    • The condition could be comparing two values, checking if two values are equal, etc.
  • The if statement

    • An if statement allows a program to enter a body of code if the condition presented is met
    • E.g. suppose we have the following situation
     	int x = 5;
     	int y = 10; 
                 int z = 0;
     	
     	if(x > y)
     	{
     		// x was larger than y
                         z = x + y;
     	}
     	if(x < y)
     	{
     		// x was smaller than y
                         z = x - y;
     	}   
     	if(x == y)
     	{
     		// do even MORE stuff
                         z = x * y;
     	}
    • Which of these would execute?
  • Conditions we could use include

    • Greater Than                                     >
    • Less Than                                          <
    • Greater Than or Equal to                  >=
    • Less Than or Equal to                       <=
    • Equal                                                 ==
    • Not Equal                                           !=

Clone this wiki locally