370 likes | 476 Views
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
E N D
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 <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
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
To declare a constant (read only) value: const int x = 20 const float PI = 3.14 Can we do this? const int x;
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
#include <stdio.h> int main() { int x; for ( x = 0; x < 10; x++ ) { printf( "%d\n", x ); } getchar(); }
Syntax: scanf(“conversion specifier”, variable);
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)
Can you write code that will ask a user to enter a number 1 , 2 , or 3 and print out the following:
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?
do { } while ( condition ); • What is the main difference between “Do while” and “while”?
while ( condition ) { Code to execute while the condition is true } • do { } while ( condition ); • Do{} while() executes code at least once!
Write a program using a WHILE Loop to display all of the multiples of 5 from 0 to 100.
#include <stdio.h> int main() { int x=0; while (x<100) { printf( "%d\n", x*5 ); x++; } getchar(); }
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 }
#include <stdio.h> int main() { int x; for ( x = 0; x < 10; x++ ) { printf( "%d\n", x ); } getchar(); }
Write a program using a FOR Loop to display all of the multiples of 5 from 0 to 100.
#include <stdio.h> int main() { int x; for ( x = 0; x < =20; x++ ) { printf( "%d\n", x*5 ); } getchar(); }
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
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 <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 ); } }