1 / 49

LESSON 03

LESSON 03. Overview of Previous Lesson(s). Over View. IDE An IDE or interactive development environment is a software application that provides comprehensive facilities to computer programmers for software development. 

media
Télécharger la présentation

LESSON 03

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. LESSON 03

  2. Overview of Previous Lesson(s)

  3. Over View • IDE • An IDE or interactive development environment is a software application that provides comprehensive facilities to computer programmers for software development.  • An IDE normally consists of a source code editor, build automation tools and a debugger.

  4. Over View.. • Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft Corporation. • It is used to develop console and graphical applications along with Windows Forms, Websites in both native code and with managed code. • It is supported by Microsoft Windows, Win Mobile, Win CE, .NET Framework.

  5. Over View… • C++ • Keywords Reserved words int, main, class, define • Identifiers Programmers defined variables • Operators Used to perform different types of operations on data • Punctuation , ; • Variables A named storage location in the computer’s memory for holding a piece of data.

  6. Over View… • C++ • Data Types When computer programs store data in variables, each variable must be assigned a specific data type. Some common data types include integers, floating point numbers, characters, strings, and arrays. • Steps performed by a program • Take the input • Process the data • Produce the output

  7. Parts of Program // sample C++ program Comment #include <iostream> Pre processor directive using namespace std; Which namespace to use int main() Main function beginning { beginning of block for main cout << "Hello, there!"; return 0; } ending of block for main

  8. TODAY’S LESSON

  9. Cout Object • The identifier cout (pronounced “C out”) is actually an object. • It is predefined in C++ to correspond to the standard output stream in IOSTREAM File. • The standard output stream normally flows to the screen display—although it can be redirected to other output devices. • << is called the insertion operator.

  10. Cin Object • The identifier cin(pronounced “C in”) is actually an object. • It is predefined in C++ to correspond to the standard input stream in IOSTREAM File. • This stream represents data coming from the keyboard unless it is redirected. • << is also called the extraction operator.

  11. The Using Directive • using namespace std; • A C++ program can be divided into different namespaces. • A namespace is a part of the program in which certain names are recognized; outside of the namespace they’re unknown. • Various program components such as cout are declared within this namespace. If we didn’t use the using directive, we would need to add the std name to many program elements. For example, in our example program we’d need to say • std::cout << “Hello World”;

  12. Escape Sequences • The name reflects the fact that the backslash causes an “escape” from the normal way characters are interpreted. • We already seen /n . • In this case the n is interpreted not as the character ‘n’ but as the next line.

  13. Escape Sequences..

  14. Escape Sequences… • Example cout << “\”Run, Spot, run,\” she said.”; • This translates to “Run, Spot, run,” she said.

  15. Library Functions • Many activities in C++ are carried out by library functions. • These functions perform file access, mathematical computations, and data conversion, among other things. • Calculate a square root of a number entered by the user.

  16. Library Functions.. // sqrt.cpp // demonstrates sqrt() library function #include <iostream> //for cout, etc. #include <cmath> //for sqrt() using namespace std; int main() { double number, answer; //sqrt() requires type double cout << “Enter a number: “; cin >> number; //get the number answer = sqrt(number); //find square root cout << “Square root is “ << answer << endl; //display it return 0; }

  17. Header & Library Files • Relationship between header and library files are bit confusing. • To use a library function, like sqrt(), you must link the library file that contains it to your program. • The appropriate functions from the library file are then connected to your program by the linker. • Each header file contains information for a particular group of functions. • The functions themselves are grouped together in a library file, but the information about them is scattered throughout a number of header files.

  18. Header & Library Files..

  19. Loops • Loops cause a section of your program to be repeated a certain number of times. • The repetition continues while a condition is true. • When the condition becomes false, the loop ends and control passes to the statements following the loop. • There are three kinds of loops in C++: • for loop • while loop • do loop

  20. For Loop • The for loop is the easiest C++ loop to understand. • The for loop executes a section of code a fixed number of times. It’s usually (although not always) used when you know, before entering the loop, how many times you want to executethe code. • Syntax of FOR loop • Example Program

  21. For Loop..

  22. For Loop.. // fordemo.cpp // demonstrates simple FOR loop #include <iostream> using namespace std; int main() { intj; //define a loop variable for(j=0; j<15; j++) //loop from 0 to 14, cout << j * j << “ “; //displaying the square of j cout << endl; return 0; {

  23. For Loop Variation • The increment expression doesn’t need to increment the loop variable. • It can perform any operation it likes. • In the next example it decrements the loop variable. • Next program, FACTOR, asks the user to type in a number, and then calculates the factorial of this number

  24. For Loop Variation.. // factor.cpp // calculates factorials, demonstrates FOR loop #include <iostream> using namespace std; int main() { unsigned int numb; unsigned long fact=1; //long for larger numbers cout << “Enter a number: “; cin>> numb; //get number for(int j=numb; j>0; j--) //multiply 1 by fact *= j; //numb, numb-1, ..., 2, 1 cout << “Factorial is “ << fact << endl; return 0; {

  25. While Loop • The for loop does something a fixed number of times. • What happens if you don’t know how many times you want to do something before you start the loop? • In this case a different kind of loop may be used: the while loop. • Syntax for while loop.. • Example for while loop …

  26. While Loop..

  27. While Loop… // endon0.cpp // demonstrates WHILE loop #include <iostream> using namespace std; int main() { intn = 99; // make sure n isn’t initialized to 0 while( n != 0 ) // loop until n is 0 cin>> n; // read a number into n cout << endl; return 0; }

  28. While Loop… • Program Output 1 27 33 144 9 0

  29. Do While Loop • In a while loop, the test expression is evaluated at the beginning of the loop. If the test expression is false when the loop is entered, the loop body won’t be executed at all. • In some situations this is what you want. But sometimes you want to guarantee that the loop body is executed at least once, no matter what the initial state of the test expression. • When this is the case you should use the do loop, which places the test expression at the end of the loop. • Syntax for do while loop.

  30. Do While Loop..

  31. Do While Loop… // divdo.cpp // demonstrates DO loop #include <iostream> using namespace std; int main() } long dividend, divisor; char ch; do //start of do loop { //do some processing cout << “Enter dividend: “; cin >> dividend; cout << “Enter divisor: “; cin >> divisor; cout << “Quotient is “ << dividend / divisor; cout << “, remainder is “ << dividend % divisor; cout << “\nDo another? (y/n): “; //do it again? cin>> ch; } while( ch != ‘n’ ); //loop condition return 0; }

  32. Do While Loop.. • Here’s an example of DIVDO’s output: Enter dividend: 11 Enter divisor: 3 Quotient is 3, remainder is 2 Do another? (y/n): y Enter dividend: 222 Enter divisor: 17 Quotient is 13, remainder is 1 Do another? (y/n): n

  33. When to use which Loop • The for loop is appropriate when you know in advance how many times the loop will be executed. • The while and do loops are used when you don’t know in advance when the loop will terminate. • The while loop when you may not want to execute the loop body even once. • The do loop when you’re sure you want to execute the loop body at least once.

  34. Decisions If If else Switch

  35. The if Statement • Tests a single or a series of conditions until one is found to be true. • Can be used to model thought processes such as:"If it is raining, take an umbrella”. • Example Program • Operational Flow

  36. The if Statement.. // ifdemo.cpp // demonstrates IF statement #include <iostream> using namespace std; int main() { intx; cout << “Enter a number: “; cin>> x; if( x > 100 ) cout << “That number is greater than 100\n”; return 0; {

  37. The if Statement... • Output of the program Enter a number: 2000 That number is greater than 100 • If the number entered is not greater than 100, the program will terminate without printing the second line.

  38. The if Statement…

  39. The if/else Statement • The if statement lets you do something if a condition is true. If it isn’t true, nothing happens. • But suppose we want to do one thing if a condition is true, and do something else if it’s false. • That’s where the if...else statement comes in. It consists of an if statement, followed by a statement or block of statements, followed by the keyword else, followed by another statement or block of statements. • Syntax and Example ..

  40. The if/else Statement..

  41. The if/else Statement… // ifelse.cpp // demonstrates IF...ELSE statememt #include <iostream> using namespace std; int main() { intx; cout << “\nEnter a number: “; cin>> x; if( x > 100 ) cout << “That number is greater than 100\n”; else cout << “That number is not greater than 100\n”; return 0; }

  42. The if/else Statement… • Here’s output from two different invocations of the program: Enter a number: 300 That number is greater than 100 Enter a number: 3 That number is not greater than 100

  43. The if/else Statement… • Flow Chart of If / else

  44. The Switch Statement • If you have a large decision tree, and all the decisions depend on the value of the same variable, you will probably want to consider a switch statement instead of a ladder of if...else or else if constructions. • Syntax of the Switch statement. • Example of the switch statement.

  45. The Switch Statement..

  46. The Switch Statement… // stars.cpp // demonstrates SWITCH statement #include <iostream> using namespace std; int main() { int month; // birth month cout << “\nEnter month number from 1, 8, or 9: “; cin>> month; //user enters month switch(month) //selection based on month { case 1: //user entered 1 cout << “Star for 1st month is Capricon\n”; break; case 8: //user entered 8 cout << “Star for 8th month is Leo\n”; break; case 9: //user entered 9 cout << “Star for 9th month is Virgo\n”; break; } return 0; }

  47. The Switch Statement… • Default switch statement • This keyword gives the switch construction a way to take an action if the value of the loop variable doesn’t match any of the case constants. • No break is necessary after default, since we’re at the end of the switch anyway.

  48. Operation of Switch Statement

  49. Thank You

More Related