1 / 37

CECS 121 Test 1

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 functions. The main function is special because the values it returns are returned to the operating system

tahlia
Download Presentation

CECS 121 Test 1

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. CECS 121 Test 1

  2. 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

  3. #include <stdio.h> 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

  4. 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

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

  6. 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

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

  8. Can you explain what the code is doing?

  9. Syntax: scanf(“conversion specifier”, variable);

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

  11. 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)

  12. Syntax:

  13. Else if:

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

  15. 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?

  16. do { } while ( condition ); • What is the main difference between “Do while” and “while”?

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

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

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

  20. 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 }

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

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

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

  24. 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

  25. 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?

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

More Related