Week 4 - Monday.  What did we talk about last time?  Precedence  Selection statements  Loops  Lab 3.

Slides:



Advertisements
Similar presentations
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Advertisements

CSCC69: Operating Systems
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Fundamentals of C and C++ Programming Control Structures and Functions.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Week 1 - Friday.  What did we talk about last time?  C basics  Data types  Output with printf()
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
CPS120 Introduction to Computer Science Iteration (Looping)
Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
1 Homework / Exam Turn in HW3 today Exam 1 next class –Open Book / Open Notes –Recommended Use of Book / Notes in Exam: Avoids reliance on “rote memorization”
CPS120: Introduction to Computer Science Decision Making in Programs.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
CPS120 Introduction to Computer Science Iteration (Looping)
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
1 ICS103 Programming in C Lecture 8: Functions I.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Week 5 - Monday.  What did we talk about last time?  Processes  Lab 4.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Discussion 4 eecs 183 Hannah Westra.
Week 4 - Wednesday CS222.
Function Topic 4.
Week 3 - Friday CS222.
Introduction To Repetition The for loop
Testing and Debugging.
Week 4 - Monday CS222.
Programmazione I a.a. 2017/2018.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Switch Statements, Do While, Break, Continue, Goto, Comma Operator
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CSC215 Lecture Control Flow.
CPS120: Introduction to Computer Science
Homework Any Questions?.
C Programming Getting started Variables Basic C operators Conditionals
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
CSC215 Lecture Control Flow.
Controlling Program Flow
CMSC 202 Exceptions.
Programming Language  C Control Flow
Review.
Week 2 - Friday CS222.
Presentation transcript:

Week 4 - Monday

 What did we talk about last time?  Precedence  Selection statements  Loops  Lab 3

Unix was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things. Doug Gwyn Unix was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things. Doug Gwyn

 The break command is a necessary part of the functioning of a switch statement  But, it can also be used to jump out of a loop  Whenever possible (i.e. always), it should not be used to jump out of a loop  Everyone once in a while, it can make things a little clearer, but usually not  Loops should have one entry point and one exit for(value = 3; value < 1000; value += 2) { … if( !isPrime(value) ) break; } for(value = 3; value < 1000; value += 2) { … if( !isPrime(value) ) break; } for(value = 3; value < 1000 && isPrime(value); value += 2) { … } for(value = 3; value < 1000 && isPrime(value); value += 2) { … }

 The continue command is similar to the break command  It will cause execution to jump to the bottom of the loop  If it is a for loop, it will execute the increment  For all loops, it will return to the top if the condition is true  It makes things easier for the programmer up front, but the code becomes harder to follow  The effect can be simulated with careful use of if statements

 A goto command jumps immediately to the named label  Unlike break and continue, it is not a legal command in Java  Except in cases of extreme (EXTREME) performance tuning, it should never be used  Spaghetti code results for(value = 3; value < 1000; value += 2) { if( !isPrime(value) ) goto stop; } printf("Loop exited normally.\n"); stop: printf("Program is done.\n"); for(value = 3; value < 1000; value += 2) { if( !isPrime(value) ) goto stop; } printf("Loop exited normally.\n"); stop: printf("Program is done.\n");

 A system call is a way to ask the kernel to do something  Since a lot of interesting things can only be done by the kernel, system calls must be provided to programmers via an API  When making a system call, the processor changes from user mode to kernel mode  There is a fixed number of system calls defined for a given system

 The most common implementation of the Standard C Library is the GNU C Library or glibc  Some of the functions in the glibc perform systems calls and some do not  There are slight differences between the versions of the glibc  Microsoft also has an implementation of the Standard C Library that doesn't always behave the same

 There are no exceptions in C  Instead, when a system call fails, it usually returns -1  To find out why the system call failed  First, make sure you #include  Then check the value of the integer errno in your program after the system call fails  Use the man pages to determine what a given value of errno means  The perror() function is often used to print errors instead of printf()  It sends the output to stderr instead of stdout

#include int main(){ int fd = open("eggplant.txt", O_WRONLY | O_CREAT | O_EXCL); if (fd == -1) { perror("Failure to create file: "); if( errno == EACCES ) perror("Insufficient privileges\n"); else if( errno == EEXIST ) perror("File already exists\n"); else perror("Unknown error\n"); exit(EXIT_FAILURE); } return 0; } #include int main(){ int fd = open("eggplant.txt", O_WRONLY | O_CREAT | O_EXCL); if (fd == -1) { perror("Failure to create file: "); if( errno == EACCES ) perror("Insufficient privileges\n"); else if( errno == EEXIST ) perror("File already exists\n"); else perror("Unknown error\n"); exit(EXIT_FAILURE); } return 0; }

 C has a feature called typedef which allows a user to give a new name to a type  System types are often created so that code is portable across different systems  The most common example is size_t, which is the type that specifies length  It's usually the same as unsigned int  There are named types for process IDs ( pid_t ), group IDs ( gid_t ), user IDs ( uid_t ), time ( time_t ), and many others

type name( arguments ) { statements }

 You don't have to specify a return type  But you should  int will be assumed if you don't  If you start calling a function before it has been defined, it will assume it has return type int and won't bother checking its parameters

 Because the C language is older, its compiler processes source code in a simpler way  It does no reasonable typechecking if a function is called before it is defined  To have appropriate typechecking for functions, create a prototype for it  Prototypes are like declarations for functions  They usually come in a block at the top of your source file

 Parameter names in the prototype are optional (and don't have to match)  Both of the following work: int root(int); int root(int blah);  You can also declare a prototype locally (inside a function), but there isn't a good reason to do so #include int root(int value); //integer square root int main() { int output = root(19); printf("Value: %d\n", output); return 0; } int root(int value) { int i = 0; while( i*i <= value ) i++; return i – 1; } #include int root(int value); //integer square root int main() { int output = root(19); printf("Value: %d\n", output); return 0; } int root(int value) { int i = 0; while( i*i <= value ) i++; return i – 1; }

 If your method takes nothing, you should put void in the argument list of the prototype  Otherwise, type checking is turned off for the arguments double stuff(); int main() { double output = stuff(6.4, "bang"); //legal return 0; } double stuff(); int main() { double output = stuff(6.4, "bang"); //legal return 0; } double stuff(void); int main() { double output = stuff(6.4, "bang"); //error return 0; } double stuff(void); int main() { double output = stuff(6.4, "bang"); //error return 0; }

 C does not force you to return a value in all cases  The compiler may warn you, but it isn't an error  Your function can "fall off the end"  Sometimes it works, other times you get garbage int sum(int a, int b) { int result = a + b; return result; } int sum(int a, int b) { int result = a + b; return result; } int sum(int a, int b) { int result = a + b; } int sum(int a, int b) { int result = a + b; }

 Let's write a function that:  Takes an unsigned integer as a parameter  Returns the location of the highest 1 bit in the integer (0-31) or -1 if the integer is 0 ParameterReturn Value

 More on functions  Variable scope

 Keep reading K&R chapter 4  Start working on Project 2  Get your teams sorted out by the end of the day!