2/19/2016it 279, chung-chih li1 branching condition statement list 1 t f statement list 2 condition...
DESCRIPTION
2/19/2016IT 279, Chung-Chih Li3 #include using namespace std; void main() { int a, b, c; cin >> a >> b >> c; if (a > b) { } else { } cout c) coutTRANSCRIPT
05/04/23 IT 279, Chung-Chih Li 1
Branching
Condition Statementlist 1
T
F
Statementlist 2
Condition Statementlist
T
F
05/04/23 IT 279, Chung-Chih Li 2
The Syntax of if and if/else statements
if ( condition ) {
statement list;}
if ( condition ) {
statement list1;}else{
statement list2;}
Indentation indicates that the statements in the statement list are at the level next to the if/else statement.
Reserved words
A Boolean expression (logical expression). In C++, 0 is false, any non-zero values will be considered as true.
truefalse
A reserved word can’t be used as an identifier
05/04/23 IT 279, Chung-Chih Li 3
#include <iostream>using namespace std;
void main(){
int a, b, c;
cin >> a >> b >> c;
if (a > b) {
}else{
}
cout << “ is the biggest”; }
if (a > c) cout << “a:” << a;
elsecout << “c:” << c;
if (b > c) cout << “b:” << b;
elsecout << “c:” << c;
max II
05/04/23 IT 279, Chung-Chih Li 4
Operators
• Arithmetic operators:
+ - / * %• Relational operators:
== > < <= >= !=• Logical operators:
|| && !
05/04/23 IT 279, Chung-Chih Li 5
Relational Operators
cout << (1 < 0) << endl; 0cout << (1 > 0) << endl; 1cout << (1 == 0) << endl; 0cout << (1 <= 0) << endl; 0cout << (1 >= 0) << endl; 1cout << ("1" > "0") << endl; 1cout << ("Yes" == "yes") << endl; 0cout << ("aab" > "aaa") << endl; 1cout << (2 < 3 < 4) << endl; cout << (4 > 3 > 2) << endl; cout << (4 > (3 > 2)) << endl; cout << (0 < 0.5 < 0.6) << endl;
== > < <= >= !=
1
10
0
05/04/23 IT 279, Chung-Chih Li 6
Logical Operators
(1 || 0) ((18 <= x) && (x <= 50))
((18 <= x) || (x <= 50))
!(x < 5) is same as (x >= 5)
(((x % 2) == 0) && ((x % 3) == 0))
|| && !
Assume x = 10
true
true
true
false
false
05/04/23 IT 279, Chung-Chih Li 7
De Morgan’s law
I am not a female student. I am not female or I am not a student.
I will not be in my office or in the lab. I will not be in my office and will not be in the lab.
!(A && B) is same as !A || !B
!(A || B) is same as !A && !B
05/04/23 IT 279, Chung-Chih Li 8
Nested if/else statement
if ( condition 1 ) {
statement list;
}else{ statement list;}
Indentation indicates the level of statements.
if ( condition 2 ) {
statement list;}else{
statement list;};statement list;
05/04/23 IT 279, Chung-Chih Li 9
Example of nested if/else statement
cout << "How many items do you want to buy? "; cin >> a; if (a == 1)
discount = 0.1; else {
if (a == 2) discount = 0.2; else { cout << "At most two items!!"; }
}
05/04/23 IT 279, Chung-Chih Li 10
Enumeration data type
int i,j;enum days {Mon, Tue, Wed, The, Fri, Sat, Sun};
enum days d1, d2=Wed;
....... d1=d2;
if (d1 < Sat) cout << d1 << “It is a week day”;else cout << d1 << “It is a weekend”;
05/04/23 IT 279, Chung-Chih Li 11
Enumeration data type II
enum days {Mon=1, Tue=2, Wed=3, The=4, Fri=5, Sat=6, Sun=7} d1;
.......
if (d1 < Sat) cout << d1 << “It is a week day”;else cout << d1 << “It is a weekend”;
enum days {Mon=5, Tue=4, Wed=3, The=2, Fri=1, Sat=0, Sun=0} d1;
.......
if (d1 != 0) cout << d1 << “It is a week day”;else cout << d1 << “It is a weekend”;
05/04/23 IT 279, Chung-Chih Li 12
Ambiguity in English
I saw the girl with a telescope.
I saw the girl with her boy friend.
05/04/23 IT 279, Chung-Chih Li 13
bool a_member, married;
// You don’t mean this:.....if (a_member)
if (married){ cout << “Input your spouse’s name:”; cin >> sname;}
elsecout << “Sorry, can’t get in!!”;
// A correct wayif (a_member){
if (married){
.....
}}else{
.....}
Ambiguity in C++
05/04/23 IT 279, Chung-Chih Li 14
Confusing nested if/else statement
bool weekend;enum days = {Mon, Tue, Wed, The, Fri, Sat, Sun};enum days d1=Mon, d2=Sun;
.......d1 = Sun;
if (d1 < Sat) if (d1 == Mon) cout << “Have a nice week!!\n”;else cout << “have a nice weekend\n”;
cout << “end\n”;
if (d1 < Sat){ if (d1 == Mon) cout << “Have a nice week!!\n”;}else cout << “have a nice weekend\n”;
cout << “end\n”;
05/04/23 IT 279, Chung-Chih Li 15
Cascaded if/else statements
if (condition_1) if (condition_2) if (condition_3) if (condition_4) if (condition_5) if (condition_6)
statement_1;else
statement_2;
if ( condition_1 && condition_2 &&
condition_3 && condition_4 && condition_5 &&
condition_6) statement_1;
else statement_2;
=
if (condition_1){ if (condition_2)
if (condition_3) if (condition_4)
if (condition_5) if (condition_6)
statement_1; else
statement_2;}
=
05/04/23 IT 279, Chung-Chih Li 16
Other forms of Nested if/else statements (Cascaded) I
if (condition_1) statement_1;
else if (condition_2) statement_2;
else if (condition_3) statement_3;
else if (condition_4) statement_4;
else if (condition_5) statement_5;
else if (condition_6) statement_6;
if (condition_1) statement_1;
if (condition_2) statement_2;
if (condition_3) statement_3;
if (condition_4) statement_4;
if (condition_5) statement_5;
if (condition_6) statement_6;
V.S.
05/04/23 IT 279, Chung-Chih Li 17
Other forms of Nested if/else statements (Cascaded) II
if (condition_1) if (condition_2) if (condition_3) if (condition_4) if (condition_5) if (condition_6)
statement_1;else
statement_2;
if ( condition_1 && condition_2 &&
condition_3 && condition_4 && condition_5 &&
condition_6) statement_1;
else statement_2;
=
05/04/23 IT 279, Chung-Chih Li 18
Switch vs. Cascaded if/else
if (i == 1) statement_1;
else if (i == 2) statement_2;
else if (i == 3) statement_3;
else if (i == 4) statement_4;
else if (i == 5) statement_5;
else if (i == 6) statement_6;
switch (i){
case 1: statement_1; break;
case 2: statement_2; break;
case 3: statement_3; break;
case 4: statement_4; break;
case 5: statement_5; break;
case 6: statement_6; break;}
05/04/23 IT 279, Chung-Chih Li 19
Example of switch
cout << "Input an integer as the day of the week:";cin >> i;
switch (i){ case 1 : cout << "\n Sunday";
break; case 2 : cout << "\n Monday"; break; case 3 : cout << "\n Tuesday"; break; case 4 : cout << "\n Wednesday"; break; case 5 : cout << "\n Thursday"; break; case 7 : cout << "\n Saturday"; break; case 6 : cout << "\n Friday"; break;
}
05/04/23 IT 279, Chung-Chih Li 20
breaks in a Switch statement
if (i == 1) statement_1;
else if (i == 2) {
statement_2; statement_3; }
else if (i == 3) statement_3;
else if (i == 4) statement_4;
switch (i){
case 1: statement_1; break;
case 2: statement_2;case 3: statement_3;
break;case 4: statement_4;
}
05/04/23 IT 279, Chung-Chih Li 21
LoopsBranching
Condition Statementlist
T
F
Condition Statementlist
T
F
05/04/23 IT 279, Chung-Chih Li 22
while
Condition Statementlist
T
F
while (Condition){ Statement list}
05/04/23 IT 279, Chung-Chih Li 23
Example 1: while
string ans = “n”;
while (ans != “Y” && ans != “y”)
{cout << “Would you marry me?”;
cin >> ans;}
cout << “Great!!”;
Should I put ; here?
(ans != “Y” || ans != “y”)
Can I put ; here?
No!!
Up to you!!
05/04/23 IT 279, Chung-Chih Li 24
Example 2: while
int no_times;
cout << “How many times do you want to say?”;cin >> no_times;
while (no_times != 0){
cout << “Hello!” << endl;no_times--;
}
cout << “End!!” << endl;
Will there be any problem?
while (no_times > 0)
What if one inputs –1?
05/04/23 IT 279, Chung-Chih Li 25
Example 3: while
int a,b,sum;
cout << “This program will return the ”;cout << “summation of integers from a to b.\n\n”;cout << “Input two integers a and b:”;cin >> a >> b;
while (a <= b){
sum += a;a++;
}
cout << “The sum is ” << sum << endl;
sum = 0;
sum = sum + a;
Don’t forget to set sum = 0;
05/04/23 IT 279, Chung-Chih Li 26
Example 4: while
int a,b,sum=0;
cout << “This program will return the sum ”;cout << “of odd numbers between a and b.\n\n”;cout << “Input two integers a and b:”;cin >> a >> b;
while (a <= b){ if (a % 2) sum += a; a++;}
cout << “The answer is ” << sum << endl;
if (a % 2 == 0) a++;
while (a <= b){ sum += a; a += 2;}
3, 4, 5, 6 , 7
2, 3, 4, 5, 6 , 7
a b
05/04/23 IT 279, Chung-Chih Li 27
Example 5: while 3N+1 problem
long n,i=0;
cout << “Input an integer:”;cin >> n;
while (n > 1){ if (n % 2) n = 3*n+1; else
n /= 2; cout << ++i << “:” << n << endl;}
cout << “Done!!” << endl;
Will we always get out of a loop?
That is the question!! No one knows!
Input an integer:71:222:113:344:175:526:267:138:409:2010:1011:512:1613:814:415:216:1Done!!Press any key to continue
Input an integer:111:342:173:524:265:136:407:208:109:510:1611:812:413:214:1Done!!Press any key to continue
Input an integer:37591:112782:56393:169184:84595:253786:126897:38068..........83:1684:885:486:287:1Done!!Press any key to continue
05/04/23 IT 279, Chung-Chih Li 28
do-while
Condition
Statementlist
T
F
do {
Statement list} while (Condition);
string ans = “n”;while (ans != “Y”){
cout << “Would you marry me?”; cin >> ans;}
cout << “Great!!”;
; is required
do{
cout << “Would you marry me?”; cin >> ans;} while (ans != “Y”);
cout << “Great!!”;
05/04/23 IT 279, Chung-Chih Li 29
Example 1: do-while
int i;
....
do { cout << “Please input a number between ” << “10 and 20:”; cin >> i;} while (i < 10 || i > 20);
......
05/04/23 IT 279, Chung-Chih Li 30
Primality Test
A prime number is a positive integer that cannot be factorized, i.e., no numbers other that 1 and itself can divide it.
is 893 a prime?Is (893 % 2 == 0) ?Is (893 % 3 == 0) ?Is (893 % 4 == 0) ?Is (893 % 5 == 0) ?Is (893 % 6 == 0) ?
.
.
. Is (893 % 892 == 0) ?
We can write a loopto test these.
05/04/23 IT 279, Chung-Chih Li 31
Example: Silly Primality Test
long int p,r,i=2;
cout << “Input an positive integer:”;cin >> p;
cout << p << “ is “;
do{
r = p % i;i++;
} while (i < p && r != 0);
if (i == p) cout << “a prime number.”;else{ cout << “not a prime number.”; cout << “The least factor is ” << --i;}
05/04/23 IT 279, Chung-Chih Li 32
Break in a loop
do{
r = p % i;if (r == 0) break;i++;
} while (i < p);
The break statement in a loop will force theprogram to jump out of the loop immediately.
do {cout << “Would you marry me?”;cin >> ans;
cout << “Really?”cin >> ans;
} while (ans != “Y” && ans != “y”);cout << “Great!!”;
if (ans == “F” || and == “f”) break;
05/04/23 IT 279, Chung-Chih Li 33
Continue in a loop
The continue statement in a loop will force the program to check the loop condition immediately.
do {cout << “Would you marry me?”;cin >> ans;
if (and != “Y” && ans != “y”) continue;
cout << “Great?”
break;
} while (true);
....
05/04/23 IT 279, Chung-Chih Li 34
Euclid Algorithm#include <iostream>using namespace std;
void main(){int a,b;
cout << "Input two positive integers:";cin >> a >> b;
int r = a % b;
while (r){ a = b; b = r;
r = a % b;}cout << "The GCD is " << b << ".\n";
}
05/04/23 IT 279, Chung-Chih Li 35
Primality Test with while loop
#include <iostream>using namespace std;void main(){ bool is_prime=true;
int d=2,p;cout << "Input a positive integers:";cin >> p;
while (d <= p/2) {
if (p % d == 0) {
is_prime=false; break;
} d++;}if (is_prime) ...
}
Can we change to while (d < p/2) ?
05/04/23 IT 279, Chung-Chih Li 36
Primality Test with do-while loop
#include <iostream>using namespace std;void main(){ bool is_prime=true;
int d=2,p;cout << "Input a positive integers:";cin >> p;
do { if (p % d == 0)
{ is_prime=false;
break; } d++;} while (d <= p/2);if (is_prime) ...
}
Can we change to while (d < p/2) ?
05/04/23 IT 279, Chung-Chih Li 37
Primality Test with do-while loop (a bit better)
void main(){ bool is_prime=true;
int d=3,p;cout << "Input a positive integers:";cin >> p;
do { if ((p % d == 0) || (p % 2 == 0))
{ is_prime=false;
break; } d += 2;} while (d < p/2);
if (is_prime) ...}
05/04/23 IT 279, Chung-Chih Li 38
Primality Test with do-while loop (yet another improvement)
void main(){ bool is_prime=true;
int d=3,p;cout << "Input a positive integers:";cin >> p;
if (p % 2 == 0} is_prime=false;
else do { if (p % d == 0)
{ is_prime=false;
break; } d += 2;} while (d < p/2);if (is_prime) ...
}
What if I forget else here?
05/04/23 IT 279, Chung-Chih Li 39
Definite Loop
• In programming a definite loop is more welcome.I.e., number of iterations isis known before the loop begins, at least the upper bound is known.
I.e., repeat the loop 100 times.
Precisely speaking, there is no definite loop in C++
05/04/23 IT 279, Chung-Chih Li 40
The general format for a for loop
for (Initialization_action; Condition; Condition_update){ statement_list;}
int n,f=1;
cin >> n;
for (i=2; i<=n; i++){ f *= i;} cout << “The factorial of ” << n << “ is ” << f << “.”;
1 2 3
Factorial of n is n(n-1)(n-2)...21
05/04/23 IT 279, Chung-Chih Li 41
Compare: for and while
for (Initialization_action; Condition; Condition_update){ statement_list;}
int n,f=1;
cin >> n;
for (i=2; i<=n; i++){ f *= i;}
cout << “The factorial of ” << n << “ is ” << f << “.”;
i=2;while (i<=n){ f *= i; i++;}
1 2 3for (Initialization_action; Condition; Condition_update){ statement_list;}
05/04/23 IT 279, Chung-Chih Li 42
For Loop is not really a definite loop
int n,i;
n = 100;
for (i=1; i <= n; i++){ statement_list;}
int n,i;
n = 100;i = 1;
while (i <= n){ statement_list; i++;}
v.s.
05/04/23 IT 279, Chung-Chih Li 43
break and continue
The break statement in a for/while loop will force theprogram to jump out of the for/while loop immediately.
The continue statement in a for/while loop will force the program to update the loop condition and then check the condition immediately.
for (Initialization_action; Condition; Condition_update){ statement_list;}
05/04/23 IT 279, Chung-Chih Li 44
Nested loops (loop in loop)
cin >> a >> b;
for (int i = 0; i < a; i++){
for (int j=0; j<b; j++){ cout << “*”;}cout << endl;
}
****************************************************
b
a
05/04/23 IT 279, Chung-Chih Li 45
Nested loops (2)
int a,b;
cin >> a >> b;
for (int i = 0; i < a; i++){
for (int j=0; j<b; j++){ if (j > i)
break; cout << “*”;}cout << endl;
}
**********
b
a
05/04/23 IT 279, Chung-Chih Li 46
Nested loops (3) **********
b
a
int a,b;
cin >> a >> b;
for (int i = 0; i < a; i++){ for (int j=0; j<b && j < i; j++)
{ cout << “*”;}cout << endl;
}
j <= i;
if (j > i) break;
05/04/23 IT 279, Chung-Chih Li 47
Nested loops (4)
int a,b;
cin >> a >> b;
for (int i = 0; i < a; i++){
for (int j=0; j<b; j++){ if (j < i)
cout << “ ”; else
cout << “*”;}cout << endl;
}
**********************************************
b
a
=
05/04/23 IT 279, Chung-Chih Li 48
Nested loops (5)
int a,i,j;cin >> a;
for (i = 0; i < a; i++) { for (j=0; j<a; j++) {
if (j < a-i) cout << " ";
else cout << "*"; }
for (j=0; j<a; j++) {if (j > i) break;cout << "*";
}
cout << endl;}
************************************