[go: up one dir, main page]

0% found this document useful (0 votes)
41 views19 pages

Programming Fundamentals (COMP1112) Selection Structure

The document discusses selection structures in C++ programming, including if, if/else, and switch statements. If statements execute code conditionally based on a boolean expression. If/else statements allow alternative code blocks to be selected based on a condition being true or false. Switch statements allow selecting between multiple cases based on a variable's value and provide a default option. Examples are provided of each type of selection structure.

Uploaded by

Abdul Hafeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views19 pages

Programming Fundamentals (COMP1112) Selection Structure

The document discusses selection structures in C++ programming, including if, if/else, and switch statements. If statements execute code conditionally based on a boolean expression. If/else statements allow alternative code blocks to be selected based on a condition being true or false. Switch statements allow selecting between multiple cases based on a variable's value and provide a default option. Examples are provided of each type of selection structure.

Uploaded by

Abdul Hafeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming Fundamentals

(COMP1112)
Lecture 4
Selection Structure

Division of Science & Technology


University of Education, Lahore.
Selection structure- a type of control structure
• Programs may not be limited to a linear sequence of statements.
• Selection statements take decisions and bifurcate the program.
• Types of selection structure include
• if
• If/else
• switch
if statement
•if keyword is used to execute a statement or block

Syntax:

if (condition) statement

Here, condition is the expression that is being evaluated.


If this condition is true, statement is executed. If it is false, statement is not executed (it is
simply ignored), and the program continues right after the entire selection statement.
Example:
int x=50;
if (x == 50)
cout << "x is 50“;
if statement (cont..)
• To include more than a single statement to be executed when the
condition is fulfilled, these statements shall be enclosed in braces
({}), forming a block
Example:
if (x == 100)
{
cout << "x is ";
cout << x;
}
• Limitation of if statement
What will happen when condition is false?
if/else structure
• Selection statements with if can also specify what
happens when the condition is not fulfilled, by using
the else keyword to introduce an alternative statement.
Syntax:
if (condition) statement1 else statement2

where statement1 is executed in case condition is true, and


in case it is not, statement2 is executed.
if/else structure (cont..)
• Example 1:
int x=90;
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
This prints x is 100, if indeed x has a value of 100, but if it
does not, and only if it does not, it prints x is not 100 instead.
Multiple if/else structure
• Example 2:
int x=1;
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else cout << "x is 0";

This prints whether x is positive, negative, or zero by concatenating two if-else


structures. Again, it would have also been possible to execute more than a
single statement per case by grouping them into blocks enclosed in braces: {}.
Nested ‘if’ structure
• If within if
• Control enters in inner if when the outer if condition is true
• User can use as many if statements inside another if as required
Syntax:
if(condition)
if(condition) {}
else {}
else
{}
Example: A Program that inputs three numbers and displays whether all numbers
are equal or not using nested if

int a,b,c;
cout<<"Enter the values of a, b and c";
cin>>a>>b>>c;
if( a==b)
if (a==c)
cout<<"\n All numbers are equal";
else
cout<<"\n Numbers are different";
else
cout<<"\n Numbers are different";
Compound condition
• A type of condition in which more than one conditions are checked.
• Implemented using logical operators &&, ||, !
Example:
if(grade==‘A’ || grade==‘a’)
Switch statement
• The switch statement executes one or more of a series of cases, based on the value
of a controlling expression.
Syntax:
switch ( expression )
statement

The Switch statement is typically a compound statement, within which are one or
more case statements executed if the control expression matches the case.
Switch statement (cont..)
The syntax for a case label and expression follows:

case constant-expression :
statement

• The constant expression must have an integral type


• No two case labels can specify the same value 
• There is no limit on the number of case labels in a switch statement
• Only one statement in the compound statement can have the following label
default:
Switch statement (cont..) -Sequence of control
When the switch statement is executed, the following sequence takes place:
• The switch control expression is evaluated (and integral promotions applied) and compared
with the constant expressions in the case labels.
• If the control expression's value matches a case label, control transfers to the statement
following that label. If a break statement is encountered, the switch statement terminates;
otherwise, execution continues into the following case or default statements until
a break statement or the end of the switch statement is encountered
• If the control expression's value does not match any case label, and there is a default label,
control is transferred to the statement following that label. If a break statement does not
end the default statement, and a case label follows, that case statement is executed.
• If the control expression's value does not match any case label and there is no default label,
execution of the switch statement terminates.
Complete Syntax: switch statement
switch(expression) {
case constant-expression :
statement(s);
break;
case constant-expression :
statement(s);
break;
default :
statement(s);
}
Program 1 – selection statement
int day =8;
switch (day) {
  case 1:
    cout << "Monday";
    break;
  case 2:
    cout << "Tuesday";
    break;
  case 3:
    cout << "Wednesday";
    break;
  case 4:
    cout << "Thursday";
    break;
  case 5:
    cout << "Friday";
    break;
  case 6:
    cout << "Saturday";
    break;
  case 7:
    cout << "Sunday";
    break;
Program 2 – Selection statement
int time = 22;
if (time < 10) {
  cout << "Good morning.";
} else if (time < 20) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening.
Program 3 – Selection statement
int day = 4;
switch (day) {
  case 6:
    cout << "Today is Saturday";
    break;
  case 7:
    cout << "Today is Sunday";
    break;
  default:
    cout << "Looking forward to the Weekend";
}
// Outputs "Looking forward to the Weekend"
Class exercise
• Write a C++ program that takes a student grade as an input and
displays that grade. The grade is valid if it is A, B, C, D or F. Incase of an
invalid grade input, the program should display appropriate message.
References
• C++ How to Program
By Deitel & Deitel

• The C++ Programming Language


By Bjarne Stroustrup

• Object oriented programming using C++ by Tasleem Mustafa, Imran Saeed, Tariq Mehmood, Ahsan Raza

• https://www.tutorialspoint.com/cplusplus

• http://ecomputernotes.com/cpp/introduction-to-oop

• http://www.cplusplus.com/doc/tutorial

• https://www.w3schools.com/cpp

You might also like