1 / 32

CS31

CS31. You Lu CS31-1K TA. Hello World!. Variable. Declare Variables before you use it t ype variable_list ; e.g. int i , j, k; Numeric Ranges (textbook page 9 ) int : double:

Download Presentation

CS31

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. CS31 You Lu CS31-1K TA

  2. Hello World!

  3. Variable • Declare Variables before you use it type variable_list; e.g. inti, j, k; • Numeric Ranges (textbook page 9) int: double: • Check its range before you use this type of variable. Make sure the value you want to store in this variable will not be out of its range.

  4. Out of the Range You input 8888888888, but it outputs another different number.

  5. I/O • cin >> • cout <<

  6. Input one word or string cin>>s; //can only input one word, e.g., only my first name. getline(cin, s); //can input the whole string, e.g., my first and last name

  7. cin.ignore(10000, '\n') Use cin.ignore(10000, '\n')  after reading a number if the next input will be of a string (using getline)

  8. Arithmatic • Division: 5/2=2; 5.0/2=2.5 • as long as one of the number is a “double” we have a “double” division. • Mod: 5%2=1 (remainder) • Casting: • int a = 2.5 a = ? 2 • (double)5/2 = ? 2.5 • (double) (5/2) = ? 2

  9. Visual Studio 2010 Check the output of compiler to see if there is any warningor error every time.

  10. iostream precision • Default is 6 digits • If “double a = 1234.56789”; “cout << a” will only display 1234.57 • Here you can use “cout.precision(9)” to set the output as 9 digits. • See the example below.

  11. iostream precision Be more careful to use “cout”

  12. header file • #include <string> //standard built-in libraries • #include “ccc_time.h” //user-defined libraries

  13. if … • if (condition) statement; condition: • non-zero value means true; • zero value means false;

  14. another example

  15. if…else… if (expression) statement; else statement; Nested if statement: if() { if() {…} else {…} } else if() { } else { }

  16. Boolean expression • Greater than or equal: “>=“ • Less than or equal: “<=” • Equal: “==” • Not equal: “!=” • Logic expression: • AND: && • OR: || • NOT: !

  17. priority • Arithmetic operator > relational operator 10 > 1+2 ↔ 10 > (1+2) • “<<“ operator > logic operator cout << “true AND false is ” << (true && false);

  18. priority

  19. while loop while (expression) { statement; } do{ statement; } while (expression)

  20. example

  21. for loop for (initialization; condition; increment) { statement; } infinite loop: for(;;) { }

  22. “continue” skip the current round loop, go to the next round loop for(…) { code 1; code 2; continue; code 3; }

  23. example

  24. “break” stop the current loop and get out of the loop. for (…) { code 1; code 2; break; code 3; } code 4

  25. example

  26. “goto” • goto and “lable” must be in the same function. lable: goto [lable];

  27. another example Jump out from a deep nested loop for(…) { for(…) { while(…) { if(…) goto stop; … } } } stop: cout << “Error in program. \n”; //”break” can only jump out from only one loop

  28. Online reference • Some websites are really helpful for you to learn C++, like http://www.cplusplus.com/ • More detailed reference: Microsoft MSDN library • http://msdn.microsoft.com/en-us/vstudio/hh388567 • http://msdn.microsoft.com/en-us/library/cscc687y(v=vs.80).aspx • http://msdn.microsoft.com/en-us/library/3bstk3k5.aspx

  29. Look up “rand()”

  30. My webpage for CS31 1K http://www.cs.ucla.edu/~youlu/CS31-1K.html

More Related