Control Structures Part 1
Control Structures
Flow of control is the order in which statements are executed Unless something special happens a program is executed sequentially When we want something special to happen we need to use a control structure Control Structures provide two basic functions: selection and repetition
Control Structures
Sequence: { S1, S2 Sk } Conditional: if, if/else, switch Loop: while, for, do.. while
Question
If you want to do something depending a certain condition, what would you need to specify?
Answer
The keyword if The condition The line(s) of code that must be executed when the condition is true Optionally: the line(s) of code that must be executed when the condition is false
if (expression) statement1;
if (expression) { statement1; statement2; Statement3; }
5
If statement
int x = if (x % { cout << } else { cout << } /* something */; 2 == 0) "x is even" << endl;
"x is odd" << endl;
Common Mistake
It is easy to mix up the assignment operator "=" with the equality operator "==". What's wrong with this:
if (grade=100) cout << "your grade is perfect RPI has decided to give you your degree today!\n";
7
Relational Operators
if (truth == true) { /* if equal */ } if (x < 96) { /* if smaller */ }
== != < > <= >= && || is is is is is is and or equal to not equal to smaller bigger smaller of equal bigger or equal
if (x == 1 && y == 2) { /* if both are true */ }
8
Examples
Assume we declared the following variables: int a = 2, b=5, c=10; Here are some examples of boolean conditions we can use: if (a == b) if (a != b) if (a <= b+c) if(a <= b) && (b <= c) if !((a < b) && (b<c))
Relational Operators
Comparing Simple Data Types
assigned to Sample expression Value
Values variables
x = 80 and y = 22 x = 80 and y = 22 x = 80 and y = 22 a = 60 and b = 42 Both operands are
x<y x + y > 100 x + y <= a + b
FALSE TRUE TRUE
4 > 10
FALSE
constants
10
Relational Operators
Comparing Simple Data Types
Valu e
TRUE The ASCII value of is 32, and the ASCII value of a is 97.
Expressio n
< a
Explanation
Because 32 is less than 97, so the expression results in a TRUE
value. R > T FALSE The ASCII value of R is 82 and the ASCII value of T is 84. Because 82 is less than 84, so the expression results in a FALSE value. + < * FALSE The ASCII value of + is 43, and the ASCII value of * is 42. Because 43 is greater than 42, so the expression results in a FALSE value.
11
If statement- Examples
Write a flowchart and a program that input two integer numbers and display teh larger on the screen
12
Answer
int main() { int x, y; cout << "Please enter two integer numbers : "; cin >> x >> y; if (x > y) cout << x << " is the larger number" << endl; else cout << y << " is the larger number" << endl; system("pause"); //this statement is needed when using DEV C++ return 0; }
13
If statement- Examples
Write a program to welcome new customers to your on-line store, and then ask them whether they have a credit card or not.
If yes: inform them that the maximum purchase is RM2000 If no: display an apology saying that the store is unable to process any purchase transactions without a credit
14
Answer
int main() { char YesOrNo; cout << "WELCOME TO MY STORE\n\n "; cout << "Do you have a credit card? (Y/N) : "; cin >> YesOrNo; if (YesOrNo == 'N') { cout << "\n We are sorry, but purchases can only be made using credit card.\n" ; cout << " But you are welcomed to browse in our store.\n " << endl; } else cout << "\n Please note that maximum purchase is RM2000.\n " << endl; system("pause"); //this statement is needed when using DEV C++ return 0; }
15
Nested If statement : Multiple selection
if it is very cold, wear a heavy coat, else, if it is chilly, wear a light jacket, else, if it is windy wear a windbreaker, else, if it is hot, wear no jacket.
16
The Switch Statement
One of several possible actions is taken, depending on the contents of a variable.
17
The Switch Statement
switch ( <variable> or expression ) { case value1: Code to execute if <variable> == value1 break; case value2: Code to execute if <variable> == value2 break; ... case valuen: Code to execute if <variable> == value3 break; default: Code to execute if <variable> not equal any of the value
18
The Switch Statement
Write a program to ask the user to enter a letter grade and then respond with an appropriate message regarding the academic status. If grade is a or A say Good Job If grade is b or B say Pretty good If grade is c or C say Better get to work If grade is d or D say You are in trouble If grade is not A, B, C or D Say You are failing
1. 2.
3.
4. 5.
Notice how complicated will be the program if we use if.. Else!!!!
19
The Switch Statement
switch (grade) { case ('A') :cout << "Good Job!\n" ;
break;
case ('B') :cout << "Pretty good.\n" ; break; case ('C') :cout << "Better get to work.\n" ; break;
case ('D') :cout << "You are in trouble.\n" ;
break; default : cout << "You are failing!!\n" ; }
20
Lab Exercises
Write a program that finds the minimum of three integers Write a program that simulates a simple calculator. It reads two integers and a character. If the character is a +, the sum is printed; if it is a -, the difference is printed; if it is a *, the product is printed; if it is a /, the quotient is printed; and if it is a % , the remainder is printed. Use switch statement
21
Lab Exercises
Write a program that tests whether an input integer is positive or negative Write a program that reads the users age and then prints You are a child if the age <18, You are an adult if age between 18 and 65, and You are a senior citizen if age >= 65
22