- In everyday life, we are often making decision.if the age of the person is above 60, then discount the ticket price to 40%
- The above statement have element of decision making. E.g only if the age is above 60 reduce the ticket by 40%. If the criteria is not matched the ticket shall not be reduced by 40%.
- This decision making process is everywhere in our daily life.
Control Structure
Everyday programming requires three control structures:
- Sequence
- Selection
- Iteration
Sequence
- Executing instructions in sequence
- Working down the pag
- C / C++ follow this sequence of control until some other is specified via return, if, while, do / while, etc.
- Statements executed one after the other in order in which they are written
- The statement used for decisions in C++ language is known as the ‘if statement'
- If statement has a simple structure and it helps to choose among alternative courses of actions.
Structure of if statement
if (condition) statement (or group of statements);
The above statement means , if condition is true, then execute the statement or a group of statements.
Here the condition is a statement which explains the condition on which a decision will be made.
Example
if ( the grade is greater than or equal to 60)
pass the student ; We write the condition in parentheses, followed by a statement of group of statements to be executed.
We use braces { } to make a group (block) of a number of statements.
We put ‘{‘ before the first statement and ‘}’ after the last statement
Syntax
if (condition)
{
statement;
statement;
.
.
statement;
} // if statement is only executed when the value is true
Example
Comparing the ages of two students. If the age of first student is greater than second, then display “ student 1 is older”#include <iostream>
main()
{
int age1, age2;
age1 = 12;
age2 = 10;
if (age1 > age2)
cout<<“Student1 is older:";
}
Realtional Operators
ExampleWrite a program in which users enter a number. If the user enter positive number, add one to the number else add two to the number.
# include<iostream>
main()
{
int num, neg ,pos;
cout<<“Please enter the no.”;
cin>> num;
if (num > 0)
pos= pos +1;
else
neg= neg +2;
}
Conditional Operator
- Closely related to if/else structure
- ?: (Conditional Operator)
- Only ternary operator in C/C++
- Takes three operands
Operand 1– condition
Operand 2 – action if condition true
Operand 3 – action if condition false
Example
grade >= 60 ? “Passed”: “Failed”Nested if Statements
Used for multiple conditions and take some actions accordingly to each condition. Example, if (condition 1 )
Statement1
else if (condition 2 )
Statement2
...
else if (condition N )
StatementN
else
Statement N+1
EXACTLY 1 of these statements will be executed
Example of grades (if only)
if (grade==‘A’)cout<<“Excellent”;
if (grade==‘B’)
cout<<“Very Good”;
if (grade==‘C’)
cout<<“Good”;
if (grade==‘D’)
cout<<“poor”;
if (grade==‘F’)
cout<<“Fail”;
‘if Statement” is computationally one of most expensive statements in program due to the fact that processor has to go through many cycles to execute them for a single decision.
To avoid this , and alternate of multiple if statements can be use of if/else statements.
//nested if/else
//printing remarks for each grade
if (grade == ‘A’)
cout<<“Excellent”;
else
if (grade == ‘B’)
cout<<“Very Good”;
else
if (grade == ‘C’)
cout<<“Good”;
else
if (grade == ‘D’)
cout<<“Poor”;
else
cout<<“Failed”;
In the ‘nested if statements’ the nested else is not executed if the first if condition is true and the control goes out of the if block.
Switch Statement
Multiple- selection construct that is used in multi decision making in an efficient way and is easy to read and understandStructure Switch Statement
switch (expression)
{
case constant1:
group of statements 1;
break;
case constant2:
group of statements 2;
break;
.
.
.
default:
default group of statements
}
How switch works?
switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes group of statements 1 until it finds the break statement. When it finds this break statement the program jumps to the end of the switch selective structureIf expression was not equal to constant1 it will be checked against constant2. If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure
Finally, if the value of expression did not match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the default: label, if it exists (since it is optional)
Example
// counting grades assigned
int aCount, bCount, cCount, dCount, fCount;
switch (grade)
{
case ‘A’: case ‘a’:
++aCount;
case ‘B’: case ‘b’:
++bCount;
case ‘C’: case ‘c’:
++cCount;
case ‘D’: case ‘d’:
++dCount;
default:
++fCount;
}
break Statement
break statement causes program control to move to the first statement after the selection/ repetition structureSwitch cases would otherwise run togethe.
No comments:
Post a Comment