Control Structures - Part 1
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 statement
int x = if (x % { cout << } else { cout << } /* something */; 2 == 0) "x is even" << 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
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
Values variables
4 > 10
FALSE
constants
10
Relational Operators
Expressio n
< a
Explanation
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
16
17
18
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.
break;
case ('B') :cout << "Pretty good.\n" ; break; case ('C') :cout << "Better get to work.\n" ; break;
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