Professional Documents
Culture Documents
Flow Control
Operators and Meaning
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
== Equality
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
% Modulo Operator (remainder)
Flow control can be broken down into two
• Conditional Statements
• Loops
int main()
{
int num;
cout<<"Enter a whole number: ";
cin>> num;
if (num % 2 == 0)
cout<<"The number you entered is even,";
else
cout<<"The number you entered is odd";
return 0;
}
Example 3
#include <iostream> if ((age>5) && (age <=10))
using namespace std; cout<<"You are not yet a teen";
int main()
if ((age >10)&& (age <= 15))
{
cout <<"You are not matured";
int age;
cout<<"Enter a whole number: "; //else affect only the if above
cin>> age ; else
if (age <= 0) cout<<endl<<endl;
cout<<"You are not yet born"; cout<<"You need to
support Chelsea no matter
if((age>0 )&&(age<=5 ))
what is happening";
cout << "you are a todler"; return 0;
}
To modify the code such that the else affect all the if above add else to all the if’s except the first
# include<iostream> else if ((age>5)&(age<=10))
using namespace std; cout<<"you are not yet a teen ";
int main ()
{
// Local variable declaration:
int x, y = 10;
x = (y <= 10) ? 30 : 40;
return 0;
#include <iostream>
using namespace std; T = (A<=R) ? 12 : 11;
?
What will be displayed and why
#include <iostream>
using namespace std;
int main ()
{ int x, y = 4, z = 2;
x = (y>z) ? y:z;
cout<< "The value of x is "<<x;
return 0;
}
?
What will be displayed and why
Many cin can be put together
#include <iostream>
int main()
{
using namespace std;
cout<<"enter two numbers: ";
int a,b;
cin>>a>>b;
cout<<"The larger of the two is "<<((a>b)?a:b)<<endl;
}
The switch statement
The switch statement
• The switch statement is similar to the if/ else,
statement.
• It evaluates the value of an integer and
compares with two or more values and
determine which code to execute.
• The following program determines your
average based on your grade.
The switch statement
• The switch statement is similar to the if/ else,
statement.
• It evaluates the value of an integer and
compares with two or more values and
determine which code to execute.
• The following program determines your
average based on your grade.
#include <iostream> case 'B':
cout<<"Your grade is between 70 - 79";
using namespace std; case 'C':
cout<<"Your grade is between 60 -
69"<<endl;
int main()
{
break;
char grade; case 'D':
cout<<"Enter your grade: "; cout<<"Your grade is between 50 - 59";
cin>>grade; break;
default:
switch (grade) cout<<"Your grade is below 50";
{ cout<<endl<<endl;
case 'A': }
cout<<"Your grade is between return 0;
80 - 100"; }
Interpretation
• The switch key word evaluates an integer
expression grade while grade is a character
variable.
• Every character has a corresponding integer
value in the American Standard Code for
Information Interchange (ASCII) between 1 to
127.
• Note that ASCII value for upper cases are
different from those of lower cases
Interpretation Cont.
• The default key word serves the same purpose
as an final else in an if / else statement.
• The integer following the switch key word is
evaluated and compared with the integer
constant following each case key word.
• If there is a match ie the two integers are
equal.
• The statement belonging to that case is
executed . Otherwise they are not
• Thus the statement belonging to a case are
conditional just as those of if / else
Differences between if /else and switch
• With an if / else statement, comparison
following the if part may be different from
comparison following the else part. Example
• If (apple == orange)
• Do this;
• else if (sales >= 500)
• Do that;
• In a switch statement, the constant integer
following a case must be compared with value
following the switch key word and nothing else.
Differences between if /else and switch
• With an if / else statement, comparison
following the if part may be different from
comparison following the else part. Example
• If (apple == orange)
• Do this;
• else if (sales >= 500)
• Do that;
• In a switch statement, the constant integer
following a case must be compared with value
following the switch key word and nothing else.
Possible
#include <iostream> case 'B':
case 'b':
using namespace std; cout<<"Your grade is between 70 - 79\n";
break;
int main()
case 'C':
{
case 'c':
char grade; cout<<"Your grade is between 60 - 69\n";
cout<<"Enter your grade: "; break;
cin>>grade;
case 'D':
case 'd':
switch (grade) cout<<"Your grade is between 50 - 59\n";
{ break;
case 'a': default:
cout<<"Your grade is below 50\n";
case 'A':
cout<<"Your grade is between 80 - 100\n";
}
break;
return 0;
}
Range of numbers
If (testScore >= 90)
Cout<< “Your grade is A”;
else if (testScore >=88)
cout<<“Your grade is B”;
else
cout<<"Your grade is blow
B\n";
#include <iostream> case 94:
using namespace std; case 93:
case 92:
case 91:
int main()
case 90:
{
int testScore; cout<<"Your grade is A\n";
cout<<"Enter your test score: "; break;
cin>>testScore; case 89:
case 88:
switch (testScore) cout<<"Your grade is B\n";
{ break;
case 100:
default:
case 99:
cout<<"Your grade is blow B\n";
case 98:
case 97: }
case 96: return 0;
case 95: }
#include <iostream> Double Arguments
using namespace std; produce Semantic
int main()
errors
{ char echo;
char bank;
cout<<"enter two variables: "; Irrelevant output
cin>>bank>>echo; would be generated
switch (echo || bank) { for both or (||) and
case '8' ||'g': (&&)
cout<<"The wish is Try it out
through"<<endl;
// case 'n'&& 'p':
default:
cout<<"Catch up"; }
Lab Exercise 2
a) Suppose savings and expenditure are
variables of type double. Write an if – else
statement that request two inputs from the user
and outputs the word “Solvent”, decreases the
value of savings by the value of expenditure, and
sets the value of expenditure to zero, provided
savings is at least greater than the expenditure.
If, however, savings is less than expenditure, the
program should output the word “Bankrupt”.
Lab Exercise 2 Cont.
Students of Amasaman Polytechnic will have to
travel to the school campus to check their results.
The authorities decided to make it possible for
them to check their results online.
On a trial basis, they asked that two persons with
the following credentials be used to test the
system.
Student’s name : Paulina Sosu
Index number PS1024
Lab Exercise 2 Cont.
Student’s name: Emmanuel Tetteh
Index number: ET1025
Write a c++ console application that would be embedded in a web application such that if a
username, PSosu and password PS1024 is inputted, the following should be displayed