CECS 121 Test 1

Preview:

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

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

#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 = 20const float PI = 3.14

Can we do this? const int x;

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)

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(); }

Can you explain what the code is doing?

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)

Syntax:

Else if:

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

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

}}

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.