From the course: Complete Guide to C++ Programming Foundations
Unlock this course with a free trial
Join today to access over 24,500 courses taught by industry experts.
If statements - C++ Tutorial
From the course: Complete Guide to C++ Programming Foundations
If statements
- [Instructor] Now let's get to know some control statements. These are parts of the code where execution may take different paths depending on some condition. Let's begin with if else statements. We're starting off with some variable declarations. An integer named score initialized at 1023, a Boolean named game paused initialized as false, and a char named action initialized as lowercase x. Now let's write some conditionals. The syntax for an if statement goes like this. First we type the if keyword, and then comes the condition which always goes between parenthesis. I'll compare score to 1000. If score is greater than 1000, I'll print a message on the screen. So after the conditional comes the block of code you want to execute when the condition is true. Notice that there is no then keyword. Now I want you to notice two things. First, if a block of code consists of exactly one line, there is no need for curly brackets. You may place them if you want, but they are not required. And…