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

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

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Fundamental of Programming (C)

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

C: How to Program. Week /Mar/05

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

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

Programming and Data Structures

Review of the C Programming Language for Principles of Operating Systems

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

Operators. Java operators are classified into three categories:

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

Variables and literals

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

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

Structured programming. Exercises 3

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

OBJECT ORIENTED PROGRAMMING

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

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

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

Review of the C Programming Language

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

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

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

6.096 Introduction to C++ January (IAP) 2009

UNIT- 3 Introduction to C++

Lecture 02 C FUNDAMENTALS

ET156 Introduction to C Programming


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

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

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

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

Syntax and Variables

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

Part I Part 1 Expressions

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

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

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

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

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

Information Science 1

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

Informatica e Sistemi in Tempo Reale

Part I Part 1 Expressions

Fundamentals of Programming

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

Flow Control. CSC215 Lecture

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

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

CSC 1214: Object-Oriented Programming

Variables. Data Types.

Fundamental of C programming. - Ompal Singh

Java Notes. 10th ICSE. Saravanan Ganesh

ET156 Introduction to C Programming

Basic Elements of C. Staff Incharge: S.Sasirekha

Introduction to the C Programming Language

Operators in C. Staff Incharge: S.Sasirekha

C - Basic Introduction

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

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

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

The Design of C: A Rational Reconstruction: Part 2

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

C Programming Class I

PROGRAMMAZIONE I A.A. 2018/2019

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Data types, variables, constants

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

INTRODUCTION TO C-PROGRAMING LANGUAGE

Basics of Java Programming

SECTION II: LANGUAGE BASICS

Programming. C++ Basics

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

Operators And Expressions

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

ANSI C Programming Simple Programs

CpSc 1111 Lab 4 Formatting and Flow Control

C LANGUAGE A Short Course

COMP 2355 Introduction to Systems Programming

Programming Language A

Introduction to Programming

A Fast Review of C Essentials Part I

EL2310 Scientific Programming

Transcription:

C/Java Syntax 1

Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 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. 3

What is a keyword? What are some of the keywords in Python? 4

Keywords in C (all 32 of them) auto double int struct break else 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 5

Variable Declarations 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 7

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 8

C Declarations Must specify the type of the variable (which will never change) 9

In Python x = 5 # x starts out as an integer more code x = now I m a string 10

In C int x = 5; more code x = Noooooooooooo! 11

Data Types Integral Types char short int long long long Floating Point Types float double 12

Why isn t there just one int and one float type? 13

sizeof Operator main() printf( int: %d bytes\n", sizeof(int)); 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 15

Operators 16

What are the operators? int x = 5; int y = x + 10; int z = (x + 20) / y; if (z < x) z = x * y; 17

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 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 19

Increment and Decrement int i = 0; int j = 10; i = i + 1; i++; j = j - 1; j--; 20

What will this output? main() int x = 5; int y = 20; x++; y--; printf("x = %d\n", x); printf("y = %d\n", y); 21

Prefix vs. Postfix Prefix = first thing that happens (before) e.g., ++i Postfix = last thing that happens (after) e.g., i++ 22

Prefix vs. Postfix Example main() int x = 5; int y = 5; int a; int b; 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); 23

What will this output? main() int i = 70; int j = 42; int a = i++ * ++j; printf( i = %d\n", i); printf( j = %d\n, j); printf( a = %d\n, a); 24

Statements 25

In Python... 1 2 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!" else: tempincelcius = farenheit_to_celcius(temp) print "In Farenheit: %f" % temp print "In Celcius: %f" % tempincelcius 26

Enter 72 1 2 def farenheit_to_celcius(temp): 5 return (temp - 32.0) * (5.0 / 9.0) print "Enter the temperature in Farenheit:" temp = input() 3 if temp < -459.67: print "It can t possibly be that cold!" else: 4 tempincelcius = farenheit_to_celcius(temp) 6 print "In Farenheit: %f" % temp 7 print "In Celcius: %f" % tempincelcius 27

In C... 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 28

if Statement Syntax if (<conditional>) <what-to-do-if-true>; else <what-to-do-if-false>; 29

Example main() int x = 3; if (x % 2 == 0) printf("x is even\n"); else printf("x is odd\n"); 30

Example main() int x = 3; if (x % 2 == 0) printf("x is even\n"); else printf("x is odd\n"); 31

Example main() int x = 3; if (x % 2 == 0)printf( "x is even\n"); else printf("x is odd\n"); 32

How would we write this in C? if temp < -459.67: print "It can t possibly be that cold!" else: tempincelcius = farenheit_to_celcius(temp) print "In Farenheit: %f" % temp print "In Celcius: %f" % tempincelcius 33

Would this work? if (temp < -459.67) printf("it can t possibly be that cold!\n"); else tempincelcius = farenheit_to_celcius(temp); printf("in Farenheit: %f\n", temp); printf("in Celcius: %f", tempincelcius); 34

Compare it to this if (temp < -459.67) printf("it can t possibly be that cold!\n"); else tempincelcius = farenheit_to_celcius(temp); printf("in Farenheit: %f\n", temp); printf("in Celcius: %f", tempincelcius); 35

Blocks Statements can be grouped together into a compound statement by enclosing them in curly braces () 36

Adjusted if Statement Syntax if (<conditional>) <what-to-do-if-true>; <possibly>; <containing>; <multiple lines>; else <what-to-do-if-false>; <also-more-lines>; 37

How would we write this in C? if temp < -459.67: print "It can t possibly be that cold!" else: tempincelcius = farenheit_to_celcius(temp) print "In Farenheit: %f" % temp print "In Celcius: %f" % tempincelcius 38

Answer if (temp < -459.67) printf("it can t possibly be that cold!\n"); else tempincelcius = farenheit_to_celcius(temp); printf("in Farenheit: %f\n", temp); printf("in Celcius: %f", tempincelcius); 39

Consider this code... if (x > 50) if (y > 200) z = x * y; else printf("error!\n"); Which if does the else belong to? 40

Good Practice if (x > 50) if (y > 200) z = x * y; else printf ("error!\n"); 41

else if and switch Statements 42

What would this do? int x = 3; if (x == 1) printf("one!\n"); else if (x == 2) printf("two!\n"); else if (x == 3) printf("three!\n"); else printf("not one, two, or three"); 43

What would this do? int x = 3; if (x == 1) printf("one!\n"); else if (x == 2) printf("two!\n"); else if (x == 3) printf("three!\n"); else printf("not one, two, or three"); 44

switch Statement Syntax switch (<variable>) case <value-one>: <code>; <code>; break; case <value-two>: <code>; <code>; break; default: <code>; break; 45

Example 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

Loops 47

Example Factorial n! = n (n-1) (n-2) 3 2 1 48

Complete this Python program print "Enter a number greater than zero:" n = input()... 49

while Syntax while (<condition>) <statement> 50

What do you think this would do? main() int i = 0; while (i < 10) printf("i = %d\n", i); i++; 51

Without the infinite loop main() int i = 0; while (i < 10) printf("i = %d\n", i); i++; 52

Complete this C Program main() int n; printf("enter a number greater than zero: "); scanf("%d", n);... 53

do-while Syntax do <statement>; <statement2>; while (<condition>); 54

Compare 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); 55

for Syntax for (<a>; <b>; <c>) <statements> <a> = Initialization of looping variable <b> = Condition <c> = Modification of looping variable 56

For Loops <a> while (<b>) <statements> <c> for (<a>; <b>; <c>;) <statements> 57

Example int i; for (i = 0; i < 5; i++) printf("%d\n", i); 58

Complete this C Program main() int n; printf("enter a number greater than zero: "); scanf("%d", n);... 59

Functions 60

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 61

Function Syntax <return-type> function_name(<parameters>) <statements> 62

Examples int add(int x, int y) return x + y; void print_int(int i) printf("%d\n", i); 63

Remember from before def farenheit_to_celcius(temp): return (temp - 32.0) * (5.0 / 9.0) 64

What would it look like in C? def farenheit_to_celcius(temp): return (temp - 32.0) * (5.0 / 9.0) 65

What would it look like in C? float farenheit_to_celcius(float temp) return (temp - 32.0) * (5.0 / 9.0); 66

What is main? 67

Variants of main void main() int main() return 0; int main(int argc, char **argv) return 1; 68

Write this whole program in C 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!" else: tempincelcius = farenheit_to_celcius(temp) print "In Farenheit: %f" % temp print "In Celcius: %f" % tempincelcius 69

Identify the different parts int max(int a, int b) if (a > b) return a; else 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); 70

Keywords int max(int a, int b) if (a > b) return a; else 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); 71

Variable Declarations int max(int a, int b) if (a > b) return a; else 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); 72

Data Types int max(int a, int b) if (a > b) return a; else 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); 73

Operators int max(int a, int b) if (a > b) return a; else 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); 74

Statements int max(int a, int b) if (a > b) return a; else 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); 75

Functions int max(int a, int b) if (a > b) return a; else 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); 76

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 77

Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 78

Next Class Arrays and Strings 79