Download presentation
Presentation is loading. Please wait.
1
Thursday, December 07, 2006 “Vision without action is a daydream, Action without a vision is a nightmare.” - Japanese Proverb
2
CS 192 / CMPE 191 Change in Grading Breakup 20% Quizzes + Homework 20% Programming Assignments 25% Midterm 35% Final Exam (25% Paper-based, 10% Lab exam)
3
CS 192 / CMPE 191 Sections 1 & 2
4
Comments have two forms in C++ //I am a Single line comment /*I am a Multi-line comment and I can span multiple lines This is my line number 5 */
5
#include int main( ) { cout <<"Welcome to CS192/CMPE191"; return 0; //this is a simple program } //end block of main
6
int main(){cout <<"Welcome to CS192/CMPE191";return 0;} Bad programming style!
7
Statements end with a semi-colon ;
8
§Omitting a semicolon is a syntax error. §Other errors could be: mis-typing a keyword omitting braces or parenthesis forgetting << operator in cout … §Compiler can catch such errors.
9
§More cout examples
10
Constants cout<< -23; cout<< “Hello”; cout<< 24.5;
11
Constants cout<< -23 <<“\n”; cout<< “Hello”<<“\n”; cout<< 24.5 <<“\n”;
12
§Programs need to manipulate data.
13
§C++ uses variables to name and store data.
14
Variables and Identifiers §The compiler assigns a memory location to each variable in the program. §The name of the variable (the identifier) is used to refer to that memory location.
15
Variables and Identifiers §Identifiers l case sensitive l must begin with a letter or underscore l consist of letter, digits and underscore only (no special characters) l can not use reserve words (key words) §Use meaningful names for variables.
16
Variable declarations §What kind of data would you be storing in a variable.
17
§Variables – must be declared before use. int value; value = 23; cout<< value;
18
§Variables – must be declared before use. int value; value = 23; cout<< value; Assignment statement
19
Simple I/O cin l Used for input l uses the >> (input operator) cout l Used for output l uses the << (output) operator
20
/***************************************************** * Sum two numbers ******************************************************/ #include //declares standard I/O library int main()//must have one and only one function named main { int number1, number2;//declare two integer variables cout << “enter two integers” << endl;//prompt for input cin >> number1 >> number2; //input values from keyboard cout << number1 + number2; //output sum to the screen return 0; }//end block of main
21
#include int main() { int number_of_pods, peas_per_pod, total_peas; cout<<"Press return after entering number\n"; cout<<"Enter number of pods\n"; cin>>number_of_pods; cout<<"Enter number of peas in a pod\n"; cin>>peas_per_pod; total_peas = number_of_pods * peas_per_pod; cout<<"If you have "; cout<<number_of_pods; cout<< " pea pods\n"; cout<<"and "; cout<<peas_per_pod; cout<<" peas in each pod, then\n"; cout<<"you have "; cout<<total_peas; cout<<" peas in all the pods.\n"; return 0; }
22
#include int main() { int number_of_pods, peas_per_pod, total_peas; cout<<"Press return after entering number\n"; cout<<"Enter number of pods\n"; cin>>number_of_pods; cout<<"Enter number of peas in a pod\n"; cin>>peas_per_pod; total_peas = number_of_pods * peas_per_pod;
23
cout<<"If you have "; cout<<number_of_pods; cout<< " pea pods\n"; cout<<"and "; cout<<peas_per_pod; cout<<" peas in each pod, then\n"; cout<<"you have "; cout<<total_peas; cout<<" peas in all the pods.\n"; return 0; }
24
Enter number of pods 20 Enter number of peas in a pod 8 If you have 20 pea pods and 8 peas in each pod, then you have 160 peas in all the pods.
25
C++ Data Types l char l short l int l float l double l … --
26
Type specifiers e.g. int, long, short etc Specific ranges of values are system dependent §On many systems short integer ranges from -32768 to 32767 §unsigned short has range of values 0 to 65535 §int4 bytes-2147,438,648 to 2,147,483,647 §float 4 bytes3.4E-38 to 3.4E+38 §Double 8 bytes1.7E-308 to 1.7E+308
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.