🖥️
Intro to Computer Science (ICS3U/C)
  • An Introduction to Computer Science
  • Videos & Slides
  • Unit 1: In the Beginning
    • The History of Computers
    • Binary & Logic
      • Bits and Bytes (Binary)
      • Transistors (Changing Bits)
      • Logic Gates
        • Poster
        • Logic.ly
    • The Parts of a Computer
  • Unit 2: Intro to Code
    • How Do We Code?
      • Coding Conventions (Rules)
      • Commenting Code
    • What is HTML?
      • Hello World! (in HTML)
      • HTML Slideshow
    • Hello World!
    • Input / Output
      • The Console
      • Prompt, Alert, Confirm
    • Variables & Data
      • Strings (Text)
      • Numbers (Values)
        • Converting & Rounding
        • The Math Object
          • Random Numbers
      • Booleans
        • Truthiness
      • Arrays
  • Unit 3: Control Flow
    • Conditionals (if this, do that)
      • If...Else
        • Logical Operators
      • Switch / Case
      • Ternary Operators
    • Loops (Repeating Code)
      • For...Loop
      • While & Do/While Loops
    • Debugging
  • Unit 4: Functions
    • Functional Programming
    • User Defined Functions
      • Hoisting and Scope
    • Calling a JS Function
  • TL;DR
    • Programming Basics
    • Slideshows & Demos
    • Javascript Syntax Poster
  • Advanced Topics
    • Recursion
    • Structures & Algorithms
    • Mmm... Pi
  • External Links
    • Typing Club!
    • repl.it
    • Khan Academy
    • Geek Reading
    • ECOO CS Contest
Powered by GitBook
On this page
  • Syntax
  • Example 1: If
  • Example 2: If...else
  • Example 3: If...else if
  • Example 4: If...else if...else
  • Example 5: With Numbers
  • Important Notes
  1. Unit 3: Control Flow
  2. Conditionals (if this, do that)

If...Else

Do something based on a boolean expression.

If you're cold, put on a sweater. If you're hot, take the sweater off. Simple, right?

In code, we need to make several thousand decisions per second. Think of a video game that checks for physical collisions or what happens when you click a certain key on the keyboard. The control of data, or flow control, can be determined based on boolean logic. if a particular value is true, do something. Otherwise, or else, do something else (or nothing at all).

Syntax

Same syntax for C++ , Java, Javascript, and many other languages.

if (booleanExpression1) {
   // code to be executed if true
}
else if(booleanExpression2) {
   // booleanExpression1 is false and booleanExpression2 is true
}
else if (booleanExpression3) {
   // booleanExpression1 and 2 are false and booleanExpression3 is true
}
.    // "else if" statements are optional and you can have infinitely many
.
else {
   // code to execute if all test expressions are false (optional)
}

Python syntax is a bit strange different - there are shortened words, it uses white space to exit a block of code, and notice the placement of colons:

if booleanExpression1:
   # statement(s)
elif booleanExpression2:
   # statement(s)
elif booleanExpression3:
   # statement(s)
else:
   # statement(s)
   
#  Note: "elif" and "else" statements are optional

If...else statements are the most basic tool for creating multiple pathways of code. There can also be shortcuts if you are comparing a simple boolean or want to combine items with And or Or boolean logic.

// C++ ternary example
int a, b = 2, c = 3;
a = (b > c ? b : c);   // since b > c is false, a takes the value of c (3)

Example 1: If

Only do something if the condition is true.

let someNumber = 5;

if (someNumber < 0) {
    console.log("That's a negative number");
}

// The above code will do absolutely nothing because 5 is NOT less than 0

Example 2: If...else

Example 3: If...else if

We can make a secondary check on a condition if the first condition equates to false. In this example, our third statement equates to true, so it is run. Notice how there is no default set of code in case none of the conditions equate to true:

let myName = "Karen";

if (myName == "Lola") {
    console.log("Her name was Lola...");
}
else if (myName == "Jackie") {
    console.log("You mean, like Jackie Chan? Awesome.");
}
else if (myName == "Karen") {
    console.log("What a lovely name!");
}
else if (myName == "Robert") {
    console.log("His name was Robert Paulson...");
}

// This will output: What a lovely name!

Example 4: If...else if...else

Example 5: With Numbers

Important Notes

While all of the examples above do a simple console.log() it is important to note that you can do whatever you like inside the code block brackets { }. You can even nest another if statement, loop, function call, or more.

PreviousConditionals (if this, do that)NextLogical Operators

Last updated 4 years ago

There is something called the operator. It is used for simple yes/no situations and is worth learning.

Run to see how we can do something else if the condition is false:

Run to see multiple options and consider how this sort of conditional code can be used.

Finally, just to get the message home, let's instead of text.

While I would love to continue with more examples, .

ternary
the following code
this second example
control things based on a number
they are very easy to find online
https://replit.com/@MatthewBrash/If-Else-Example?lite=truereplit.com
Example 2: If...else
LogoSign up to continue codingreplit
Testing Tech.io instead of Repl.it Tech.io is based on Node.js, so no prompt() or alert()