Transcript REACH TEST REVIEW
CECS 130 EXAM 1
Data Type Description Declaration Example
Integer Floating point number Character Whole numbers, positive or negative All numbers, positive or negative, decimals and fractions Representations of integer values known as character codes To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x; int x = float x = ; ; -3 , 0, 3 , 29 -0.35543 , 0.00, 554433.33281
char x =‘’ ; m, M, *
TYPE
bool char int short long float double
SIZE
1 byte 1 byte 4 bytes 2 bytes 4 bytes 4 bytes 8 bytes
VALUES
true (1) or false (0) ‘a’ to‘z’ , ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on -2,147,483,648 to 2,147,483,647 -32,768 to 32,767 -2,147,483,648 to 2,147,483,647 + - (1.2 x 10^-38 to 3.4 x 10^38) +- (2.3 x 10^-308 to -1.7 x 10^308)
} #include
Serve to control program execution and functionality. Must end with a semicolon(;) with the exception of: Comments: /* */ Preprocessor Directives: #include or #define Begin and end program identifiers: { } Function definition beginnings: main()
Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different functions.
The main function is special because the values it returns are returned to the operating system Most main functions in this course do not take or pass information to the operating system
#include
Definition: Escape sequences are specially sequenced characters used to format output \” Ex: printf(“ \ “This is quoted text \ “ “); \’ Ex: printf(“ \n A single quote looks like \’ \n”); \* *\ Comment Block
Character Integer Float (decimal) String Printf Format Tags: %c %d %f %s %[flags][width][.precision][length]specifier %[.precision]specifer -> %.2f
#include
} int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977); printf ("floats: %4.2f \n", 3.1416); printf ("%s \n", "A string"); return 0; }
printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977); printf ("floats: %4.2f \n", 3.1416); printf ("%s \n", "A string"); aA 1977650000 1977 0000001977 3.14
A string Can you create a tabular data using printf?
Example
printf(“%.1f”,3.123456); printf(“\n%.2f”,3.123456); printf(“\n%.3f”,3.123456); printf(“\n%.4f”,3.123456); printf(“\n%.5f”,3.123456); printf(“\n%.6f”,3.123456);
Printout
3.1
3.12
3.123
3.1235
3.12346
3.123456
#include
Operator Description
* Multiplication / % + Division Modulus (remainder) Addition Subtraction
Order of Precedence Description
( ) Parentheses are evaluated first, from innermost to outermost *, /, % + , Evaluated second, from Left to Right Evaluated last, from Left to Right
#include
Operator
== != > < >= <=
Description
Equal to Not Equal Greater Than Less Than Greater Than or Equal to Less Than or Equal to
Order of Precedence Description
&& AND condition || OR condition
x++; ++x; x--; --x; Tells the function to use the current value of x and increment it by 1.
Increment the value of x by 1 and use the new value for calculations.
Tells the function to use the current value of x and decrease its value by 1.
Decrease the value of x by 1 and use the new value for calculations.
x=0; printf(“The Value of x is: %d”, x++); printf(“\n The Value of x is: %d”,++x); Would results in: The Value of x is: 0 The Value of x is: 2
Application of: += & -=
x += y; x -= y;
Equivalent Equation
x = x + y; x = x – y;
Do you know the answers to these?
A. !( 1 || 0 ) B. !( 1 || 1 && 0 ) C. !( ( 1 || 0 ) && 0 )
A. !( 1 || 0 ) ANSWER: 0 B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR) C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)
What’s the syntax of While(Logical Condition){ } Do{ } While(Logical Condition) What’s the difference between While and Do- While()?
while ( condition ) { Code to execute while the condition is true } do { } while ( condition ); Do{} while() executes code at least once!
Use when the number of iterations is already known Syntax: for ( variable initialization; condition; variable increment/decrement) { Code to execute while the condition is true }
Write a program using a FOR Loop to display all of the multiples of 5 from 0 to 100.
#include
Use to manipulate flow in loops What does a Break statement do when executed within a loop?
What does a Continue statement do when executed within a loop?
#include
Function Prototype Syntax return-type function_name ( arg_type arg1, ..., arg_type argN) Function Prototypes tell you the data type returned by the function, the data type of parameters, how many parameters, and the order of parameters Function definitions implement the function prototype Where are function prototypes located in the program?
Where do you find function definitions?
Where are function prototypes located in the program?
Answer: before the Main(){} Function!
Function Definitions are self contained outside of the Main(){} function
#include
#include
#include
Variable scope defines the life time of a variable Local Scope: defined within functions and loses scope after function is finished. Can reuse in other functions (ex. p.123) Global Scope: defined outside of functions and can be accessed by multiple functions