Skip to main content
Module 2~8 min

Conditional Statements

Programs often need to make decisions. Conditional statements let code choose different actions based on whether something is true or false.

What You'll Learn

  • What a conditional statement is
  • How if/else works
  • Simple examples of conditions

Making Decisions in Code

A conditional statement runs different code depending on whether a condition is true or false, letting a program react differently to different situations.

If and Else

The most common conditional is `if`, which runs a block of code only when its condition is true. An `else` block can run instead when the condition is false.

Example

A program checking login attempts might say: if the password is correct, grant access; else, show an error message.

Key Concepts

ConditionalIf/ElseCondition

Key Takeaways

  • Conditional statements let a program choose between different actions
  • `if` runs code only when a condition is true
  • `else` provides an alternative when the condition is false

Knowledge Check

What does an `if` statement do?

What does an `if` statement do?