Computer Programming 04
Computer Programming 04
PROGRAMMING
Lecture 04
Switch Multiple Selection Structure
While Repetition Structure
switch ( variable ) {
case value1: // taken if variable == value1
statements
break; // necessary to exit switch
case value2:
case value3: // taken if variable == value2 or == value3
statements
break;
You passed
Your grade is D
char letter;
cout << "The character (" << 'a' << ") has the value "
<< ( int ) ( 'a' ) << endl;
Prints
The character (a) has the value 97
no
condition
yes
Statements in
Loop body
If true, the body of the loop is executed and the control is transferred back
to the condition for re-evaluation
During execution, some item from the boolean expression
is changed
If false, the while loop is exited and control is transferred to the statement
after the while loop body.
Condition there are more items on my shopping list is either true or false.
If true, the action “Purchase next item and cross it off my list” is performed.
The action will be performed repeatedly while the condition remains true.
The condition will become false when the last item on the list is purchased and
crossed of the list. The repetition will terminate.
Then the first statement after the repetition structure is executed.
Example statement
Consider a program segment designed to find the first power of 2 larger
than 1000.
Suppose the integer variable product has been initialized to 2.
When wile repetition structure finishes executing.
Product will contain the desired answer.
true
product <= 1000 product = 2 * product
false
C++ Code
int product = 2;
while ( product <= 1000 )
product = 2 * product;
4. #include <iostream.h>
5. int main()
6. {
7. int count_down; //declaring count_down as an integer variable
8. cout<<"How many greetings do u want\n"; //prompt user to enter the number for greetings
9. cin>>count_down; //reading value this value gives number of times greeting will appears
10. while(count_down > 0) //while loop condition, if countdown is greater than zero
11. {
12. cout<<"HELLO!\t"; //printing hello
13. count_down = count_down - 1; //decrementing the value in count_down (counter) by 1
14. } //while body ends here
15. cout<<endl;
16. cout<<"thats all \n"<<"have a great day";
17. return 0;
18. }
Input = 5
Input = 7
Printing hello 7 times
3. int main()
4. {
5. int count; //declaration of control variable count
6. count=1; //initialization of control variable
7. while (count<=100) //while count is less than 100 program will print the count
8. {
9. cout<<count<<“\t”;
10. count=count+1 //add one to the count every time the loop repeats.
11. } /*when count is greater than 100 then the loop will
12. terminate and the statement next to the while loop will execute*/
13. Return 0; //indicated successful termination
14. } //end of function main
Sentinel value
Also called dummy value, signal value or a flag value.
Indicates “end of data entry”
Loop ends with sentinel input
Sentinel value should be chosen so that it cannot be confused with an
acceptable data value.
For example (-1) in case of grade example.
As -1 is not going to be any grade (grade is only + value) so
this can be used to quit the loop.
Include <iostream>