From the course: C Essential Training

Conditionals

- [Instructor] A conditional statement allows you to run one block of code or another based on a condition. This is an example of a conditional statement in C . If the condition evaluates as true, then the statement-block will be executed. The statement-block may be one or more statements enclosed in curly braces. The if statement is the simplest form of conditional. When the condition is evaluated, it's found to be either true or false, if the condition is true, then the enclosed statement-block is executed and flow continues after the conditional. If the condition is false, then the flow continues after the conditional without executing the enclosed statement-block. An if statement may also have an else clause, which looks like this in C . Notice that there's a separate statement-block for the else condition. This block of statements is executed if the condition is evaluated as false. The if-else construct works like this. If the condition is true, then the statement-block is executed and the flow continues after the conditional. If the condition is false, then the else-statement-block is executed and flow continues after the conditional. In either case, one and only one of the blocks of statements will be executed. Let's take a look at these statements in practice. This is working.cpp from chapter three of the exercise files. I'm just going to come in here and I'm going to create a little if statement. I'm going to create a couple of variables, x and y. And a little if statement. And we'll just change this to say after. Now when I build and run this, you'll notice that it says condition is true and after. And if I change the conditional, instead of x is greater than y to x is less than y, then you notice that the condition is not true, so it just goes straight to after and it doesn't run the statement-block because the condition x is less than y is false. X is not less than y. X is 42, y is 7. So I can create an else clause. And in the else clause, I can say condition is false. And now I don't need this after anymore. And I can build and run this and it says condition is false because x is not less than y. There's a few different relational operators. We have greater than and less than, which we've seen. We also have equal to, which is two equal signs. And of course, that is false. We can do less than or equal to like this. And that's false. We can do greater than or equal to. And that should be true. And we can say not equal to, which should also be true. What's interesting here is that this is called a Boolean expression. It returns a Boolean value of either true or false. In C , you have keywords true and false. So I can say if true, and of course, I get these warnings of unused variables and code will never be executed. Or I can say if false, and I get condition is false. I can use a one or a zero. The number one is always true. And number zero is always false. Build and run that. It says false. In fact, anything that is not zero is considered true. So if I say 42 here, it is true. The empty string is nonzero because in C , that's actually an address. So I can build and run and it says condition is true 'cause it's nonzero. Only a zero value is considered false and that's why we have these keywords of true and false like that. It's worth noting that the statement-block may be a single statement, like this. We'll go back to x is greater than y. And of course, we have it in a block like this. It says condition is true, right? Well, since it's just a single statement, we can, and you'll often see this, we can simply put it on a line like this without the curly braces. Now, some people consider this poor practice because it's easy to modify this and end up with confusing code. In my view, as long as it's all on a line by itself like this, then you don't need the curly braces. When you go and want to put it on the next line like this, then I strongly recommend you include the curly braces so that when you add lines, you don't end up accidentally putting those lines outside of the conditional block even though they're still indented. And so if there's only one statement, and you put it on the same line as the condition, then I don't consider this bad practice but many people do. But you'll see this a lot. Just a little one-liner with a condition like that. There's also a conditional operator called the turnary conditional operator. It's a simple shortcut for choosing a value based on a condition. And it looks something like this. Auto s equals x greater than y, question mark yes, colon, no. And so what this does is it evaluates the condition, and you have this question mark that separates the condition from the results. If the condition is true, it'll return the first value and if the condition is false, it'll return the second value. And so it'll assign one of those yes or no to s and then I can print it like this, and I can say the answer is yes. And obviously, if I change the relational operator to a less than, then the answer is no. So this is called the turnary conditional operator because it takes three values: a condition, which is this value here, a true value, which is this one here, and a false value, which is this one. So it takes all three of those values with the question mark and the colon. If the condition is true, it returns the true value, and if the condition is false, it returns the false value. The condition may be any condition that resolves to a Boolean value. This can be very handy in cases where you just need to select a single value and you don't need the complexity of a block-oriented is-else construct. The turnary conditional operator is a common shortcut for testing a simple condition with a simple result. It looks a little weird if you've not seen it before but it's actually quite simple and quite common.

Contents