7 -Conditionals

Get an introduction to or review of conditional statements in Java programming.

Code HuntJava
1.0x

7 -Conditionals

Created 3 years ago

Duration 0:05:15
lesson view count 5
Get an introduction to or review of conditional statements in Java programming.
Select the file type you wish to download
Slide Content
Tags: Code Hunt Java
  1. Conditional Statements

    Slide 2 - Conditional Statements

  2. Conditional Execution

    Slide 3 - Conditional Execution

    • To execute code only under certain circumstances, we use an if statement:
    • if (<condition>) {
    • <statements>
    • }
    • The condition can contain one of several relational operators:
  3. The body of the if statement will be executed only when the condition evaluates to true.  Otherwise, it will be skipped.

    Slide 4 - The body of the if statement will be executed only when the condition evaluates to true. Otherwise, it will be skipped.

    • Conditional Execution
    • if (3 < 5) {
    • System.out.println("Yes!");
    • }
    • if (10 == 12) {
    • System.out.println("OK!");
    • }
    • int x = 5;
    • if (x >= 0) {
    • x++;
    • }
    • System.out.println(x);
    • int x = 0;
    • if (x > 0) {
    • x--;
    • }
    • System.out.println(x);
  4. if-else statements

    Slide 5 - if-else statements

    • To execute one block of code if a condition is true, and another if the condition is false, we use an if-else statement:
    • if (<condition>) { <statements>
    • } else {
    • <statements>
    • }
  5. Exactly one of the two sets of statements will be executed in an if-else.

    Slide 6 - Exactly one of the two sets of statements will be executed in an if-else.

    • if-else statements
    • if (3 < 5) {
    • System.out.println("Yes!");
    • } else {
    • System.out.println("No!");
    • }
    • if (10 == 12) {
    • System.out.println("OK!");
    • } else {
    • System.out.println("Wrong!");
    • }
    • int x = 5;
    • if (x >= 0) {
    • x++;
    • } else {
    • x--;
    • }
    • System.out.println(x);
    • int x = 0;
    • if (x >= 0) {
    • x--;
    • } else {
    • x++;
    • }
    • System.out.println(x);