Key Concepts

Review core concepts you need to learn to master this subject

Types of Conditionals

A conditional in C can be written using if, else-if, else, ternary operators, and switch statements.

Conditionals: Lesson
Lesson 1 of 1
  1. 1
    Every program we’ve seen so far has only had one possible path of execution — they all execute line by line, from top to bottom. And every time you run one of those programs, it gives you the same …
  2. 2
    Before we dive deep into the syntax of the if statement, let’s do a demo! title Here, we have a **coinflip…
  3. 3
    An if statement is used to test an expression for truth and execute some code based on it. Here’s a simple form of the if statement: if (condition) { // Statement(s) } If the condition is tr…
  4. 4
    We’ve already seen relational operators at work. Conditionals incorporate them in all their forms. For example, we can do something like: if (grade >= 60) { // Do something } In much the sa…
  5. 5
    We can also add an else clause to an if statement to provide code that will only be executed if the initial if condition is false. Here’s a form of an if statement that includes an else clause: if…
  6. 6
    So what happens if you want more than two possible outcomes? This is where else if comes in! if (condition) { // Some code } else if (condition) { // Some code } else { // Some code } The…
  7. 7
    Now that we know how if, else if, and else work, we can write programs that have multiple outcomes. Programs with multiple outcomes are so common that C provides a special statement for it — the sw…
  8. 8
    We’ve learned that the if / else statement is versatile. It’s so versatile that C actually has a built-in special way to shortcut an if / else statement into one line. And you don’t even need to us…
  9. 9
    Congratulations on completing the lesson on conditionals! 🙌 Let’s review some of the major concepts from the lesson: - An if statement checks a condition and will execute a task if that condition …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo