Professional Documents
Culture Documents
True False
Condition
Do Step A Do Step B
• In C++, there are three types of selection
statements:
• one-way
• two-way
• multi-way
One-Way Decisions
Executed when
condition is true
Executed when
condition is false
If statement (Two-Way)
- Syntax of if with single-statement body
Executed when
condition is true
Executed when
condition is false
• Example 1:
• int c = 10;
•
• if (c >= 0)
• {
• cout << "c is a positive or neutral integer" << endl;
• }
• else
• {
• cout << "c is a negative integer" << endl;
• }
• Example 2:
• int num = 100;
•
• if (num % 2 == 0)
• {
• cout << "num is an even integer" << endl;
• }
• else
• {
• cout << "num is an odd integer" << endl;
• }
• Practical
Examples…. (if, Two-way)
Write a program that squares a number (entered
by user), if it is between 10 and 100. For all other
numbers, an Error message is shown and program
terminates.
Examples…. (if, Two-way)
2. Write a payroll program using following rules:
– Hourly rate: 100 (rupees)
• Multi-way decisions are used to evaluate a logical expression that could have several possible values.
• “if / else if” statements are often used to choose between ranges of values
• if ( logical expression )
• {
• stmtT1;
• }
• else if ( logical expression )
• {
• stmtT2;
• }
• else if ( logical expression )
• {
• stmtT3;
• }
• else if ( logical expression )
• {
• stmtTN;
• }
• else
\01
else if statement
The form of a multi-way decision using
“nested if” :
• Purpose: To test more than one factors before we
write our executable code
return 0;
}
The else...if Construction
if (marks>80)
{
cout<<“\n You got A grade”;
cout<<“\n You won scholarship too”;
}
else if (marks>70)
cout<<“\n You got B grade”;
else if (marks>60)
cout<<“\n You got C grade”;
else if (marks>50)
cout<<“\n You got D grade”;
else
cout<<“\n You are fail”;
The Conditional Operator
• This operator consists of two symbols, which
operate on three operands.
• Is same as we write:
The Conditional Operator
is equivalent to
y = (x > 0) ? 1 : -1;
Syntax:
result = (condition) ? Expression1 : Expression2;
Conditional Operator, examples
switch (ch) {
case 'a': cout <<“ ch contains a”;
case 'b': cout <<“ ch contains b”;
case 'c': cout <<“ ch contains c”;
}
switch statement (with break)
Suppose ch is ‘b’
switch (ch) {
case 'a': cout <<“ ch contains a”; break;
case 'b': cout <<“ ch contains b”; break;
case 'c': cout <<“ ch contains c”; break;
}
cout<<“\n End of program…”;
Switch – Example-1
char grade;
cin>>grade;
switch (grade)
{
case ‘A’: tution_fees *= 0.20;
break;
default: tution_fees *= 1;
}
Switch – Example-3
int a, b; char key;
cout<<“\nEnter numbers: “;
cin>>a>>b;
cout<<“Enter an arithmetic operator”;
cin>> key;
switch (key)
{
case ‘+’ : cout<<(a+b); break;
case ‘-’ : cout<<(a-b); break;
case ‘*’ : cout<<(a*b); break;
case ‘/’ : cout<<(a/b); break;
default: cout<<“Error: Invalid key…";
}
Logical Operators
switch(marks)
{
case 50 : cout<<”Passed”;
case 60 : cout<<”Got C”;
case 70 : cout<<”Got B”;
case 80 : cout<<”Got A”;
}
Class Exercise-2
int n = -29;
int temp = (n<0)?-n:n;
cout<<temp;
Class Exercise-3
int x = 5, y = 30;
if(y/x > 2)
if(y % x !=2)
x = x + 2;
cout<<x<<”\n“<<y;
homework
1I
2 II
3 III
4 IV
homework
Write a program that ask to input value in seconds.
Then the program converts the number of seconds
into days, hours, minutes, and seconds value. In the
end, the program shows the output in the following
format: