1 / 50

PROGRAMMING LANGUAGES

PROGRAMMING LANGUAGES. AU/MITM/1.6 By Mohammed A. Saleh. Loop Structures. The FOR loop This is a loop structure that will perform repetitive tasks The for loop has several parts that handle the repeated actions: Setting a value initially

thane-glenn
Télécharger la présentation

PROGRAMMING LANGUAGES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PROGRAMMING LANGUAGES AU/MITM/1.6 By Mohammed A. Saleh

  2. Loop Structures • The FOR loop • This is a loop structure that will perform repetitive tasks • The for loop has several parts that handle the repeated actions: • Setting a value initially • Performing a test to see whether the loop should continue

  3. Loop Structures • Executing the loop actions • Updating value(s) used for the test • The initialization (a), test (b), and update actions(d) constitute a three-part control section enclosed in parentheses. • Each part is an expression, and semicolons separate the expressions from each other. • The statement following the control section is called the body of the loop, and it is executed as long as the test expression remains true.

  4. Loop Structures • The for loop syntax: for (initialization; test-expression; update-expression) body • Initialization is done just once. This is used as an expression to set a variable to a starting value and then use the variable to count the loop cycles.

  5. Loop Structures • The test-expression determines whether the loop body gets executed. This is normally a relational expression, i.e, one that compares two values, e.g (i>5) • Example of a program used for a numeric test:

  6. Loop Structures // num_test.cpp -- use numeric test in for loop #include <iostream> int main() { int limit; cout << “Enter the starting countdown value: “; cin >> limit; int i; for (i = limit; i; i--) // quits when i is 0 cout << “i = “ << i << “\n”; cout << “Done now that i = “ << i << “\n”; return 0; }

  7. Loop Structures • Here is the output from the program: Enter the starting countdown value: 4 i = 4 i = 3 i = 2 i = 1 Done now that i = 0 • The loop terminates when i reaches 0.

  8. Loop Structures • The for loop is an entry-condition loop. This means the test expression is evaluated before each loop cycle. The loop never executes the loop body when the test expression is false. • The update-expression is evaluated at the end of the loop, after the body has been executed. Typically, it’s used to increase or decrease the value of the variable keeping track of the number of loop cycles.

  9. Loop Structures Figure 1.0: The FOR loop structure

  10. Loop Structures • The DO… WHILE loop • This is different from the other two loops because it is an exit-condition loop. • It first of all executes the body of the loop and only then evaluates the test expression to see whether it should continue looping. • If the condition evaluates to false, the loop terminates; otherwise, a new cycle of execution and testing begins

  11. Loop Structures • This loop always executes at least once because its program flow must pass through the body of the loop before reaching the test. • The syntax for the do … while loop: do body while (test-expression); • The body portion can be a single statement or a brace-delimited statement block.

  12. Loop Structures Figure 2.0: The DO … WHILE loop structure

  13. Loop Structures • An entry-condition loop is usually a better choice than an exit-condition loop because the entry-condition loop checks before looping. • A do … while example:

  14. Loop Structures // dowhile.cpp -- exit-condition loop #include <iostream> int main() { int n //variable declaration cout << “Enter numbers in the range 1-10 to find “; cout << “my favorite number\n”; do { cin >> n; // execute body } while (n != 7); // then test cout << “Yes, 7 is my favorite.\n” ; return 0; }

  15. Loop Structures • A sample run of the program: Enter numbers in the range 1-10 to find my favorite number 9 4 7 Yes, 7 is my favorite.

  16. for VERSUS while • These are essentially equivalent loops. This for loop could be written this way: for (init-expression; test-expression; update-expression) { statement(s) } could be rewritten this way: init-expression; while (test-expression) { statement(s) update-expression; }

  17. for VERSUS while • Similarly this while loop: while (test-expression) body could be rewritten this way: for ( ;test-expression;) body

  18. Guidelines when designing a loop • Identify the condition that terminates loop execution. • Initialize that condition before the first test. • Update the condition in each loop cycle, before the condition is tested again.

  19. Course Schedule ➩Module 2 Fundamentals of C++ - Operators & Expressions – Data I/O – Control Structures – Storage Classes – Arrays and Strings.

  20. Fundamentals of C++ C++ Initiation • C++ is case sensitive; that is, it discriminates between uppercase characters and lowercase characters, e.g it uses cout and NOT Cout or COUT. • The compiler is also spelling sensitive, i.e using kout instead of cout. • The cpp filename extension is a common way to indicate a C++ program.

  21. Fundamentals of C++ // Example 1.0 // myfirst.cpp--displays a message #include <iostream> // a PREPROCESSOR directive int main() // function header { // start of function body cout << “Come up and C++ me some time.”; // message cout << endl; // start a new line cout << “You won’t regret it!” << endl; // more output return 0; // terminate main() } // end of function body

  22. Fundamentals of C++ • C++ programs are constructed from building blocks called functions. Typically, you organize a program into major tasks and then design separate functions to handle those tasks. • Example 1.0 contains a single function named main(). The myfirst.cpp program has the following elements:

  23. Fundamentals of C++ • Comments, indicated by the //prefix • A preprocessor #include directive • A function header: int main() • A function body, delimited by {and } • Statements that uses the C++ cout facility to display a message • A return statement to terminate the main() function

  24. Fundamentals of C++ The main () Function • Its fundamental structure: int main() { statements return 0; }

  25. Fundamentals of C++ Figure 3.0: The main() Function • A function constitutes of a function definition, which has two parts i) function header and ii) function body

  26. Fundamentals of C++ C++ Statements • A C++ program is a collection of functions, and each function is a collection of statements. • Kinds of statements: • Declaration statement – creates a variable • Assignment statement – provides a value for the variable

  27. Fundamentals of C++ // carrots.cpp -- food processing program // uses and displays a variable #include <iostream> int main() { int carrots; // declare an integer variable carrots = 25; // assign a value to the variable cout << “I have “; cout << carrots; // display the value of the variable cout << “ carrots.”; cout << endl; carrots = carrots - 1; // modify the variable cout << “Crunch, crunch. Now I have “ << carrots << “ carrots.” << endl; return 0; }

  28. Fundamentals of C++ // Example 1.1 - carrots.cpp -- food processing program // uses and displays a variable #include <iostream> int main() { int carrots; // declare an integer variable carrots = 25; // assign a value to the variable cout << “I have “; cout << carrots; // display the value of the variable cout << “ carrots.”; cout << endl; carrots = carrots - 1; // modify the variable cout << “Crunch, crunch. Now I have “ << carrots << “ carrots.” << endl; return 0; }

  29. Fundamentals of C++ • Program output: I have 25 carrots. Crunch, crunch. Now I have 24 carrots. Declaration statements • To store an item of information in a computer, you must identify both the storage location and how much memory storage space the information requires.

  30. Fundamentals of C++ • Why Must Variables Be Declared? • Comparing C, Pascal and C++ declaration.

  31. Fundamentals of C++ Assignment statements • Assigns a value to a storage location. For example carrots = 25; • Assigns the integer 25 to the location represented by the variable carrots. • The = symbol is called the assignment operator • The second assignment statement in Example 1.1 demonstrates that you can change the value of a variable:

  32. Fundamentals of C++ carrots = carrots - 1; // modify the variable • (carrots - 1) is an arithmetic example. The computer will subtract 1 from 25.

  33. Fundamentals of C++ Dealing with Data • The essence of object-oriented programming (OOP) is designing and extending your own data types. But before you can create your own types, you must know and understand the types that are built in to C++ because those types will be your building blocks. • The built-in C++ types come in two groups: fundamental types and compound types.

  34. Fundamentals of C++ Simple Variables • Programs typically need to store information, to do that, the program must keep track of three fundamental properties: • Where the information is stored • What value is kept there • What kind of information is stored • Example: int braincount; braincount = 5;

  35. Fundamentals of C++ • These statements tell the program that it is storing an integer and that the name braincount represents the integer’s value, 5. Names for Variables • Use meaningful names. • Naming rules: • The only characters you can use in names are alphabetic characters, numeric digits, and the underscore (_) character.

  36. Fundamentals of C++ • The first character in a name cannot be a numeric digit. • Uppercase characters are considered distinct from lowercase characters. • You can’t use a C++ keyword for a name. • Names beginning with two underscore characters or with an underscore character followed by an uppercase letter are reserved for use by the implementation—that is, the compiler and the resources it uses.

  37. Fundamentals of C++ • Names beginning with a single underscore character are reserved for use as global identifiers by the implementation. • C++ places no limits on the length of a name, and all characters in a name are significant. • Some valid and invalid C++ names: • int poodle; • int Poodle; • int POODLE;

  38. Fundamentals of C++ • Int terrier; • int my_stars3; • int _Mystars3; • int 4ever; • int double; • int begin; • int __fools; • int honky-tonk; • int the_very_best_variable_i_can_be_112;

  39. Fundamentals of C++ Data Types • Integer types • Numbers with no fractional part, such as 2, 98, –5286, and 0. • C++ provides several choices of integer types to meet a program’s particular requirement. • These integer types differ in the amount of memory they use to hold an integer. • Basic integer types, in order of increasing width, are short, int, and long.

  40. Fundamentals of C++ • Each comes in both signed and unsigned versions. • By having several choices of integer types, C++ offers a flexible standard with some guaranteed minimum sizes. • A short integer is at least 16 bits wide. • An int integer is at least as big as short. • A long integer is at least 32 bits wide and at least as big as int.

  41. Fundamentals of C++ • You use these type names to declare variables just as you would use int: short score; // creates a type short integer variable int temperature; // creates a type int integer variable long position; // creates a type long integer variable Initialization • Combines assignment with declaration. For example: int uncles = 5; // initialize uncles to 5 int aunts = uncles; // initialize aunts to 5

  42. Fundamentals of C++ • You can even initialize a variable to an expression, provided that all the values in the expression are known when program execution reaches the declaration: int chairs = aunts + uncles + 4; // initialize chairs to 14 • The char Type • Designed to store characters, such as letters and numeric digits.

  43. Fundamentals of C++ • It’s guaranteed to be large enough to represent the entire range of basic symbols—all the letters, digits, punctuation, and the like. • Example program: // chartype.cpp -- the char type #include <iostream> int main( ) { char ch; // declare a char variable cout << “Enter a character: “ << endl; cin >> ch; cout << “Holla! “; cout << “Thank you for the “ << ch << “ character.” << endl; return 0; }

  44. Fundamentals of C++ • Here’s the output from the program: Enter a character: M Holla! Thank you for the M character. • The bool type • Represents a Boolean variable one whose value can be either true or false. bool isready = true;

  45. Fundamentals of C++ • The literals true and false can be converted to type int by promotion, with true converting to 1 and false to 0: int ans = true; // ans assigned 1 int promise = false; // promise assigned • Floating-point Types • C++ has three floating-point types: float, double, and long double • They are described in terms of the number of significant figures they can represent and the minimum allowable range of exponents

  46. Fundamentals of C++ • C++ has two ways of writing floating-point numbers. • Standard decimal- point notation 12.34 // floating-point 939001.32 // floating-point 8.0 // still floating-point • E notation 3.45E6 - means that the value 3.45 is multiplied by 1,000,000. The E6 means 10 to the 6th power.

  47. Fundamentals of C++ • E notation is most useful for very large and very small numbers. The const Qualifier • C++ provides better way to handle symbolic constants. • The const keyword to modify a variable declaration and initialization. • Suppose, for example, that you want a symbolic constant for the number of months in a year:

  48. Fundamentals of C++ const int MONTHS = 12; // Months is symbolic constant for 12 • Now you can use MONTHS in a program instead of 12. • The keyword const is termed a qualifier because it qualifies the meaning of a declaration. • General form: const type name= value;

  49. Review Questions 1. Why does C++ have more than one integer type? 2. Declare variables matching the following descriptions: • A short integer with the value 80 • An unsigned int integer with the value 42,110 • An integer with the value 3,000,000,000 3. What safeguards does C++ provide to keep you from exceeding the limits of an integer type?

  50. Review Questions • What is the distinction between 33L and 33? • Consider the two C++ statements that follow: char grade = 65; char grade = ‘A’; Are they equivalent?

More Related