Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Similar documents
C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Fundamental of Programming (C)

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Fundamentals of Programming

DEPARTMENT OF MATHS, MJ COLLEGE

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Expressions and Precedence. Last updated 12/10/18

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

C: How to Program. Week /Mar/05

Procedures, Parameters, Values and Variables. Steven R. Bagley

Operators. Java operators are classified into three categories:

Programming and Data Structures

Review of the C Programming Language for Principles of Operating Systems

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

A flow chart is a graphical or symbolic representation of a process.

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Variables and literals

Structured programming. Exercises 3

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

Lecture 3. More About C

Chapter 2 - Introduction to C Programming

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

OBJECT ORIENTED PROGRAMMING

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Review of the C Programming Language

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

UNIT- 3 Introduction to C++

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

6.096 Introduction to C++ January (IAP) 2009

ET156 Introduction to C Programming

Lecture 02 C FUNDAMENTALS

Information Science 1


C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Syntax and Variables

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

Part I Part 1 Expressions

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Part I Part 1 Expressions

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Fundamental of C programming. - Ompal Singh

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Operators in C. Staff Incharge: S.Sasirekha

Operators and expressions. (precedence and associability of operators, type conversions).

Flow Control. CSC215 Lecture

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Course Outline Introduction to C-Programming

Chapter 1 & 2 Introduction to C Language

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Variables. Data Types.

CSC 1214: Object-Oriented Programming

Basic Elements of C. Staff Incharge: S.Sasirekha

Java Notes. 10th ICSE. Saravanan Ganesh

Basics of Java Programming

C - Basic Introduction

Introduction to the C Programming Language

ET156 Introduction to C Programming

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

C Programming Class I

The Design of C: A Rational Reconstruction: Part 2

CpSc 1111 Lab 4 Formatting and Flow Control

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Continued from previous lecture

PROGRAMMAZIONE I A.A. 2018/2019

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

INTRODUCTION TO C-PROGRAMING LANGUAGE

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Work relative to other classes

Data types, variables, constants

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

Programming. C++ Basics

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

SECTION II: LANGUAGE BASICS

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

C LANGUAGE A Short Course

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

ANSI C Programming Simple Programs

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Transcription:

Lecture 02 Summary C/Java Syntax Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 1 2 By the end of this lecture, you will be able to identify the different parts of a C program. You will also be able to create a simple C program. What is a keyword? What are some of the keywords in Python? 3 4 1

Keywords in C (all 32 of them) auto double int struct break long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while Variable Declarations 5 6 In Python which lines are okay? 1var = 5 _pi = 3.1415 str+ing = hello while = 3.3 x = 0 mystr2 = I m a string C Variable Names Start with a letter or underscore Subsequent characters can also be numbers Can t use reserved keywords Case sensitive mystring is not the same as mystring 7 8 2

C Declarations Must specify the type of the variable (which will never change) In Python x = 5 # x starts out as an integer more code x = now I m a string 9 10 In C Data Types int x = 5; more code x = Noooooooooooo! Integral Types char short int long long long Floating Point Types float double 11 12 3

sizeof Operator Why isn t there just one int and one float type? main() printf( int: %d bytes\n", sizeof(int)); 13 14 In C which lines are okay? int 1var = 5 float _pi = 3.1415 short str+ing = hello double while = 3.3 char x = 0 int mystr2 = I m a string Operators 15 16 4

What are the operators? int x = 5; int y = x + 10; int z = (x + 20) / y; if (z < x) z = x * y; Operators in C Operator Description ++ -- postfix increment and decrement () function call [] array subscription -> element selection through pointer ++ -- prefix increment and decrement + - unary plus and minus! ~ logical not, bitwise not (type) type cast * indirection/dereferencing of pointer & address of sizeof get the size of element * / % multiplication, division, modulus 17 18 Operators in C (cont d) Operator Description + - addition and subtraction << >> bitwise shift left and right < <= > >= less than, less than equals, greater than, greater than equals ==!= equals, not equals & bitwise AND ^ bitwise XOR (exclusive-or) Bitwise OR && Logical AND Logical OR cond?t:f ternary command = += -= *= /= %= Assignment operators Increment and Decrement int i = 0; int j = 10; i = i + 1; i++; j = j - 1; j--; 19 20 5

What will this output? main() int x = 5; int y = 20; x++; y--; printf("x = %d\n", x); printf("y = %d\n", y); Prefix vs. Postfix Prefix = first thing that happens (before) e.g., ++i Postfix = last thing that happens (after) e.g., i++ 21 22 Prefix vs. Postfix Example What will this output? main() int x = 5; int y = 5; int a; int b; main() int i = 70; int j = 42; int a = i++ * ++j; a = x++; /* postfix */ b = ++y; /* prefix */ printf("a = %d\n", a); printf("b = %d\n", b); printf("x = %d\n", x); printf("y = %d\n", y); printf( i = %d\n", i); printf( j = %d\n, j); printf( a = %d\n, a); 23 24 6

In Python... Statements def farenheit_to_celcius(temp): return (temp - 32.0) * (5.0 / 9.0) 1 print "Enter the temperature in Farenheit:" 2 temp = input() if temp < -459.67: print "It can t possibly be that cold!" : tempincelcius = farenheit_to_celcius(temp) print "In Farenheit: %f" % temp print "In Celcius: %f" % tempincelcius 25 26 Enter 72 In C... def farenheit_to_celcius(temp): 5 return (temp - 32.0) * (5.0 / 9.0) 1 print "Enter the temperature in Farenheit:" 2 temp = input() 3 if temp < -459.67: print "It can t possibly be that cold!" : 4 tempincelcius = farenheit_to_celcius(temp) 6 print "In Farenheit: %f" % temp 7 print "In Celcius: %f" % tempincelcius Simple statements end with a semi-colon (;) Any white space is ignored by the computer spaces, tabs, new lines but is very helpful to people who read the code 27 28 7

if Statement Syntax Example if (<conditional>) <what-to-do-if-true>; <what-to-do-if-false>; main() int x = 3; if (x % 2 == 0) printf("x is even\n"); printf("x is odd\n"); 29 30 Example Example main() int x = 3; main() int x = 3; if (x % 2 == 0)printf( "x is even\n"); printf("x is odd\n"); if (x % 2 == 0) printf("x is even\n"); printf("x is odd\n"); 31 32 8

How would we write this in C? Would this work? if temp < -459.67: print "It can t possibly be that cold!" : tempincelcius = farenheit_to_celcius(temp) print "In Farenheit: %f" % temp print "In Celcius: %f" % tempincelcius if (temp < -459.67) printf("it can t possibly be that cold!\n"); tempincelcius = farenheit_to_celcius(temp); printf("in Farenheit: %f\n", temp); printf("in Celcius: %f", tempincelcius); 33 34 Compare it to this Blocks Statements can be grouped together into a compound statement by enclosing them in curly braces () if (temp < -459.67) printf("it can t possibly be that cold!\n"); tempincelcius = farenheit_to_celcius(temp); printf("in Farenheit: %f\n", temp); printf("in Celcius: %f", tempincelcius); 35 36 9

Adjusted if Statement Syntax if (<conditional>) <what-to-do-if-true>; <possibly>; <containing>; <multiple lines>; <what-to-do-if-false>; <also-more-lines>; How would we write this in C? if temp < -459.67: print "It can t possibly be that cold!" : tempincelcius = farenheit_to_celcius(temp) print "In Farenheit: %f" % temp print "In Celcius: %f" % tempincelcius 37 38 Answer if (temp < -459.67) printf("it can t possibly be that cold!\n"); tempincelcius = farenheit_to_celcius(temp); printf("in Farenheit: %f\n", temp); printf("in Celcius: %f", tempincelcius); Consider this code... if (x > 50) if (y > 200) z = x * y; printf("error!\n"); Which if does the belong to? 39 40 10

Good Practice if (x > 50) if (y > 200) z = x * y; printf ("error!\n"); if and switch Statements 41 42 What would this do? int x = 3; if (x == 1) printf("one!\n"); if (x == 2) printf("two!\n"); if (x == 3) printf("three!\n"); printf("not one, two, or three"); What would this do? int x = 3; if (x == 1) printf("one!\n"); if (x == 2) printf("two!\n"); if (x == 3) printf("three!\n"); printf("not one, two, or three"); 43 44 11

switch Statement Syntax Example switch (<variable>) case <value-one>: <code>; <code>; break; case <value-two>: <code>; <code>; break; default: <code>; break; 45 int x = 3; switch (x) case 1: printf("one!\n"); break; case 2: printf("two!\n"); break; case 3: printf("three!\n"); break; default: printf("invalid choice!\n"); break; 46 Example Factorial Loops n! = n (n-1) (n-2) 3 2 1 47 48 12

Complete this Python program print "Enter a number greater than zero:" n = input()... while Syntax while (<condition>) <statement> 49 50 What do you think this would do? main() int i = 0; Without the infinite loop main() int i = 0; while (i < 10) printf("i = %d\n", i); i++; while (i < 10) printf("i = %d\n", i); i++; 51 52 13

Complete this C Program do-while Syntax main() int n; printf("enter a number greater than zero: "); scanf("%d", n);... do <statement>; <statement2>; while (<condition>); 53 54 Compare for Syntax int x = 5; int x = 5; while (x > 0) printf("%d\n",x); x = x - 1; do printf("%d\n",x); x = x - 1; while (x > 0); for (<a>; <b>; <c>) <statements> <a> = Initialization of looping variable <b> = Condition <c> = Modification of looping variable 55 56 14

For Loops Example <a> while (<b>) <statements> <c> for (<a>; <b>; <c>;) <statements> int i; for (i = 0; i < 5; i++) printf("%d\n", i); 57 58 Complete this C Program main() int n; printf("enter a number greater than zero: "); scanf("%d", n); Functions... 59 60 15

Functions in C Must specify what type is returned if there is no return statement, must return void Must specify the type of each parameter Function Syntax <return-type> function_name(<parameters>) <statements> 61 62 Examples int add(int x, int y) return x + y; Remember from before def farenheit_to_celcius(temp): return (temp - 32.0) * (5.0 / 9.0) void print_int(int i) printf("%d\n", i); 63 64 16

What would it look like in C? def farenheit_to_celcius(temp): return (temp - 32.0) * (5.0 / 9.0) What would it look like in C? float farenheit_to_celcius(float temp) return (temp - 32.0) * (5.0 / 9.0); 65 66 Variants of main What is main? void main() int main() return 0; int main(int argc, char **argv) return 1; 67 68 17

Write this whole program in C Identify the different parts def farenheit_to_celcius(temp): return (temp - 32.0) * (5.0 / 9.0) print "Enter the temperature in Farenheit:" temp = input() if temp < -459.67: print "It can t possibly be that cold!" : tempincelcius = farenheit_to_celcius(temp) print "In Farenheit: %f" % temp print "In Celcius: %f" % tempincelcius int max(int a, int b) if (a > b) return a; return b; void main() int first; int second; int bigger; printf("enter a number: "); scanf("%d", &first); printf("enter another number: "); scanf("%d", &second); bigger = max(first, second); printf("the bigger number is: %d\n", bigger); 69 70 Keywords Variable Declarations int max(int a, int b) if (a > b) return a; return b; int max(int a, int b) if (a > b) return a; return b; void main() int first; int second; int bigger; void main() int first; int second; int bigger; printf("enter a number: "); scanf("%d", &first); printf("enter another number: "); scanf("%d", &second); printf("enter a number: "); scanf("%d", &first); printf("enter another number: "); scanf("%d", &second); bigger = max(first, second); printf("the bigger number is: %d\n", bigger); bigger = max(first, second); printf("the bigger number is: %d\n", bigger); 71 72 18

Data Types Operators int max(int a, int b) if (a > b) return a; return b; int max(int a, int b) if (a > b) return a; return b; void main() int first; int second; int bigger; void main() int first; int second; int bigger; printf("enter a number: "); scanf("%d", &first); printf("enter another number: "); scanf("%d", &second); printf("enter a number: "); scanf("%d", &first); printf("enter another number: "); scanf("%d", &second); bigger = max(first, second); printf("the bigger number is: %d\n", bigger); bigger = max(first, second); printf("the bigger number is: %d\n", bigger); 73 74 Statements Functions int max(int a, int b) if (a > b) return a; return b; int max(int a, int b) if (a > b) return a; return b; void main() int first; int second; int bigger; void main() int first; int second; int bigger; printf("enter a number: "); scanf("%d", &first); printf("enter another number: "); scanf("%d", &second); printf("enter a number: "); scanf("%d", &first); printf("enter another number: "); scanf("%d", &second); bigger = max(first, second); printf("the bigger number is: %d\n", bigger); bigger = max(first, second); printf("the bigger number is: %d\n", bigger); 75 76 19

Create a Program in C Input: three floating point numbers Output: the average of those three numbers Use: scanf to get the input printf to show the result a function to calculate the average Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 77 78 Next Class Arrays and Strings 79 20