by Dinesh Thakur Category: Control Structures

We know that Instruction those are written in c++ Language are Executed in Sequence wise or Step Wise as they are Written in Program. Or these are executed in Sequence Order.Decision Making statements are used when we wants to execute the statements as according to the user needs. A User can Change the Sequence of the Statements for Execution.

 Many Times we Wants to Execute the Set of Instructions to be executed in one Situation and other Statements in other Situations for Executing the Statements in Specific Situation there are Some Decision Making Statements or Decision Control Statements those are provided by the C++ Language. In The Decision First a Condition is checked it is true then it Executes the next Instruction otherwise it Executes other Statements.

 

 There are three decision making statements :-

 

1) if

 2) if-else

3) The Conditional Operators.

 

Understanding If statement

 

The Keyword if tells the compiler what follows The Condition Following the Keyword if is always enclosed within a pair or Parentheses if The Condition id True Then the Statements is Executed if the Condition is not true then the Statement is not Executed For Checking a condition. Relational Operators are Used Like > ,< ,==, >=,<= etc. The if statement is widely used for checking a particular condition

Suppose you want to check the condition and then execute the condition

if your condition is met

for ex:

          if(a>b)

          then print a is greater then here we first check the condition

          that either a is greater than b if yes then execute the condition

 

1) if-else= the if Statement is used for Executing a Single Statement or a Group of Statements When the Condition Following is true. It does nothing when the Condition is False , For Executing the group of Statements either a Condition is False we uses else The if-else is similar to if but the difference is that is also provides us the alternative to execute the other statement if a condition is not true for ex:

          if(a>b)

                         cout <<a

          else

                         cout <<b

Here if first checks the condition either the a is greater than b if yes then it will print a suppose if a is not greater than b then it will be print b because we specify the b in the else statement.

 

2) if-elseif

The if-elseif is similar to the if-else but here the first if is used for checking a condition and the other elseif is used for checking a one more condition suppose if we wants to check the two or more conditions then we can use the if-elseif In This first if Statement Checks the Condition whether it is true or not if the first Condition is false then it jumps to the Second if Statement for Checking the next Condition so and so. if all the Conditions are false the Control will jump to the else block

 

3) Conditional Operator: - This Operates is also same like a if –else Statement., In this we first Checks the Condition and then this will gives us the Result after Checking the Condition. This Operators uses ? and : Signs . In this ? is used for Checking a Condition after Checking this will gives us the Results but if a Condition has Failed then this will Execute the Statements those are Written in the : ( Colon ) For Example

                         int a,b,c

                                      a=10

                                      b=20

                                      c= (a>b) ? a : b

                             Now here we are seeing that the value f a is Less than so this will Store the value of b variable into the C variable. Means c Contains 20.

 

Switch Statement:-This Statement is Similar to if –else but it uses matching operations rather than checking a condition if we Wants to Check Many Conditions then the Program will very Complex So in that Situation We Will use Switch Statement Switch Statement Uses Many Cases rather and it matches the value with Case where it Matches it will Execute the Statement But There is Very Necessary to Stop the Execution of Each Case So that it Doesn’t Execute Next Case So that for this Purpose we have to put the break Statement after Ending of an Each Case We Know if uses Else Statement if Condition is False or All Conditions are False The Code of Else Statement is Executed if Code of all if statements is false So that in Switch Default is used when all Cases are not Matched.

 



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles