Selection Statement
Selection Statement
Syntax: Expression
True
if expression
statement_1
False
else Statement_1
Statement_2
statement_2
if-else statement
Example:
a) if (cgpa < 2.0 )
status = ‘F’;
else
status = ‘P’;
b) if ( choice == 1)
gender = ‘M’;
else
gender = ‘F’;
Example: if-else statement
#include <iostream.h>
void main()
{
int marks;
cout<< “Enter marks: ”;
cin>> marks;
if (marks >= 50)
{
cout<<“*******CONGRATULATION*******\n”;
cout<< “You pass the exam”;
}
else
cout<< “Sorry, you fail the exam”;
}
if-else-if statement
Syntax:
if (expression1)
statement1;
else if (expression2)
statement2;
:
:
else if(expressionn )
statementn;
else
last_statement;
If…else if statement
The if…else if statement is an extension of the "if else“
statement.
In this form, expression1 is first evaluated. If it is true,
statement1 is executed and the whole statement terminated.
On the other hand, if expression1 is false, control passes to
the else if part and expression2 is evaluated. If it is true,
statement2 is executed and the whole statement is
terminated.
If it is false, other else if parts (if any) are tested in a similar
way.
Finally, if expressionn is true, statementn is executed; if not,
last_statement is executed.
Only ONE of the statements will be executed
Example 1:if…else-if statement
#include <iostream.h>
void main()
{
int marks;
}
Example 2:if…else if statement
Write a program to test whether a banking
transaction is a deposit, withdrawal, transfer or
an invalid transaction, than for a valid
transaction enter amount.
#include<iostream.h>
void main()
{
float amount;
char transaction_code;
cout <<"\n****************************************";
cout <<"\n** Types of banking transaction : **";
cout <<"\n** D for deposit **";
cout <<"\n** W for withdrawal **";
cout <<"\n** T for transfer **";
cout <<"\n****************************************";
cout <<"\n\nEnter your transaction : ";
cin >>transaction_code;
if (transaction_code == 'D')
{
cout << "Deposit transaction";
cout << "\nEnter amount : ";
cin >> amount;
}
else if (transaction_code == 'W')
{
cout << "Withdrawal transaction";
cout << "\nEnter amount : ";
cin >> amount;
}
else if (transaction_code == 'T')
{
cout << "Transfer transaction";
cout << "\nEnter amount : ";
cin >> amount;
}
else
{
cout << "Invalid transaction!";
cout << "\nPlease enter the correct transaction code";
}
}
Nested if
generally take the forms:
if-if else.
Read string
cin.getline(firstString, SIZE);
Comparing string
The expression
strcmp(str1, str2)
void main()
{
const int SIZE = 20;
char firstString[SIZE], secondString[SIZE];
void main()
{
char gred;
cout<<"----------------------------------------------------------";
cout<<"\nEnter student grade for discrete math : ";
cin>>gred;
cout<<"\n**********************************************************";
switch (gred)
{
case 'A':
cout<<"\nYou deserve to take the principle of programming course.\n";
cout<<"Prerequisite are fulfilled.";
break;
case 'B':
cout<<"\nYou deserve to take the principle of programming course. \nPrerequisite are
fulfilled.";
break;
default:
cout<<"\nYou are not deserve to take the principle of programming course.";
cout<<"\nPrerequisite are not fulfilled.\nMinimun B in discrete math";
}
cout<<"\n**********************************************************";
}
Conditional Operator
It provides a shorthand method of expressing a simple if/else
statement.
Syntax:
expression ? statement1 : statement2;
Example:
(x < 0) ? (y = 10) : (z = 20);
The condition above is equivalent with
if (x <0)
y = 10;
else
z = 20;
Can be assigned to a variable:
result = x <= y;
Assigns 0 for false, 1 for true
Evaluate
Let x = 10 and y = -7
result = __________
Exercise
1. Write an if/else statement that assigns 1 to x if y is equal
to 100. Otherwise it should assign 0 to x.