Dev C++ If Else Örnekleri

So we've learnt how to collect basic data from the user, but wouldn't it be useful if we could do different things depending on what the user typed in? Well this happens to be a very core concept of computer programming, and we can do exactly as previously described with these things called 'if' statements. These are basic statements which allow us to do certain things only in certain conditions.

The first thing we're going to learn about is the 'if' itself. Just write the if keyword and then in some brackets, the condition. To specify the condition you simply write one value (either a variable or constant), then the comparison operator you want to compare them with (for example - equal to, which is ) and then the second value (either a variable or constant). We then put some curly brackets, and anything inside the curly brackets is what will be executed if the condition is true. For example, the following would compare 1 to 1 (which is a bit silly, but gives an example which is obviously always true):

Dev C++ If Else Örnekleri

Note that the value that you are comparing the second thing to must match the type of the first thing - for example, if comparing a string you must either compare to a string variable, or to a string constant (and remember that string constants are always shown in double quotes). In our example, however, one always equals one, so it's not much of a condition -- we can use variables to actually create a somewhat useful condition:

If else statement in C Sometimes you have a condition and you want to execute a block of code if condition is true and execute another piece of code if the same condition is false. This can be achieved in C using if-else statement. When using if, else if, else statements there are few points to keep in mind. An if can have zero or one else's and it must come after any else if's. An if can have zero to many else if's and they must come before the else. Once an else if succeeds, none of he remaining else if's or else's will be tested. Program pernyataan if biasanya selalu berdampingan dengan fungsi else fungsi else hanya bisa berjalan saat menggunakan fungsi if. Apa fungsi else dalam program c? Else berfungsi untuk menerima nilai jika pernyataan if yang dibuat tidak memenuhi persyaratan dari program yang dibuat atau bisa dikatakan else adalah fungsi yang menerima selain.

In this case the program would output 'Wow, I'm 16 too!' if the user entered the value 16, but would not output anything if the user inputted any other number. We can also compare any two variables using the same method:

Dev C++ If Else Örnekleri

The issue we have at the moment, is that in most programs we aren't always going to want to just check if something is equal to something else. What if we wanted to check if something was less than, or greater than, or not equal to something else? Well luckily there are other comparison operators we can use instead of just being restricted to the 'is equal to' operator (). 'Less Than' (<) and 'Greater Than' (>) are relatively simple - they are simply their usual symbols, and so we could check if the user's height is greater than their age like this:

We can also do 'Greater Than or Equal To' and 'Less Than or Equal To' by simply adding a single equals sign after the appropriate symbol. For example, we could check if the user's height was less than or equal to their age like this:

There are also the simple 'equal to' and 'not equal to' conditional operators. We already know 'equal to' as , and 'not equal to' is an exclamation mark followed by an equals sign: !=. So we could check if the user's height doesn't equal their age like so:

Dev C If Else Ornekleri 1

Ok, so now that we can compare two values pretty well - what if we want to do a variety of things depending on different conditions? For example if we wanted to do one thing if their height and age were equal, and if they aren't then do something else if another condition is met. Well we can accomplish this using 'else if'. You can just write else if after your closing curly bracket for your original 'if' statement, and then specify your 'else if' condition followed by the curly brackets to contain the code to be executed if they are met. For example:

Notice that it's very easy just to string together 'if's and 'else if's, in this case I've just chained two 'else if's off my original 'if'. We can also specify something to do if none of the conditions are met (in our example above this wouldn't really be useful since it's impossible not to meet any of the conditions, but it's generally good practice to put an else in just in case something goes seriously wrong). We can do this using the else keyword, and then some curly brackets to specify the code that could be executed at the end of our 'daisy chain':

A great example of 'daisy chaining' these all up is to create a program which asks for a student's test score, and then spits out the grade that they got. Try and create such an application yourself and see how you do. One solution to the problem is as follows, however it's worth noting that repeating this much code should be making your 'bad code' sense tingle! It goes against the 'Don't Repeat Yourself' principle of programming, but for now, a solution like this will have to do:

Sometimes when creating a C++ program, you run into a situation in which you want to compare one thing to a number of other things. Let's say, for example, that you took a character from the user and wanted to compare this to a number of characters to perform different actions. For now, let's just say that you have three commands which operate from the keys: 'h', 'e', and 'q'.

We can use an if-statement to check equality to 'h', 'e', and 'q' - remember that we use single quotes because we're dealing with chars, not strings. So let's just write the code in the way we're used to (and let's wrap a while loop around it so it keeps getting a character and acting upon what it was, because that makes our program slightly better and easier to test), using if-statements, like the following:

This program should be something you're comfortable with creating by this point, however your programmer instincts should also be kicking in and telling you that you shouldn't be repeating so much code here. One of the core principles of object orientated programming is DRY: Don't Repeat Yourself. In this program we're having to repeat a lot of code for the 'else if's including using ch every time we're doing a comparison.

The solution to this 'problem' is what this tutorial is all about: switch statements. These are essentially just a really nice way to compare one expression to a bunch of different things. They are started via the switch keyword, and from there comparisons are made using a case: syntax. It isn't easily described in words, so take a look at the syntax below:

The different cases essentially act as many if/else-ifs in a chain, and an 'else' type clause can be specified by using default:

Dev C++ If Else

This type of functionality should be reasonably easy to understand with if-statements securely under your belt, and so if you're feeling brave - try porting the basic program we created at the start of this tutorial to use if-statements. If you're not quite that brave, the cleaner and neater switch version of the code is below: