Conditional Statements

Arkadiusz Włodarczyk
A free video tutorial from Arkadiusz Włodarczyk
Excellent teacher, Expert in Programming
4.4 instructor rating • 18 courses • 249,763 students

Lecture description

We learn what are conditional statements. We learn what are curly braces used for {}. We learn about nested conditional statements.

Learn more from the full course

C++: From Beginner to Expert

Designed for people who don't have any knowledge about the programming and want to program in C++

15:44:00 of on-demand video • Updated November 2020

  • How to use C++ language in action
  • What is compiler / IDE / Variables / types of variables etc.
  • How to work with files - fstream library (i/o operation on files)
  • operators - arithmetic, assigment, logical, bitwise
  • conditions like if / else / switch
  • arrays / multi-dimensional arrays
  • loops - for / while / do-while
  • functions, overloading functions, passing variables to functions etc.
  • structures
  • referencers
  • pointers
  • dynamic allocation of memory
  • creating project in IDE
  • classes
  • object oriented programming
  • class and function templates
  • namespaces
  • exceptions
  • input / output streams and validation of data
  • STL: vector
English Hello everybody. Today we are going to talk about conditional statements instructions. Conditional instructions are used to condition/check if something happened. Conditional instructions let us execute/perform some block of instructions only if some requirements are met OK so if the condition is met which means condition is true which also means that it is not equal to zero some block of code will be executed. Otherwise the code will be omitted/skipped. In programming language it looks like that. For example let's write following code int a, b; And now let's ask our user for the variable a. Now let's ask for variable b Now let's take it from the input. And now I want to execute some code only if for example value a variable a is greater than value of variable B. We don't know what values will be stored in that variables because that values will be assigned by user of our program. Right. So that's why it would be nice if we could for example check this situation. OK. So I will use conditional statement which looks like that: if parenthesis and inside parentheses I'll write Condition and condition is just anything that can be as final result 0 or any other number. So for example an expression so if I write a is greater then b and after it I will write any instruction that will be executed ONLY if that condition is met. So for example cout a is greater than b So we are sure that we can tell our users that A is greater than B because that condition must be met in order to run the instruction here. So now I can compile the guy and I'll enter for example 10 and enter as b as 7. And as you can see that instruction is now here, on our output. So, 10 is greater than B Ok so so everything was OK but what if I run it again And now I will type for example 4 now I'll type for example 25 as you can see that instruction was omitted or skipped right because a is not greater than b, well wasn't greater than b. Now we'll test a few more things and that thing will slow us a bit. So, let's just comment this, let's comment that things here And let's do something like that. If seven is greater than 4. cout << "something"; OK. And now let's cout here: test What I want to tell you now. Look what if I wanted to execute more than ONE line of code only if that guy here if that expression if that condition is met. Now as you can see both of that instructions got executed and it's OK but what if I do something like that, 4 is, 4 is greater than 7. Only that guy will be executed. Right. It might be a bit confusing when I write it that way Still only that guy will be executed. test But what if I want to execute that guy only when this condition is met. I need to use something like. Brackets are used to group many instructions. So as you can see we have got here brackets opening and closing one and that brackets are used to group that instructions here. These brackets here. Opening and closing one are here to group that instruction here because of the brackets that two instructions here are treated like the one instruction. That is why now when I build this program as you can see both instruction weren't shown. OK. Now as I said earlier anything anything, right that is not equal to zero. is true! So condition above is surely not equal to zero as you can see this thing That instruction got executed OK, we can use also not only relational operators but we can use the logical operators so I can do something like that If for example 7 is lower than 4, or 0 is lower than 10 show let's show that thing here, for example, and tell me about that instruction will it be executed? Yes because is zero lower than 10? Yes it is lower than 10 and this is alternative this is disjunction So we we don't need to think about that guy even. Even if it is false. The result of that operation will be 1 anyway right? If you do not understand it please repeat the subject about the logical operators. OK. As you can see everything is working fine. Now let's maybe make it a bit more confusing. Now I will add for example here conjunction and I'll say and 10 is equal to 9. That thing as a result will give us 0 that thing as a result will give us 1 conjuction is true only if both expressions are true or equal to 1 right? So as you can see the result. Final result is zero. And now when we build it we cannot see that guy here. OK. These were some exercises. Now I want to tell you about something very important. Let's comment that thing here now. And let's back to that guy here. We'll Assign values by ourselves For example like that. And what would happen. What would happen if I wanted to for example do something ELSE when that condition is not met. Then I can do something like that, add here else and say the condition a is greater than b surely wasn't met, right? and now is 100 greater than 10 of course it's greater than 10. So this instruction is executed. But when I do for example that thing, as you can see, the condition a is greater than b surely wasn't met, but we can do other things for example I can do something like that ELSE If a is lower than b cout a is lower than b And now I should change that guy, condition a is greater than B surely wasn't met and the condition A B surely wasn't met That instruction is executed only if all of the conditions weren't met. So right now as you can see minus 4 is lower than... is smaller than and that instruction was executed. But if I want to execute that guy, it must be, that condition cannot be met and that condition cannot be met So when will it happen, when that two variables are equal, right? So that instructions is executed only. if all the conditions weren't met. Maybe I should change this thing. This instruction will be executed only if all of the conditions weren't met. OK. So as you have probably noticed, we don't need to use that as 'if else' or 'else' thing. We can use as many as else here as we want to check many other things. Right? And that instruction after else will be executed ONLY when all of that conditions aren't met. And we should also know that we can nest so we can have many conditional statements when we use brackets. We can have nested conditional instructions for example that way: Now I can, for example, check if a is lower than for example 10 a is, I'm sorry, smaller, a is smaller than 10. Now when we build it as you can see these variables are equal but we can do for example something like that. and now we will execute that guy here, 10 smaller than 20. But this thing wasn't executed because a is equal to 10. It is not smaller than 10. Now when I do something like that as you can see everything is working fine. So we can nest our conditional statements. OK so since today we can decide what we want to execute and that's all in this lesson. Thank you very much.