37
CECS 121 Test 1

CECS 121 Test 1

  • Upload
    tahlia

  • View
    42

  • Download
    5

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: CECS 121 Test 1

CECS 121 Test 1

Page 2: CECS 121 Test 1
Page 3: 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

Most main functions in this course do not take or pass information to the operating system

Page 4: CECS 121 Test 1

#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

Page 5: CECS 121 Test 1

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

Page 6: CECS 121 Test 1

To declare a constant (read only) value:const int x = 20const float PI = 3.14

Can we do this? const int x;

Page 7: CECS 121 Test 1

TYPE SIZE VALUES

bool 1 byte true (1) or false (0)

char 1 byte ‘a’ to‘z’ , ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on

int 4 bytes -2,147,483,648 to 2,147,483,647

short 2 bytes -32,768 to 32,767

long 4 bytes -2,147,483,648 to 2,147,483,647

float 4 bytes + - (1.2 x 10^-38 to 3.4 x 10^38)

double 8 bytes +- (2.3 x 10^-308 to -1.7 x 10^308)

Page 8: CECS 121 Test 1
Page 9: CECS 121 Test 1

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

Page 10: CECS 121 Test 1
Page 11: CECS 121 Test 1

#include <stdio.h>

int main() {int x; for ( x = 0; x < 10; x++ ) {

printf( "%d\n", x ); }

getchar(); }

Page 12: CECS 121 Test 1

Can you explain what the code is doing?

Page 13: CECS 121 Test 1
Page 14: CECS 121 Test 1
Page 15: CECS 121 Test 1

Syntax:

scanf(“conversion specifier”, variable);

Page 16: CECS 121 Test 1
Page 17: CECS 121 Test 1

Do you know the answers to these?

A. !( 1 || 0 )

B. !( 1 || 1 && 0 )

C. !( ( 1 || 0 ) && 0 )

Page 18: CECS 121 Test 1

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)

Page 19: CECS 121 Test 1

Syntax:

Page 20: CECS 121 Test 1

Else if:

Page 21: CECS 121 Test 1

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

Page 22: CECS 121 Test 1
Page 23: CECS 121 Test 1
Page 24: CECS 121 Test 1
Page 25: CECS 121 Test 1

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?

Page 26: CECS 121 Test 1
Page 27: CECS 121 Test 1

do { } while ( condition );

What is the main difference between “Do while” and “while”?

Page 28: CECS 121 Test 1

while ( condition ) { Code to execute while the condition is true }

do { } while ( condition );

Do{} while() executes code at least once!

Page 29: CECS 121 Test 1

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

Page 30: CECS 121 Test 1

#include <stdio.h>

int main() {

int x=0;

while (x<100) {

printf( "%d\n", x*5 ); x++;

}

getchar();}

Page 31: CECS 121 Test 1

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 }

Page 32: CECS 121 Test 1

#include <stdio.h>

int main() { int x;

for ( x = 0; x < 10; x++ ) { printf( "%d\n", x );

} getchar(); }

Page 33: CECS 121 Test 1

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

Page 34: CECS 121 Test 1

#include <stdio.h>

int main() { int x; for ( x = 0; x < =20; x++ ){

printf( "%d\n", x*5 ); }

getchar(); }

Page 35: CECS 121 Test 1

x++; x--; Postfix++x; --x; Prefixmain(){ 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:01

Page 36: CECS 121 Test 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?

Page 37: CECS 121 Test 1

#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 );

}}

kmnich01
"In this program, the condition (x==7) becomes true after the third iteration. Next, the break statement is executed and program control is sent out from the for loop and continues with the printf statement"(Vine)
kmnich01
7 is not included in the output because once x==7 is true, the rest of the statements are skipped and the next iteration is executed.