Wednesday, 12 November 2014

Control Structures – Conditional statements in C++ with the Examples

Control Structures – Conditional statements in C++ with the Examples:

As of we know that a program is not a linear sequence of instructions. While in the process there are situations that will take the decisions, it may split or it may use repeated code.

To handle this type of scenarios in C++ we have the control structures.
As we know that the program is a list of statements that are separated by semicolons (;).
 All the statements are enclosed with the curly brasses { }.
 Here is the syntax:
int _tmain (int argc, _TCHAR* argv[])

{  // start of the main body

 ClsFirst ocFirst;  // statment1

 ClsSecond ocSecond; // statement 2

 ocFirst.Dump ("Before"); // statment3

 ocSecond.Update (ocFirst, 5);  // statment4

 ocFirst.Dump ("After");  // statement 5

  getchar (); 

  return 0;

}// end of the main body




 Similarly, all the control structures are also enclosed with the curly brasses, which is called as block.

In C++ we have 3 types of control structures:




Conditional structure:

The if-else statement is called as conditional structure. the keyword if is used to execute the bock of code that is enclosed with the condition, only when the if condition is fulfilled/true.
 The syntax of the if statement is
If (condition) {

Statment1..
.
.
.
.
Stament N;
}


If there is only one statement need to be executed after the condition, there is no need of placing the statement in the {}
 if (condition) statement 


The below is the sample that will explain us to understand the if condition



In the above case the if condition is true then only the print statement is executed. Otherwise, the statement is not executed and the program continues right after this conditional structure.



If we want more than a single statement to be executed in case that the condition is true we can specify a block using braces { }:



if –else statement:

It is possible we can specify that if condition fails what to be done using the else statement.
The below is the pictorial representation of the if-else statement


 Below is the syntax of the if-else statement:
if (condition) {

 statement - 1
…..
statement - n
}
else  {
statement
}


Below is the example for if-else condition:


prints on the screen x is 10 if indeed x has a value of 10, but if it has not -and only if not- it prints out x is not 10.
The if + else structures can be concatenated with the intention of verifying a range of values. The following example shows its use telling if the value currently stored in x is positive, negative or none of them.

Nested if else Statement:
These types of statements are useful to handle the multiple conditions.
The syntax of the nested if statements are as follows
if(condition 1)
 statement1;

 else if(condition 2)

statement2;

else if (condition 3)

statement3;

else if (condition 3)

statement3;



 The following figure explains the flow of Nested if-else statement:



Let us see the below sample code that explains more about the nested if statements



Nested if-else statements with innermost if statement:



No comments: