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,

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 5: Control Structures II (Repetition)
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Do-while loop Syntax do statement while (loop repetition condition)
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
REPETITION CONTROL STRUCTURE
Unit 3 Lesson 9 Repetition Statements (Loops)
EKT120 COMPUTER PROGRAMMING
Decisions Chapter 4.
Control Structures.
Chapter 5: Control Structures II
Chapter 6: Conditional Statements and Loops
Looping.
Chapter 8 Repetition Statements
Arrays, For loop While loop Do while loop
CS1100 Computational Engineering
Loops in C.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Presentation transcript:

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, 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?

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); }

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(); }

The Switch Program Type your program here.

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?

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);

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.

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);

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; }

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.

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.

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.

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.

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);

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

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.

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= )

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.