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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Loops – While, Do, For Repetition Statements Introduction to Arrays
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
1 Chapter Four Boolean Expressions and Control Statements.
Introduction to C Language
C Programming A Modern Approach
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
Conditional Statement
Outline Symbolic Constants Character Input and Output if … else switch…case.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Flow of Control Part 1: Selection
Computers and Programming CPC The 2 nd lecture Jiří Šebesta.
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
IIT Kanpur C Course Lecture 3 Aug 31, Rishi Kumar, Final year BT-MT, CSE.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
G. Pullaiah College of Engineering and Technology
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Chapter 4 – C Program Control
ECE Application Programming
Chap. 2. Types, Operators, and Expressions
Week 3 - Friday CS222.
Administrative things
Introduction to C CSE 2031 Fall /3/ :33 AM.
Lecture 13 & 14.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Introduction to C Programming
Arithmetic operations, decisions and looping
- Additional C Statements
CS1100 Computational Engineering
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Relational, Logical, and Equality Operators
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Dale Roberts, Lecturer IUPUI
Introduction to C EECS May 2019.
Flow of Control.
CSC215 Lecture Control Flow.
Presentation transcript:

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

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 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 Syntax: if( expression ) statement1 else if( expression2 ) statement2 else if( expression3 ) statement3 … else statement If-else-if-else…

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

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 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 To Read from K&R Covered so far (please read): –Chapter 1: –Chapter 2: ALL (except 2.7) –Chapter 3: ALL