CSC 107 – Programming For Science. Announcements  Tutors available MTWR in WTC206/WTC208  Special lab (with Macs) & not in the Tutoring Center  Can.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
The If/Else Statement, Boolean Flags, and Menus Page 180
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Spacing in a Program  C++ ignores spaces in a program  This also means where newlines placed ignored  #define &
CPS120: Introduction to Computer Science Decision Making in Programs.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Problem of the Day  Why are manhole covers round?
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CONTROLLING PROGRAM FLOW
CSC 107 – Programming For Science. Announcements.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  While at Bell Labs, created language to develop Unix 
CSC 107 – Programming For Science. Announcement Today’s Goal  Know how to write selections besides if-else  Why we use select or if - else and how.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CSC 107 – Programming For Science. Announcements.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CSC 107 – Programming For Science. Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CPS120: Introduction to Computer Science Decision Making in Programs.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
CPS120: Introduction to Computer Science Decision Making in Programs.
CSC 107 – Programming For Science. Final Exams Dec. 16, 8AM – 10AM in OM221  Exam for CSC107: Dec. 16, 8AM – 10AM in OM221  Will be done using paper.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Branching statements.
Chapter 3 Selection Statements
EGR 2261 Unit 4 Control Structures I: Selection
CSC113: Computer Programming (Theory = 03, Lab = 01)
Conditionals & Boolean Expressions
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Life is Full of Alternatives
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

CSC 107 – Programming For Science

Announcements  Tutors available MTWR in WTC206/WTC208  Special lab (with Macs) & not in the Tutoring Center  Can answer questions you have about material  Also very good at helping with weekly assignments  Directly across from my office, so I can also help  My office hours are minimum time I am around  During the day, often (usually) around at other times  Please ask questions and get any help you need  No RSVPs; could also take candy I have in my office

Today’s Goals  Be able to use if – else if – else statements  Know how to order choices to work properly  Explain why if & else if conditions not related  Correctly use braces ( { } ) and make code pretty  Explain when & why if – else if – else used

if (…) statement  1 st evaluates boolean expression in parenthesis  Executes next statement if expression is true  When expression is false, skips over the statement int num = -32; if (num > val; if ((val < num) || (val <= 0)) { cout << "That was a poor choice“ << endl; }

if (…) statement

else statement  Often want to perform one of two actions  Execute some statements when condition is true  But when it is false, do some other work  Continue as normal afterward, no matter the path ifelse  In these cases, can use if-else statement must  else must come immediately after if's ; or }  Like if, else should always have {}  Cannot have else without if to start process  In C++, there is no problem having if & no else false true

if - else Examples if (value > 0) { cout << "Its positive" << endl; } else { value = abs(value); } if ((year % 4 == 0) && ((year % 100) != 0)) { daysInYear = 366; cout << "Not quite right, but close enough"; cout << endl; leapYear = true; } else { leapYear = false; daysInYear = 365; }

Many Choices  May want to choose from many possibilities

Many Choices  May want to choose from many possibilities  Could nest if – else statements inside one another if (a > 3) { cout 10) { cout << "b greater than 10" << endl; } else { if (c == -23) { cout << "c is equal to -23" << endl; } else { cout << "This really sucks" << endl; cout << "Sorry its stupid" << endl; } } }

Many Choices  May want to choose from many possibilities  Could nest if – else statements inside one another else if  Or easier & nicer if – else if – else statement if (a > 3) { cout 10) { cout << "b greater than 10" << endl; } else if (c == -23) { cout << "c is equal to -23" << endl; } else { cout << "This really sucks" << endl; cout << "Sorry its stupid" << endl; }

if – else if – else Usage

if – else if – else Example if (wealth > ) { cout ) { cout ) { cout 10000) { cout 0) { cout << "Good luck" << endl; }

if – else if – else Example if (wealth > ) { cout ) { cout ) { cout 10000) { cout 0) { cout << "Good luck" << endl; }

if – else if – else Example if (wealth > ) { cout ) { cout ) { cout 10000) { cout 0) { cout << "Good luck" << endl; } else { cout << "Government bailout!" << endl; }

if – else if – else Example if (wealth > ) { cout ) { cout ) { cout 10000) { cout 0) { cout << "Good luck" << endl; } else { cout << "Government bailout!" << endl; }

if – else if – else Example if (wealth > ) { cout ) { cout 10000) { cout 0) { cout ) { cout << "File for bankruptcy" << endl; } else { cout << "Government bailout!" << endl; }

Spacing in a Program  C++ ignores spaces in a program  This also means where newlines placed ignored  #define & text in quotes major exceptions to rule  This can lead to interesting code choices

Coding Style  Use consistent size to indent (I use 2 spaces)  Not required, but makes it much easier to read code  Indent code within braces ( {} ) to show structure  If line could or does contain { add to indentation  Decrease indentation each } should be on a line  if, else if, or else line has opening brace  Always use braces to prevent having to fix bugs  Save space: closing brace on else/else if line  Closing brace on own line at end of the statement

Coding Style Example #include using namespace std; int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; } } cout << "And now code continues" << endl; return 0; }

Coding Style Example #include using namespace std; int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; } } cout << "And now code continues" << endl; return 0; }

Coding Style Example #include using namespace std; int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; } } cout << "And now code continues" << endl; return 0; }

Coding Style Example #include using namespace std; int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; } } cout << "And now code continues" << endl; return 0; }

Coding Style Example #include using namespace std; int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; } } cout << "And now code continues" << endl; return 0; }

Coding Style Example #include using namespace std; int main() { int dcScore = 22, azScore = 21; if (dcScore > azScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He gives out Fs"; dcScore += 100; if (dcScore < azScore) { cout << "Ouch. That's Buffalo bad." <<endl; } } cout << "And now code continues" << endl; return 0; }

Your Turn  Get in groups of 3 & work on following activity

For Next Lecture  Read sections 7.4, 7.6 – 7.7 for Friday  What if we want to run multiple clauses?  Is something easier for testing lots of equality?  How can we break lots of things?  Week #3 weekly assignment due yesterday  But could still submit today by using virtual extension  Week #4 weekly assignment also on Angel  If problem takes more than 10 minutes, TALK TO ME!