Code Monkey home page Code Monkey logo

int-u4l3-23-24-student-exercises's Introduction

Lesson 4.3: Basic Conditional Statements

Conditional Statements

Conditional statements allow you to execute different actions under different conditions. They are a fundamental part of programming logic and are used to make decisions within your code.

Syntax of Conditional Statements

if (condition) {
  // statement 1
} else {
  // statement 2
}
  • if: The keyword to start the conditional statement.
  • condition: An expression that evaluates to true or false.
  • else: The keyword for the alternate path when the condition is false.

Examples

  1. Basic Example:

    let age = 17;
    if (age > 16) {
      console.log("You can drive!");
    } else {
      console.log("Sorry. Too young!");
    }
  2. Working with Strings:

    let weather = "raining";
    if (weather === "raining") {
      alert("☔️");
    } else {
      alert("☀️");
    }

Operators in Conditions

  • <: Less than.
  • >: Greater than.
  • <=: Less than or equal to.
  • >=: Greater than or equal to.
  • ===: Equal to.
  • !==: Not equal to.

Real-World Analogies

  • If it's raining outside, bring an umbrella; otherwise, bring sunglasses.
  • If travel time is more than 1 hour, read a book; otherwise, listen to music.
  • If it's a weekday, go to sleep at 10 PM; otherwise, stay up and play video games.

Happy coding! 😊

int-u4l3-23-24-student-exercises's People

Contributors

cn-curriculum avatar cn-mika avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.