0% found this document useful (0 votes)
12 views27 pages

CSC201 - Week8

This document covers selection statements in C++, including the if statement, if...else statement, and switch statement. It explains how these statements allow for conditional execution based on relational operators and provides examples of their usage. The document also discusses compound conditions, short-circuiting, boolean expressions, and nested selection statements.

Uploaded by

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

CSC201 - Week8

This document covers selection statements in C++, including the if statement, if...else statement, and switch statement. It explains how these statements allow for conditional execution based on relational operators and provides examples of their usage. The document also discusses compound conditions, short-circuiting, boolean expressions, and nested selection statements.

Uploaded by

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

Week 8 – SELECTION

Course Lecturer:
Haruna Musa
([email protected])

Department of Computer Science,


FUD - 2015/2016
Contents

 The if statement
 The if..else statement
 The Switch Statement
SELECTION
 Sometimes you won’t want every statement
in your C++ program to execute every time
the program runs.

 every program in the previous section has


executed from the top and has continued,
line-by-line, until the last statement
completes.

 you might not always want this to happen


 Programs that don’t always execute by rote
are known as data driven programs.

 the data dictates what the program does.

 This is possible through the use of


relational operators that conditionally
control other statements.
The if Statement
 You incorporate relational operators in C++
programs with the if statement.

 Such an expression is called a decision


statement because it tests a relationship—
using the relational operators

 It makes a decision about which statement to


execute next based on the tests results
 The if statement allows conditional
execution.

 your program flows along line by line in the


order in which it appears in your source
code

 The if statement enables you to test for a


condition and branch to different parts of
your code, depending on the result
The simplest form of an if statement

if (condition){
statements;
}
 The condition includes any relational
comparison, and it must be enclosed in
parentheses.
 The block of one or more C++ statements
is any C++ statement, such as an
assignment or cout, enclosed in braces.
 The block of the if, sometimes called the body
of the if statement, is usually indented a few
spaces for readability.
 Consider the following example:

if (bigNumber > smallNumber){


bigNumber = smallNumber;}

 This code compares bigNumber and


smallNumber. If bigNumber is larger, the
second line sets its value to the value of
smallNumber.
 The following type of branch can be
quite large and powerful:

if (expression){
statement1;
statement2;
statement3;
}
 Here's a simple example of this usage:

if (bigNumber > smallNumber){


bigNumber = smallNumber;
cout << "bigNumber: " << bigNumber << "\n";
cout << "smallNumber: " << smallNumber << "\n";}

 This time, if bigNumber is larger than


smallNumber, not only is it set to the
value of smallNumber, but an
informational message is printed
 Example: this program tests if one positive integer
is not divisible by another.
#include<iostream>
int main()
{int n;
cout<<"enter a positive integers:";
cin>>n;
if (n%2 !=0)
cout<<"is not an even number"<<endl;
}
 In C++, whenever an integral expression is
used as a condition, the value 0 means
“false” and all other values means “true”.

 The program in example above is


inadequate statement because it provides no
affirmative information when n is even
number. that fault can be remedied with an
if …. else statement.
The if … else Statement
 The if…else statement causes one of two
alternative statements to execute depending
upon whether the condition is true. Its
syntax is:

If (condition)
Statement;
else
Statement;
 Where condition is an integral expression and statement1
and statement2 are executed statements.
 Example: this program is the same as the program above
except that, the if statement has been replaced by an if…else
statement.
#include<iostream>
int main()
{int n;
cout<<"enter a positive integers:";
cin>>n;
if (n%2 ==0)
cout<<"is even number"<<endl;
else
cout<<"is not an even number"<<endl;}
Statement Blocks
 A statement block is a sequence of statements
enclosed by braces { } like this:
{ int temp = x; x = y; y = temp; }
Example: this program inputs two integers and then outputs them in increasing
order;
#include<iostream >
Int main()
{ int x, y;
Cout<< “enter the two integers:”;
Cin>>x>>y;
If (x>y)
{int temp = x; x =y; y =temp;} //swap x and y:
Cout<<x<< “<=”<<y<<endl;
}
 Note that a C++ program itself is a statement
block preceded by int main().
 Recall that the scope of a variable is that part
of program where the variable be used.
 It extends from the point where the variable is
declared to the end of the block which that
declaration controls.
 So a block can be used to limit the scope of a
variable, thereby allowing the same name to
be used for different variables in different
parts of the program.
#include<iosteam>
Int main()
{
Int n = 44;
Cout<< “n =”<<n<<endl;
{int n;
Cout<< “enter an integer:”;
Cin>>n;
Cout<< “n =”<<n<<endl;
}
{cout<< “n=”<<n<<endl;
}
{int n;
Cout<< “n=”<<n<<endl;
}
Cout<< “n=”<<n<<endl;
}
Compound Conditions
 Conditions such as n%d and x>=y can be combined
to form compound conditions. This is done using
the logical operators && (and), // (or), and ! (not).
They are define by

 P && q evaluates to true if and only if both


p and q evaluates to true
 P // q evaluates to false if and only if one or
both evaluates to true.
 !p evaluates to true if and only if p
evaluates to false
Example: this example uses the compound conditions to
find the minimum of the three integers.
#include<iostream>
Int main()
{int n1, n2, n3;
Cout<< “enter the three integers:”;
Cin>>n1>>n2>>n3;
If (n1<=n2 && n1<=n3) cout<<the minimum
is:”<<n1<<endl;
If(n2<=n1 && n2<=n3) cout<< “the minimum
is:”<<n2<<endl;
If(n3<=n1 && n3<=n2) cout<< “the minimum
is:”<<n3<<endl;
}
 Example: this program allows the user to input
either a “Y”, or “y”, for “yes”.
 #include<iostream>
 Int main()
 { char ans;
 Cout<< “are you enrolled(y/n)?:”;
 Cin>>ans;
 If(ans == ‘Y’ // ans== ‘y’)
 cout<< “you are enrolled.\n”
 Else
 cout<< “you are not enrolled.\n”;
 }
Short - Circuiting
 Compound conditions that use && and // will not
even evaluate the second operand of the condition
unless necessary, this is called short – circuiting.
Example: this program tests integer divisibility.
#include<iostream>
int main()
{ int n, d;
Cout<< “enter the two positive integers:”;
Cin>>n>>d;
If(d !=0 && n%d ==0) cout<<d<< “divides”<<n<<endl;
Else cout<<d<< “does not divides”<<n<<endl;
}
Boolean Expressions
 A Boolean expression is a condition that is either
true or false.
 In the previous example, the expression d>0, n
%d==0, and (d>0 && n%d ==0) are Boolean
expressions.
Nested Selection Statements
 Like compound statements, selection
statements can be used whenever any other
statement can be used within another selection
statement.

 This is called nesting statements.


Example: take a look at this program below.
#include<iostream>
Int main()
{int n, d;
Cout<< “enter two positive integers:”;
Cin>>n>>d;
If(d !=0)
If(n%d==0) cout<<d<< “divides”<<n<<endl;
Else cout<<d<< “does not divide”<<n<<endl;
}
The else…if construct.
 if…else statements are often used to test a
sequence of parallel alternatives, where only the
else clauses contain further nesting.
Example: this program request the users language and then
prints a greeting in that language.
#include<iostream>
Int main()
{char language;
Cout<< “engl., fren, ger, ital, or rush? (e/f/g/i/r):”;
Cin>>language;
If (language== ‘e’) cout<< “welcome to england:”;
Elseif (language == ‘f’) cout<< ‘welcome to france:”;
Elseif(language == ‘g’) cout<< “welcome to germany:”;
Elseif(language == ‘I’) cout<< “welcome to itely:”;
Elseif (language == ‘r’) cout<< “welcome to Russia:”;
Else cout<< “sorry! Specify the language:”;
}
The Switch Statement
 The switch statement can be used instead of the else…
if the construct to implement a sequence of parallel
alternatives.
Its syntax is:
Switch (expression)
{ case constant1: statement1;
case constant2: statement2;
case constant3: statement3;


case constantN: statement;
default: statementlist 0;
}

You might also like