CIS 15 NOTES - City University of New York



Review of C

Before a C program is compiled, it is first edited by a preprocessor. Commands intended for the preprocessor are called directives.

#include

This directive states that the information in is to be “included” into the program before it is compiled. contains information about C’s standard input/output library. C has a number of headers like . C has no built-in “read” and “write” commands, so we include . i/o is provided by the functions in the standard library. (scanf/printf).

Constants – values that don’t change during execution.

#define TRUE 1

#define FALSE 0

Selection, Iteration, Jump Statements

Selection statements – The if and switch statements allow a program to select a particular execution path from a set of alternatives

I. Selection Constructs

• if

• if/else

• nested if

• switch/case

if statement

In pseudocode (structured English) we might express a decision as:

If student’s grade is greater than or equal to 60

Print “Passed”

if (grade >= 60)

printf(“Passed”);

Syntax for if statement:

if ;

A boolean expression represents a condition that is either true or false. It is formed using operands (constants, variables) and operators (arithmetic operators, relational operators, logical operators).

|Arithmetic |

|+ - |

|* / % |

|arithmetic functions |

|Relational |

|< less than > greater than |

|== equal to |

|!= not equal to |

|= greater than or equal to |

|Logical |

||| or |

|&& and |

|! not |

Ex. if ((x > 0) && (a == b)) …

Operator Precedence

|Operator |Associativity |Type |

|( ) |L ( R |parenthesis |

| ++ – – + – ! |R ( L |unary operators |

|* ? % |L ( R |multiplicative |

|+ – |L ( R |additive |

|> |L ( R |insertion |

|< >= |L ( R |relational |

|= = != |L ( R |equality |

|&& |L ( R |and |

||| |L ( R |or |

|?: |R ( L |conditional |

|= += –= *= /= %= |R ( L |assignment |

if/else statement

In pseudocode:

If student’s grade is greater than or equal to 60

Print “Passed”

else

Print “Failed”

In C

if (grade >= 60)

printf(“Passed\n”);

else

printf(“Failed\n”);

55

Failed

syntax for if/else statement:

if [else ];

The C conditional operator - ?:

Same as the if/else structure but more compact. This is the only ternary operator in C. Example:

grade>=60? printf(“Passed”) : printf(“Failed”);

In this case, the conditional expression evaluates to “Passed” if the condition is true and to “Failed” if the condition is false.

Nested-if

/*nestedIf.c

This program converts a test score into

its equivalent letter grade. */

#include

int main()

{

int score;

printf("Enter the test score: ");

scanf(“%d”, &score);

if (score > 100) printf("Error: score is out of range.\n") ;

else if (score >= 90) printf(“A\n”);

else if (score >= 80) printf(“B\n”);

else if (score >= 70) printf(“C\n”);

else if (score >= 60) printf(“D\n”);

else if (score >= 0) printf(“F\n”);

else printf("Error: score is out of range.");

return 0; //successful termination

}

Enter the test score: 75

C

Switch

/*switch.c

This program converts a test score into

its equivalent letter grade, using a switch statement.*/

#include

int main()

{

int score;

printf("Enter the test score: ");

scanf(“%d”, &score);

switch (score/10) {

case 10:

case 9: printf(“A\n”); break;

case 8: printf(“B\n”); break;

case 7: printf(“C\n”); break;

case 6: printf(“D\n”); break;

case 5:

case 4:

case 3:

case 2:

case 1:

case 0: printf(“F\n”); break;

default: printf(“Error: score is out of range.\n");

}

return 0; //successful termination

}

Enter the test score: 83

B

II. Iteration Constructs

Iteration Statements – The while, do, and for statements support iteration (looping)

In order to control iteration, we use one of three structured control statements.

• for

• while

• do/while

A Counting Program: We would like to write a program that will print the integers between 1 and 20.

//counting2.c

// A Counting Program

// Using a FOR Loop

#include

int main()

{

int count ;

for (count=1; count ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download