3. C++ Control Structures

Control structures are a very useful programming idea. They help us execute a certain set of commands if a situation occurs. For instance, if a password that we ask a user to provide is correct, we evaluate it and present the user with a message. There are 3 primary control structures :
1. The if-else control structure
This is the simplest one. We can use it whenever we want to select between one or two states checking a certain condition that we have preset. If the condition is true, a group of commands gets executed, else another group of commands gets executed. This control structure is used this way :
#include <iostream.h> void main() { if (control structure) { commands executed if the condition gets evaluated as true } else { commands executed if the condition gets evaluated as false } }
Using the program below, you will be able to better understand the control structures in general. You can copy, paste and compile this example directly :
#include <iostream.h> #include <conio.h> void main() { int i; cout<<”Input an integer :”<<endl; cin>>i; if (i<9) // checks if i is smaller than 9 { // if the condition is true, this part is executed cout<<”This number is smaller than 9”; } else { // if the condition is false, execute this part cout<<”This number is bigger or equal to 9”; } getch(); }
When you execute this program, it will ask you to input an integer number. If this number is smaller than 9, the program will output the first message. If you input a number that is bigger than 9 or 9 itself, it will output the second message. Now, let’s dwelve on this command structure a bit more. At the time the program execution reaches the part where the control structure gets initiated, that is on the if part, the first thing that it will do is check the condition specified inside the parenthesis. In this example, it checks whether the value of the integer variable i is smaller than the integer number 9. If this condition is true, it executes the group of commands right below if inside the two characters { and }. If the condition is false, it goes on and executes the commands right after the else and inside the characters { and }. There is a case that there is no else in an if command. In that case, the program just keeps on with its execution. Using else, we just make sure that different execution will happen depending on a certain condition that comes up.
2. The if-else if control structure
This control structure is pretty similar to the previous one, but it has an important advantage. The choices that we can make are not limited to 2 but as many as the programmer wants. This control structure is formed like this :
#include <iostream.h> void main() { if (condition 1) { group of commands executed if condition 1 is true } else if (condition 2) { group of commands executed if condition 2 is true } else if (condition 3) { group of commands executed if condition 3 is true } else { group of commands executed if no conditions specified above are true } }
As in the previous control structure, when the program execution reaches the if command, it checks for the condition 1 inside the parenthesis. If it is true, the commands inside the {} right after the first if check are executed. If the condition is not true, then condition 2 is checked and so on. When we arrive at an else if command, the same procedure as if is made. In the end, if all control stuctures are false, the group commands inside else are executed.
2. The switch-break control structure
This control structure is exactly like if-else. The difference is that they are written in a different fashion. When lots of conditions need to be checked, this structure is probably the best choice. Here is how to use switch :
#include <iostream.h> void main() { int i; switch (i) // inside the parenthesis lies the condition variable { case 1: // if i = 1 this is executed and so on .. cout<<"1"<<endl; break; // breaks, exits the control structure case 2: cout<<"2"<<endl; break; case 3: cout<<"3"<<endl; break; case 4: cout<<"4"<<endl; break; case 5: cout<<"5"<<endl; break; default : cout<<" "<< endl; } }
Inside the parenthesis near switch, we put the value that we want to check against. It is actually like if-else if. When this value becomes the same as the value right after the case command, the commands after “:” are executed till “break;” is reached. Break instructs the program to stop the whole execution of the control structure and keeps going wit the next program commands. We have to use break to each case because if we don’t the program will keep on with the execution and execute the code that is inside the other cases as well. So, if no break was there and the value of i was 3, the cases for 3,4,6 and default would be executed. If none of the cases is true, the switch defaults to the “default” case and the code inside this is executed. Notice that default can be omitted.
Thanx for reading that, make sure that you read the second part about C++ Variables and Constants.
This was a guest post by a good personal friend of mine under the name Black Shadow.
I do not know whether it’s just me or if everybody else experiencing issues
with your site. It seems like some of the text on your content are running off the
screen. Can somebody else please comment and let me know if this is happening
to them too? This may be a issue with my browser because I’ve had this happen previously.
Appreciate it
I visit daily some sites and blogs to read content, however this website presents quality
based posts.
You actually make it seem really easy along with your presentation however I
in finding this topic to be actually something which
I feel I’d by no means understand. It kind of feels too complex
and extremely large for me. I’m having a look ahead to your next post,
I will try to get the grasp of it!
Spot onn with this write-up, I really believe this amazing ite needs far more attention. I’ll probably be back again to read through more,
thanks for the advice!
Awesome blog you have here but I was curious if you knew of any community forums that cover the same topics discussed here?
I’d really love to be a part of community where I can get feed-back from
other knowledgeable people that share the same interest.
If you have any recommendations, please let me know. Cheers!
I have to thank you for the efforts you have put in penning this website.
I’m hoping to see the same high-grade content by you later on as well.
In fact, your creative writing abilities has encouraged me to get my own, personal website now
😉