Presentation is loading. Please wait.

Presentation is loading. Please wait.

February 7, 2008. You have seen how If statement works from last week’s activities. You have learned that If statement is a conditional statement. Today,

Similar presentations


Presentation on theme: "February 7, 2008. You have seen how If statement works from last week’s activities. You have learned that If statement is a conditional statement. Today,"— Presentation transcript:

1 February 7, 2008

2 You have seen how If statement works from last week’s activities. You have learned that If statement is a conditional statement. Today, you will learn that If statement is not alone in Turbo C. There is Switch statement capable also of evaluating expression and executing statements based on a given expression. What is Switch statement? What is its syntax?

3 The Switch statement is the most flexible program control statement in C language. It can execute different statements based on an expression that can have more than two values. It is true that nested If statements are also used to control program flow based on more than two values. But with switch statement, nesting is unnecessary. The general form of the switch statement is: switch(variable) { case value_1: statement(s); case value_2: statement(s); …. case value_n :statement(s); default:statement(s); }

4 Directions: Use the old groupings. Convert the If statement in the program into Switch statement. Check the syntax of Switch statement in the previous slide. Type your program in the next slide. { int num; printf(“please enter a number from 1 to 5:”); scanf(“%d”, &num); if num==1 printf(“\n You entered 1.”); if num==2 printf(“\n You entered 2.”); if num==3 printf(“\n You entered 3.”); if num==4 printf(“\n You entered 4.”); if num==5 printf(“\n You entered 5.”); else printf(“\n Out of range.”); getch(); }

5 The Switch Program Type your program here.

6 After converting If statement into Switch statement, please answer the question below? Q : What is your opinion about the structure of nested If statement in the program? Is it convenient to use or is it confusing? Why?

7 The Switch Activity 1. Make a subfolder named SWITCH in the 3rd Term folder. 2. Open Turbo C and type the program in the code window. #include void main() { clrscr(); int num; printf(“The Switch Program of Nina Sayoc JB\n\n”); printf(“Pls enter an integer from 1 to 3 :”); scanf(“%d”,&num);

8 switch(num) { case 1:printf(“You entered 1.”); break; case 2:printf(“You entered 2.”); break; case 3:printf(“You entered 3.”); break; default:printf(“Out of range.Please try again.”); } getch(); } 3. Save the program as switch.cpp in the Switch subfolder. 4. Make an EXE file by pressing F9. 5. Update your index. Note: The Break statement is used to terminate a Case in the If statement.

9 The Switch Menu Activity 1. Make a subfolder named SWITCH Menu in the 3rd Term folder. 2. Open Turbo C and type the program in the code window. #include void main() { clrscr(); int num1,num2,menu; float ans; printf(“The Menu Program of Cookie Padua JB \n\n”); printf(“Pls enter the first number:”); scanf(“%d”,&num1); printf(“\n Pls enter the second number:”); scanf(“%d”,&num2);

10 printf(“\nPlease enter the operation.”); printf(“\n Use 1 for addition,2 for subtraction, 3 for multiplication, and 4 for division.”); scanf(“%d”,&menu); switch(menu) { case 1: { ans=num1+num2; printf(“\n The sum is %f.”, ans); break; } case 2: { ans=num1-num2; printf(“\n The difference is %f.”, ans); break; } case 3: { ans=num1*num2; printf(“\n The product is %f.”, ans); break; }

11 case 4: { ans=num1/num2; printf(“\n The quotient is %.2f”,ans); break; } default: printf(“Out of range.Please try again.”); } /* end of Switch statement */ getch(); } 3. Save the program as switchmenu.cpp in the Switch Menu subfolder. 4. Make an EXE file by pressing F9. 5. Update your index.

12 Comprehension questions Directions: Please answer the questions below as a group. Type the answers on the space provided after each question. 1. What are the variables in the program? 2. What is/are the data type/s of the variable/s? 3. What will happen if the user enters the number 3? Be ready to share your answers to the class.

13 The Level Activity 1. Make a subfolder named Level in the 3rd Term folder. 2. Open Turbo C and type the program of the output below: Sample Output: Pls enter your name: Patrick Pls enter your level (1,2,3 or 4): 3 Hi Patrick! You are a Junior! 3. Save the program as level.cpp in the Level subfolder. 4. Make an EXE file. Press F9.

14 5. Once the program is finished, get 1/8 sheet cross wise. Write your name, section, date, and title of the activity which is The Level Activity. Evaluate the output using the rating scale below: 10/10 – The output is correct. All requirements are present. 6/10 – The program has a minor error. 4/10 – The program has many errors. No output was seen in the run mode.

15 The do..while loop executes a block of statements as long as the specified condition is True. It tests the condition at the end of the loop which gives one output if the condition returns False. Syntax of the do..while loop: do { Statement sequence; } while(condition);

16 The Do While Loop Activity 1. Make a subfolder named dowhile in the 3rd Term folder. 2. Open Turbo C and type the program in the code window. #include void main() { clrscr(); int sum, counter; sum=0; counter=0; do { sum+=1; ++counter; } while(counter<=10); Sum+=1 is the same as sum=sum+1 ++counter is the same as counter=counter+1 Initialize the value of sum and counter Increase/decrease the value of the counter The loop shall stop when COUNTER reaches 11

17 printf(“The sum is %d.”,sum); getch(); } The loop shall be executed 10 times. 3. Save the program as dowhile.cpp in the dowhile subfolder. 4. Make an EXE file. Output: The sum is 11.

18 1.Study for Seatwork#3 next meeting. Scope: Switch Statement and Do While Loop 2. Make a C program that shall ask the user a number. The program must compute and display the summation of the number entered. Use a loop. Write the program in ½ cross wise of paper. Example: Pls enter a number: 5 The summation of 5 is 15. (Summation of 5= 5+4+3+2+1)

19 Q1 : Which is more convenient to use between nested If statement and Switch statement? Why? Q2 : As a young Filipino programmer, what are your criteria in choosing your answer in Question1? Be ready to share your answers to the class. Use ¼ sheet of paper for the summary. Submit the paper to the teacher.


Download ppt "February 7, 2008. You have seen how If statement works from last week’s activities. You have learned that If statement is a conditional statement. Today,"

Similar presentations


Ads by Google