C++ Session 3
C++ Session 3
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)
If( (6!=6 & 7<=8) ||4>=4)
cout<<“Onaapo”;
else
cout<< “Obanapo”;
Flow control can be broken down into two
• Conditional Statements
• Loops
#include <iostream>
using namespace std;
int main()
{ char man[34];
cout << "enter man" << endl;
cin.get(man,34);
cout<<"you entered "<<man;
return 0; }
Correction – Two approaches
1. Define the variable as char array and use
puts() – for message and gets() for variable
• #include <iostream> enter name:
• using namespace std; Jame Aglawal Solomons
• int main() you entered
Jame Aglawal Solomons
• { char name[12];
• puts( "enter name: " ); //display
• gets(name); //collect
• puts("you entered ");//display
• puts(name); //display
• return 0; }
Correction – Two approaches
1. Define the variable as string and use
getline(cin, name) function for c++
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 need to
cout<<"You are not yet born";
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;
int main ()
{ cout << "value of T is "
// Local variable declaration: << T << endl;
int A,R,T;
cout<<"enter the value of A: return 0;
";
}
cin>> A;
cout<<"enter the value of R:
";
cin>>R;
#include <iostream>
using namespace std;
int main ()
{ // Local variable declaration:
int a,b = 6,x = 9;
a = (b>x)? 20: 40;
cout << "value of a: " << a << endl;
return 0;
}
?
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:
cout<<"Your grade is below 50";
switch (grade) cout<<endl<<endl;
{ }
case 'A': return 0;
cout<<"Your grade is between }
80 - 100";
break;
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':
char grade; case 'c':
cout<<"Enter your grade: "; cout<<"Your grade is between 60 - 69\n";
cin>>grade; break;
case 'D':
switch (grade) case 'd':
{ cout<<"Your grade is between 50 - 59\n";
case 'a': break;
case 'A': default:
cout<<"Your grade is between 80 - 100\n"; cout<<"Your grade is below 50\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:
int main() case 91:
{ case 90:
int testScore;
cout<<"Enter your test score: "; cout<<"Your grade is A\n";
cin>>testScore; break;
case 89:
switch (testScore) case 88:
{ cout<<"Your grade is B\n";
case 100: break;
case 99:
case 98: default:
case 97: cout<<"Your grade is blow B\n";
case 96:
case 95: }
return 0;
}
#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
through"<<endl; Try it out
// case 'n'&& 'p':
default:
cout<<"Catch up"; }
return 0;
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