Conditionals
Learn about conditionals in C.
StartKey Concepts
Review core concepts you need to learn to master this subject
Types of Conditionals
if Statements
else-if Statements
else Statements
Dangling else Statement
Ternary Operators
switch Statements
Operators and Conditionals
Types of Conditionals
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
- 1Every 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 …
- 2Before we dive deep into the syntax of the if statement, let’s do a demo!
Here, we have a **coinflip…
- 3An 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…
- 4We’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…
- 5We 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…
- 7Now 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…
- 8We’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…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory