COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez

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

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Beginning C for Engineers Fall 2005 Lecture 2 – Section 2 (9/7/05) Section 4 (9/8/05)
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Chapter 4 Selection Structures: Making Decisions.
Flow of Control Part 1: Selection
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
CSC 107 – Programming For Science. The Week’s Goal.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Lecture2.
CprE 185: Intro to Problem Solving (using C)
Chapter 4 (Conditional Statements)
The C “switch” Statement
Introduction to Programming
The C “switch” Statement
Chapter 2 - Introduction to C Programming
C Programming Variables.
Chapter 2 - Introduction to C Programming
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
Conditions and Boolean Expressions
Chapter 2 - Introduction to C Programming
Lecture3.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
EECE.2160 ECE Application Programming
Control Structures Lecture 6.
Chapter 2 - Introduction to C Programming
The switch Statement Topics Multiple Selection switch Statement
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 13 Control Structures
Presentation transcript:

COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez

ADMINISTRATIVE STUFF Not so new rule: whenever you participate tell me your name! Reminder: First quiz will be on Friday (5/22). It will cover everything we’ve covered until today! (Just what is actually covered in class, extra slides not necessary) Do we have any runners in the class? This includes: Treadmill Outside Long distance Short distance But no one who uses the ‘j’ word

HOMEWORK #2 Pace calculator: Inputs: Race distance (char) (a. 5k, b. 10k, c. 15k, d. Half marathon, e. Full marathon). Distance unit (char) (k for km, m for miles) Expected finish time (3 ints – hours, minutes, seconds) (hh:mm:ss) Output Constant pace required to finish that race in that time (using the selected unit) Due date: Monday June 1 st (11:59pm) Distance conversions and expected format detailed on the homework specification By Friday, we will have all the knowledge required to code this program

Aprox 10k

ASSIGNMENT AND OPERATIONS Sometimes we want to do a quick operation on a variable and save the result in the variable itself. For example: int a = 8; a = a * 2; // get the double of 8. A quick shortcut for this is: a *= 2; //same as a = a * 2; This also works for other operators!

ASSIGNMENT AND OPERATIONS An even more specific common operation that has it’s own operand is: Adding or subtracting 1 from a variable! For example: int a = 8; a = a + 1; // adding 1 Of course we could do: a += 1; // adding 1 However an even easier way is a++; // adding 1 Same story for subtracting 1 (--).

MAKING DECISIONS Sometimes we need to make decisions based on inputs and provide different output based on the decision. Examples: Odd/Even program Which one is the larger of two numbers. Letter grade based on numeric grade Others? Homework #2! Decision making in C: if statement switch statement Conditional operator

IF STATEMENT if( ) When is true, is executed. What does looks like? We have relational operators! OperatorMeaningExample ==Equal tocount == 10 !=Not equal toa != b <Less thani < 0 <=Less than or equal toi <= 100 >Greater thanpointer > end >=Greater than or equal toj >= 0

#include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 == 0) printf("The number is even.\n"); return 0; }

#include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 != 0) printf("The number is odd.\n"); return 0; }

#include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 == 0) printf("The number is even.\n"); if(num % 2 != 0) printf("The number is odd.\n"); return 0; }

ELSE if( ) else Whenever the is true is executed Whenever the is false is executed

#include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 == 0) printf("The number is even.\n"); else // much more efficient than if(num % 2 != 0) printf("The number is odd.\n"); return 0; }

#include int main(void) { int num = -1; printf("Input a number"); scanf("%d", &num); if(num % 2 == 0) { printf("The number is even.\n"); } else { printf("The number is odd.\n"); } return 0; }

#include int main(void) { char a = '\0'; char b = '\0'; printf("Input your first letter"); scanf("%c", &a); printf("Input your second letter"); scanf("%c", &b); if(a < b) { printf("%c comes before %c.\n", a,b); } else if(a == b) { printf("You input %c twice!.\n", a); } else { printf("%c comes before %c.\n", b,a); } return 0; } Why doesn’t this work??

(SIDENOTE) GOING DEEPER INTO SCANF Spoiler alert : If you miss this part in class you will likely struggle with HW2. Why didn’t our example work? Scanf doesn’t ignore the newline character that pressing “Enter” generates. The second scanf takes that newline character, so how do we ignore it? How to fix it. There is a function called getchar(). getchar() consumes one character from the console. That includes the newline character!

#include int main(void) { char a = '\0'; char b = '\0'; printf("Input your first letter"); scanf("%c", &a); getchar(); printf("Input your second letter"); scanf("%c", &b); getchar(); if(a < b) { printf("%c comes before %c.\n", a,b); } else if(a == b) { printf("You input %c twice!.\n", a); } else { printf("%c comes before %c.\n", b,a); } return 0; }

GOING DEEPER INTO SCANF Remember how I said we wouldn’t need to read more than one input per line using scanf ? I lied… or at least I didn’t expect my HW idea to require it. Expected finish time (3 ints – hours, minutes, seconds) (hh:mm:ss) So, how do we do that? Read two or more variables in a particular format in one scanf ? We add more characters in the string and more parameters! (very similar to how we print more than one variable using printf )

#include int main(void) { char a = '\0'; char b = '\0'; printf("Input two letters separated by a hyphen: "); scanf("%c-%c", &a, &b); getchar(); //printf("Input your second letter"); // scanf("%c", &b); getchar(); if(a < b) { printf("%c comes before %c.\n", a,b); } else if(a == b) { printf("You input %c twice!.\n", a); } else { printf("%c comes before %c.\n", b,a); } return 0; }

ADMINISTRATIVE STUFF We have the first Quiz today! (5/22) It will 11:40am. You are free to leave as soon as you are done. This Monday (5/25) is Memorial Day! UF Holiday! No class Second Quiz will still be (5/29)

(BACK TO CONDITIONALS) COMPOUND RELATIONAL TESTS Sometimes we want to check more than just one condition. We already have some of that with = (greater than or equal)

COMPOUND RELATIONAL TESTS Sometimes we want to check more than just one condition. We already have some of that with = (greater than or equal) How can we test if a number is in a range? For example if a float grade is between 92 and 88 and is therefore a B+. (Such an strict instructor!) We use the logical AND (&&) and logical OR(||) operators. if( grade >= 88 && grade < 92) printf("You got a B+\n");

NESTED IFS Remember an if statement looks like this: if( ) Just happens that the if statement is also a program statement. So we can have things that look like this: if( ) This is what we call a nested if! An if statement inside another one.

NESTED IFS When? Why? Let’s go back to: if(grade >= 88 && grade < 92) printf("You got a B+\n"); We could change it for: if(grade >= 88) if(grade < 92) printf("You got a B+\n"); Same result, the printf only happens if grade is both greater than or equal to 88 but less than 92.

NESTED IFS if(grade >= 88) if(grade < 92) printf("You got a B+\n"); What happens if I want to do something else? Like assign an A- or A? if(grade >= 88) if(grade < 92) printf("You got a B+\n"); else printf(“Either an A- or A, right?\n”);

NESTED IFS Curly brackets/parenthesis are your friends. Use them! The previous ifs are the same as: if(grade >= 88) { if(grade < 92) { printf("You got a B+\n"); } else { printf(“Either an A- or A, for sure!\n”); } }else But just in case, know how C works if they are not there. [Confusing brackets will never be a part of my quizzes]

SWITCH STATEMENTS What happens when the variable of interest can take multiple values and we need to do something different for each one? For example, let’s take a simple two operand calculator.

#include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //do the calculations here… printf("The result is: %f\n“, result); return 0; }

#include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: if(operator == '+') { result = operand1 + operand2; } else if(operator == '-') { result = operand1 + operand2; } else if(operator == '*') { result = operand1 * operand2; } else if(operator == '/') { result = operand1 / operand2; } printf("The result is: %f\n“, result); return 0; }

SWITCH STATEMENTS switch( ) { case : break; case : break; … case : break; default: }

#include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; } printf("The result is: %f\n“, result); return 0; }

#include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: printf(“Unknown operator %c\n“, operator); } printf("The result is: %f\n“, result); return 0; }

#include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': case 'x': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: printf(“Unknown operator %c\n“, operator); } printf("The result is: %f\n“, result); return 0; }

#include int main(void) { float operand1 = 0.0f, operand2 = 0.0f, result = 0.0f; char operator = '\0'; printf("Input the operation you want to evaluate: "); scanf(“%f %c %f", &operand1, &operator, &operand2); //with what we know, we could do: switch(operator) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case 'x': printf(“Warning the recommended operator is * not x\n“); case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default: printf(“Unknown operator %c\n“, operator); } printf("The result is: %f\n“, result); return 0; } No break;

CONDITIONAL OPERATOR ? : ; This is useful to assign a variable a value for example: float result = operator == ‘+’? operand1 + operand2 : operand1 – operand2; float result = (operator == ‘+’)?(operand1 + operand2) : (operand1 – operand2) ;