Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:

Similar presentations


Presentation on theme: "CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:"— Presentation transcript:

1 CS113 Introduction to C Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.eduvetsikas@cs.cornell.edu Lecture 2 : August 28 webpage: http://www.cs.cornell.edu/Courses/cs113/2000FA/cs113.htm

2 2 Relational, Logical, and Equality Operators Think of these as returning true/false (logical) values, represented by integer values 0 (false) and 1 (true) Relational operators: >, <=, <, <= Equalify operators: ==, != These binary operators can be applied to built-in data types such as int, float, etc. (will discuss strings in future) Examples: (3 != 4) evaluates to 0. Hence, printf( “%d”, (3 != 4)); (in a program) would print out 0. IMPORTANT! == (two equals) vs. = (one equal) common source of programmer errors in C. –Assignment has a value (remember?) Logical operators: &&, ||, !

3 3 Syntax: if( expression ) statement1 [else statement2] Recall that statements are either single commands terminated by semicolons, or blocks of commands delimited by braces {}. if( expression ) same as if( expression != 0 ) if(( year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ) printf( “%d is a leap year\n”, year ); Instead of: if (year % 100 != 0) one could say: if (year % 100) etc. If-else

4 4 Syntax: if( expression ) statement1 else if( expression2 ) statement2 else if( expression3 ) statement3 … else statement If-else-if-else…

5 5 Example: If-else #include void main() { int low, high, i; scanf( “%d”, &low ); scanf( “%d”, &high ); for( i = low; i <= high; i++ ) { if( i % 3 == 0 ) printf(“%d is divisible by 3\n”,i); else if( i % 2 == 0 ) printf(“%d is divisible by 2, but not 3\n”, i); }

6 6 Dangling Else if (expr1) if (expr2) statement1 else statement2 Two possibilities:if (expr1) if (expr2) statement1else statement2 Usually else goes to closest if, but don’t count on it!

7 7 Syntax: switch( int-valued-exp ) { case const-expr: statements [break;] case const-expr: statements [break;] … default: statements } Switch & CaseDo {} While () do { statements } while (condition) Same as ‘while () {}’ except that the loop does run once before the condition is evaluated In ‘while’ the loop might not run at all if the condition is false at the beginning

8 8 Switch example switch( month ) /* month given as integer(1-12) */ { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf( “31 days.\n” ); break; case 2: printf( “28 days.\n” ); break; default; printf( “30 days.\n” ); }

9 9 Goto Really old and not very elegant way of writing code Can put labels as follows: Label: statement; Then with the statement goto label; somewhere in the code the program continues executing from the statement that “label” annotates.

10 10 break & continue while (…) {do { for (…) { ……… break;break;break; continue;continue;continue; ……… }}} Conclusion: Break: stops execution of loop and continues after it Continue: stops execution of the loop and lets it continue from the start again (as long as the condition remains true!)

11 11 Symbolic Constants #define LOWER 0 #define UPPER 100 #define Replaces each occurrence of with e.g. fahr = LOWER becomes fahr = 0 Can also define: #define SQ(X) X*X

12 12 Symbolic Constants #define REPL 5+1 #define SQ(X) X*X #include void main() { printf("%d\n", SQ(REPL)); } Output = 36?NO!It’s 11!!! Why? SQ(REPL) becomes REPL*REPL which is 5+1*5+1 Also do not put ; (e.g. #define MAX 200; )

13 13 Input / Output #include void main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } getchar & putchar: read and write one character respectively scanf(format, &variables, …) & printf(format, variables, …): read and write respectively (e.g. integers, floats, chars, strings, etc.)

14 14 More on Constants Character constants: ‘a’ ‘\0’ ‘\n’ –Arbitrary char in octal format: ‘\ooo’ (‘\040’) –Arbitrary char in hex format: ‘\xhh’ (‘\x20’) Integer constants: 12 -1 040 (octal) 0x20 (hex) –Append L for long & U for unsigned (e.g. 12UL) Floating point constants: 24.0 2.4e1 240.0e-1 –default = double –Append F for float and L for long double (e.g. 24.0l or 2.4e1f)

15 15 Some more operators &, |, ^, ~ : Bitwise AND, OR, XOR and complement (complement at full int size) –x = x | y (also x |= y ), or y = ~x etc. > shift left and shift right –x << n shifts the integer x left by n bits filling the rightmost bits with zeroes –x >> n shifts the integer x right by n bits filling the leftmost bits either with zeroes or the sign bit (this sometimes only for signed integers)

16 16 Assignment IF The statement: if (a>b) z=a else z=b can be also written as: z = (a>b) ? a : b In general the expression: if (cond) v=expr1 else v=expr2 can be also written: v = (cond) ? expr1 : expr2

17 17 To Read from K&R Covered so far (please read): –Chapter 1: 1.1-1.5 –Chapter 2: ALL (except 2.7) –Chapter 3: ALL


Download ppt "CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:"

Similar presentations


Ads by Google