Presentation is loading. Please wait.

Presentation is loading. Please wait.

CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.

Similar presentations


Presentation on theme: "CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different."— Presentation transcript:

1 CECS 121 Test 1

2

3 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

4 #include Using a directive to include a header file stdio.h = standard input output header file stdlib.h = ‘system’ commands Iostream.h= Input/Output stream header library Math.h= The Math library header file

5  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

6 To declare a constant (read only) value: const int x = 20 const float PI = 3.14 Can we do this? const int x;

7 TYPESIZEVALUES bool1 bytetrue (1) or false (0) char1 byte‘a’ to‘z’, ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on int4 bytes-2,147,483,648 to 2,147,483,647 short2 bytes-32,768 to 32,767 long4 bytes-2,147,483,648 to 2,147,483,647 float4 bytes+ - (1.2 x 10^-38 to 3.4 x 10^38) double8 bytes+- (2.3 x 10^-308 to -1.7 x 10^308)

8

9

10 X++; Tells the function to use the current value of x and increment it by 1. ++x; Increment the value of x by 1 and use the new value for calculations. x--; Tells the function to use the current value of x and decrease its value by 1. --x; Decrease the value of x by 1 and use the new value for calculations. Int 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

11 Variables and Constants To declare a constant (read only) value: const int x = 20 const float PI = 3.14 What is wrong with this code?

12 Variables and Constants Are the following valid names for a variables in C: 1) _name 1) $student_number 1) #salary 1) 9pins+1 and 1) First-name 1) TheIncrediblyEdiblySpiderManWithTheVeryLongNameOfMoreTha n50Characters

13

14 #include int main() { int x; for ( x = 0; x < 10; x++ ){ printf( "%d\n", x ); } getchar(); }

15  Can you explain what the code is doing?

16

17

18  Syntax: scanf(“conversion specifier”, variable);

19

20 Do you know the answers to these?  A. !( 1 || 0 )  B. !( 1 || 1 && 0 )  C. !( ( 1 || 0 ) && 0 )

21  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)

22 Syntax:

23 Else if:

24  Can you write code that will ask a user to enter a number 1, 2, or 3 and print out the following:

25

26

27

28  while ( condition ) { Code to execute while the condition is true }  Quiz: Can you write a program that prints x while x increments from 0 to 10?

29

30  do { } while ( condition );  What is the main difference between “Do while” and “while”?

31  while ( condition ) { Code to execute while the condition is true }  do { } while ( condition );  Do{} while() executes code at least once!

32  Write a program using a WHILE Loop to display all of the multiples of 5 from 0 to 100.

33 #include int main() { int x=0; while (x<100) { printf( "%d\n", x*5 ); x++; } getchar(); }

34  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 }

35 #include int main() { int x; for ( x = 0; x < 10; x++ ){ printf( "%d\n", x ); } getchar(); }

36  Write a program using a FOR Loop to display all of the multiples of 5 from 0 to 100.

37 #include int main() { int x; for ( x = 0; x < =20; x++ ) { printf( "%d\n", x*5 ); } getchar(); }

38 x++; x--; Postfix ++x; --x; Prefix main() {int x = 0; int y = 0; printf(“\n The value of y is %d \n”, y++); printf(“\n The value of x is %d \n”, ++x); } Answer: 0 1

39  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?

40 #include main() { int x; for ( x = 10; x >5; x-- ) { if (x==7) break; } printf( “\n %d \n ”, x ); } #include main() { int x; for ( x = 10; x >5; x-- ) { if (x==7) continue; printf( “\n %d \n ”, x ); }


Download ppt "CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different."

Similar presentations


Ads by Google