Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.

Similar presentations


Presentation on theme: "CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004."— Presentation transcript:

1 CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004

2 Arithmetic Arithmetic calculations Use + (addition) - (subtraction) * (multiplication) and / (division) Integer division truncates remainder 7 / 5 evaluates to 1 Modulus operator(%) returns the remainder 7 % 5 evaluates to 2 Operator precedence Some arithmetic operators act before others (i.e., multiplication before addition) - use parenthesis when needed. Example: Find the average of three variables a, b and c Do not use: a + b + c / 3 Use: (a + b + c ) / 3

3 The if statement The Selection structure is used to choose among alternative courses of action Example: If student’s grade is greater than or equal to 60 then Print “Passed” Algorithm: If condition true Print statement executed and program goes on to next statement. If false, print statement is ignored and the program goes onto the next statement. Code: if (grade >= 60) printf("Passed\n");

4 The else clause The if selection statement may have an “else” part. Example: If student’s grade is greater than or equal to 60 then Print “Passed” else Print “Failed.” Here is the C code. Note use of relational operator (>=): if (grade >= 60) printf("Passed\n"); else printf("Failed\n"); Statement could have been written (using ?: notation): grade >= 60 ? printf(“Passed\n”): printf(“Failed\n”);

5 The else clause (continued) You can have a set of statements within a pair of braces, example: if (grade >= 60) printf("Passed.\n"); else{ printf("Failed.\n"); printf("You must take this course again.\n"); } The second printf is part of the else clause. Note also the use of the relational operator >= (less than or equal to)

6 Booleans The if statement above contains a Boolean expression. This is an expression with two values that eveluates to either true or false. if (x < 4) printf(“x is less than 4”); You can also declare Boolean variables and give them an initial value. bool myBoolean = false; if (myBoolean) printf(“This statement is never displayed”); Booleans named after George Boole, First Professor of Mathematics, U.C.C.

7 Relational Operators

8 C Characters Enclosing a character in single quotes, e.g. ‘H’, represents a printable character constant. Internally, characters are represented numerically, usually by their ASCII codes. For example, the ASCII code for ‘A’ is 65. Example: Here we declare a character variable answer with the initial value ‘y’ (for yes) and change it in the couse of the program to ‘n’ (for no). char answer = ‘y’; /* set as yes */... answer = ‘n’; /* changed to no */ Non-printable characters are represented using the escape sequence backslash ( \ ). The same convention is used to represent the characters \ and ‘ which would otherwise cause confusion, i.e. \\ and \’.

9 Special Characters C has special characters that need to be “escaped”: new-line \n horizontal tabulation \t vertical tabulation \v backspace \b carriage return \r bell \a backslash \\ question mark \? single quote \’ double quote \” Here is an example within a printf statement: printf(“\tThis is \”layed\” out nicely\n”);

10 Reading in data Before we can write useful programs we need to be able to read in data from the keyboard. A scanf() performs a similar function to printf except its for (formatted) input. Note the use of the address-of operator (&) in the argument list. Here’s an example where we read in an integer and a character into variables (and then display them). int anInt; char aChar; printf(“Enter one character and one integer:”); scanf(“%c %d”, &aChar, &anInt); printf(“The value of the integer is %d and the value of the character is %c\n”, anInt, aChar);

11 Reading in data (continued) The scanf function uses standard input (usually keyboard). Here is a simple example: int int_var; scanf(“%d”,%int_var); This scanf statement has two arguments %d - indicates data should be a decimal integer &int_var - location in memory to store variable The user responds to this scanf statement by typing in a number, and then pressing the enter (return) key.


Download ppt "CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004."

Similar presentations


Ads by Google