Conditions in CPlusPlus

Conditions in CPlusPlus

conditions
Image credit:www.functionx.com

When we talk about Conditions in CPlusPlus, our focus diverts entirely towards the checks that have been made through the development of our program.

What are conditions

Conditions are very important features of C++ programming. For example, if I am driving a car and just closer to the bridge, I hear some voices that the bridge is in bad shape, kindly follow another path.
Now what? When we heard that the bridge has been broken, we simply change our decisions, based on the condition that if bridge is broken, take another route, else drive through the bridge.Same situation can be applied in C++

The if condition

In any condition (including the if condition), some operators that we require are given below:

> greater than
< less than >= greater than or equal
<= less than or equal == equal to != not equal to

Let us elaborate it with the help of a program

#include “stdafx.h”
#include <iostream>

using namespace std;

int main()
{
// initializing two variables a an b with values 12 and 14 respectively
int a=12, b=14;

//if a is greater than b then goto if body
if(a>b)
{
cout << “a is greater than b”;
cout<<endl;

}

/* here we have used the technique of else if. It is not something strange.
The else encourages the next line consists of if check

*/
else if(a<b)

{

cout << “a is lesser than b”;
cout<<endl;

}

/* else is the keyword that tells the compiler that if the condition
is not satisfied then execute else blog */
else

{
cout << “a is not greater than b”;
cout<<endl;

}

Output

a is lesser than b

The Switch statement

A thought often comes in developer’s mind about the Switch statement. What are the benefits of it and why if and else are not used
Let’s read this excellent answer to this

1. Cleaner code. Code with multiple chained if/else if … looks messy and is difficult to maintain – switch gives cleaner structure.
2. Performance. For dense case values compiler generates jump table, for sparse – binary search or series of if/else, so in worst case switch is as fast as if/else, but typically faster. Although some compilers can similarly optimise if/else.
3. Test order doesn’t matter. To speed up series of if/else tests one needs to put more likely cases first. With switch/case programmer doesn’t need to think about this.
4. Default can be anywhere. With if/else default case must be at the very end – after last else. In switch – default can be anywhere, wherever programmer finds it more appropriate.
5. Common code. If you need to execute common code for several cases, you may omit break and the execution will “fall through” – something you cannot achieve with if/else. (There is a good practice to place a special comment /* FALLTHROUGH */ for such cases – lint recognises it and doesn’t complain, without this comment it does complain as it is common error to forgot break).



reference: http://stackoverflow.com/questions/1028437/why-switch-case-and-not-if-else-if

#include “stdafx.h”
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
int _var=1;

switch (_var) {
case 1:
std::cout << "x is 1"; break; case 2: std::cout << "x is 2"; break; case 3: std::cout << "x is 3"; break; default: std::cout << "x is not 1, 2 nor 3"; break; } return 0; } Note: we always use break after the statement in C++ so other statements won't get executed when our condition is satisfied

Output
x is 1

Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.