Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Advertisements

Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 – C Program Control
Control Structures Sequential execution Transfer of control
REPETITION CONTROL STRUCTURE
while Repetition Structure
Chapter 4 C Program Control Part II
C++ Programming: CS150 For.
Controlling execution - iteration
Control Statements: Part 2
Chapter 4 - Program Control
Control Structures II (Repetition)
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Control Statements Kingdom of Saudi Arabia
Programming Fundamentals
JavaScript: Control Statements I
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 4 - Program Control
3 Control Statements:.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Program Control Topics While loop For loop Switch statement
Chapter 6 Control Statements: Part 2
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Let’s all Repeat Together
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
2.6 The if/else Selection Structure
Chapter 5: Control Structures II (Repetition)
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Lecture 9: Implementing Complex Logic
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Control Statements:.
Presentation transcript:

Introduction to Computer Programming CS101: Lecture 8

Formulating Algorithms: Sentinel-Controlled Repetition (cont.) The second refinement of the preceding pseudocode statement is then Prompt the user to enter the first grade Input the first grade (possibly the sentinel) While the user has not yet entered the sentinel Add this grade into the running total Add one to the grade counter Prompt the user to enter the next grade Input the next grade (possibly the sentinel) ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

©1992-2014 by Pearson Education, Inc. All Rights Reserved.

// class average problem with sentinel-controlled repetition #include <iostream> using namespace std; int main() { int counter; double average ,total ,grade; total = 0; counter = 0; grade=0; average = 0; cout << " Enter grade or -1 to quit : " ; cin >> grade; // input grade or sentinel value

while (grade != -1) // while grade is not -1 { total = total + grade; ++counter; cout << "Enter grade or -1 to quit: "; cin >> grade; // input grade or sentinel value } average = total / counter; cout << "\nTotal of all " << counter << " grades is: " << total << endl; cout << "Class average is :" << average << endl; return 0; } // end main

Output: Enter grade or -1 to quit : 98 Enter grade or -1 to quit: 75 Total of all 3 grades is: 237.5 Class average is :79.1667

©1992-2014 by Pearson Education, Inc. All Rights Reserved.

©1992-2014 by Pearson Education, Inc. All Rights Reserved.

Formulating Algorithms: Sentinel-Controlled Repetition (cont.) The pseudocode statement Calculate and print the total of all student grades and the class average can be refined as follows: If the counter is not equal to zero Set the average to the total divided by the counter Print the total of the grades for all students in the class Print the class average Else Print “No grades were entered” Test for the possibility of division by zero Normally a fatal logic error that, if undetected, would cause the program to fail (often called “crashing”). ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

#include <iostream> using namespace std; int main() { int counter; double average ,total ,grade; total = 0; counter = 0; grade=0; average = 0; cout << " Enter grade or -1 to quit : " ; cin >> grade; // input grade or sentinel value

while (grade != -1) // while grade is not -1 { total = total + grade; ++counter; cout << "Enter grade or -1 to quit: "; cin >> grade; // input grade or sentinel value } if (counter != 0) //check the denominator (counter)not equal zero average = total / counter; cout << "\nTotal of all " << counter << "grades is: " << total << endl; cout << "Class average is :" << average << endl; else cout << "No grades were entered !!" << endl; return 0;

break and continue Statements break Statement The break statement, when executed in a while, for, do…while, or switch statement, causes immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch statement. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

1 2 3 4 Broke out of loop at count = 5 #include <iostream> using namespace std; int main() { int counter; for (counter = 1; counter <= 10; ++counter) // loop 10 times if (counter == 5) break; // break loop only if counter is 5 cout << counter << " "; } // end for cout << "\nBroke out of loop at count = " << counter << endl; return 0; } // end main

break and continue Statements (cont.) The continue statement, when executed in a while, for or do…while statement, skips the remaining statements in the body of that statement and proceeds with the next iteration of the loop. In while and do…while statements, the loop-continuation test evaluates immediately after the continue statement executes. In the for statement, the increment expression executes, then the loop-continuation test evaluates. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

Used continue to skip printing 5 #include <iostream> using namespace std; int main() { int counter; for (counter = 1; counter <= 10; ++counter) if (counter == 5) continue; cout << counter << " "; } // end for cout << "\nUsed continue to skip printing 5" << endl; return 0; } // end main 1 2 3 4 6 7 8 9 10 Used continue to skip printing 5

Program that reverses the digits in an integer variable number #include <iostream> #include <iomanip> using namespace std; int main() { int number, rem; cin>>number; while (number > 0) rem = number % 10; cout << rem; number = number / 10; } return 0; } // end main

C++ code to calculate x raised to the y power C++ code to calculate x raised to the y power. The code should have a while repetition statement. int x, y; int i = 1, power = 1; cout << "Enter base as an integer: "; cin >> x;// input base cout << "Enter exponent as an integer: "; cin >> y; // input exponent while (i <=y) { power *= x; i++; } cout << power << endl;

©1992-2014 by Pearson Education, Inc. All Rights Reserved. Logical Operators C++ provides logical operators that are used to form more complex conditions by combining simple conditions. The logical operators are && (logical AND), || (logical OR) and ! (logical NOT, also called logical negation). Ex: if (gender == 1 && age >= 65) ++seniorFemales; ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

Logical Operators (cont.) Logical AND (&&) Operator The && (logical AND) operator is used to ensure that two conditions are both true before we choose a certain path of execution. The simple condition to the left of the && operator evaluates first. If necessary, the simple condition to the right of the && operator evaluates next. The right side of a logical AND expression is evaluated only if the left side is true. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

©1992-2014 by Pearson Education, Inc. All Rights Reserved.

©1992-2014 by Pearson Education, Inc. All Rights Reserved.

Logical Operators (cont.) Logical OR (||) Operator The || (logical OR) operator determines if either or both of two conditions are true before we choose a certain path of execution. The && operator has a higher precedence than the || operator. Both operators associate from left to right. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

©1992-2014 by Pearson Education, Inc. All Rights Reserved.

Write a C++ statement(s) to accomplish the following: - If semesterAverage >= 90 or finalExam >= 90, print “Student grade = A”. ANS: if ((semesterAverage >= 90) || (finalExam >= 90)) cout << "Student grade is A" << endl;

Logical Operators (cont.) Logical Negation (!) Operator C++ provides the ! (logical NOT, also called logical negation) operator to “reverse” a condition’s meaning. The unary logical negation operator has only a single condition as an operand. You can often avoid the ! operator by using an appropriate relational or equality operator. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

©1992-2014 by Pearson Education, Inc. All Rights Reserved.