Day 3: Intro to Conditional Statements


Terms you'll find helpful in completing today's challenge are outlined below, along with sample Java code (where appropriate).

Boolean

A logical statement that evaluates to true or false. In some languages, true is interchangeable with the number and false is interchangeable with the number .

Conditional Statements

These are a way of programming different workflows depending on some boolean condition. The if-else statement is probably the most widely used conditional in programming, and its workflow is demonstrated below:

if-then-else flow

Image Source: Wikipedia(Conditional Statements)

The basic syntax used by Java (and many other languages) is:

if(condition) {
    // do this if 'condition' is true
}
else {
    // do this if 'condition' is false
}

where is a boolean statement that evaluates to true or false. You can also use an if without an else, or follow an if(condition) with else if(secondCondition) if you have a second condition that only need be checked when is false. If the if (or else if) condition evaluates to true, any other sequential statements connected to it (i.e.: else or an additional else if) will not execute.

Logical Operators

Customize your condition checks by using logical operators. Here are the three to know:

  • || is the OR operator, also known as logical disjunction.
  • && is the AND operator, also known as logical conjunction.
  • ! is the NOT operator, also known as negation.

Here are some usage examples:

// if A is true and B is true, then C
if(A && B){ 
    C;	
}
    
// if A is true or B is true, then C
if(A || B){
    C;
}
    
// if A is false (i.e.: not true), then B
if(!A){
    B;
}


Another great operator is the ternary operator for conditional statements (? :). Let's say we have a variable, , and a condition, . If the condition is true, we want to be assigned the value of ; if condition is false, we want to be assigned the value of . We can write this with the following simple statement:

v = c ? a : b;


In other words, you can read c ? a : b as "if is true, then ; otherwise, ". Whichever value is chosen by the statement is then assigned to .

Switch Statement

This is a great control structure for when your control flow depends on a number of known values. Let's say we have a variable, , whose possible values are , , , and each value has an action to perform (which we will call some variant of ). We can switch between actions with the following code:

switch (condition) {
    case val0: 	behavior0;
                break;
    case val1:	behavior1;
                break;
    case val2:	behavior2;
                break;
    default: 	behavior;
                break;
}

Note: Unless you include break; at the end of each case statement, the statements will execute sequentially. Also, while it's good practice to include a default: case (even if it's just to print an error message), it's not strictly necessary.

Additional Language Resources
C++ Statements and Flow Control
Python Control Flow Tools



Solve Problem