Top Banner
CS31 You Lu CS31-1K TA
32

CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

Dec 14, 2015

Download

Documents

Beau Linam
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

CS31

You LuCS31-1K TA

Page 2: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

Hello World!

Page 3: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

Variable

• Declare Variables before you use ittype variable_list;e.g. int i, 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.

Page 4: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

Out of the RangeYou input 8888888888, but it outputs another different number.

Page 5: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

I/O

• cin >>• cout <<

Page 6: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

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

Page 7: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

cin.ignore(10000, '\n')

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

Page 8: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

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

Page 9: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.
Page 10: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

Visual Studio 2010

Check the output of compiler to see if there is any warning or error every time.

Page 11: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

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.

Page 12: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

iostream precision

Be more careful to use “cout”

Page 13: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

header file

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

Page 14: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

if …

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

Page 15: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

another example

Page 16: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

if…else… if (expression) statement;else statement;

Nested if statement:if(){ if() {…} else {…}}else if(){}else{}

Page 17: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

Boolean expression

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

Page 18: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

priority• Arithmetic operator > relational operator

10 > 1+2 ↔ 10 > (1+2)• “<<“ operator > logic operatorcout << “true AND false is ” << (true && false);

Page 19: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

priority

highest !

> >= < <=

== !=

&&

lowest ||

Page 20: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

while loop

while (expression){

statement;}

do{statement;

}while (expression)

Page 21: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

example

Page 22: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

for loop

for (initialization; condition; increment) {

statement;}

infinite loop:for(;;){}

Page 23: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.
Page 24: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

“continue”

skip the current round loop, go to the next round loop

for(…){

code 1;code 2;continue;code 3;

}

Page 25: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

example

Page 26: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

“break”

stop the current loop and get out of the loop.

for (…){

code 1;code 2;break;code 3;

}

code 4

Page 27: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

example

Page 28: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

“goto”

• goto and “lable” must be in the same function.

lable:goto [lable];

Page 29: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

another exampleJump out from a deep nested loopfor(…){

for(…){

while(…){

if(…) goto stop;…

}}

}stop:

cout << “Error in program. \n”;//”break” can only jump out from only one loop

Page 30: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

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/cscc687

y(v=vs.80).aspx

– http://msdn.microsoft.com/en-us/library/3bstk3k5.aspx

Page 31: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

Look up “rand()”

Page 32: CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input 8888888888, but it outputs another different number.

My webpage for CS31 1K

http://www.cs.ucla.edu/~youlu/CS31-1K.html