CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 5: Control Structures II (Repetition)
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 4 Making Decisions
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CprE 185: Intro to Problem Solving (using C)
ECE Application Programming
ECE Application Programming
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
Programming Languages and Paradigms
CS1010 Discussion Group 11 Week 9 – Pointers.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 5: Control Structures II
Primitive Data, Variables, Loops (Maybe)
Functions.
CS1010 Discussion Group 11 Week 6 – One dimensional arrays.
Lecture 13 & 14.
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
Programming Paradigms
Functions, variables, operators, loops Modularity
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Control Structures – Selection
Variable Declarations, Data types, Expressions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
The University of Texas – Pan American
Functions I Creating a programming with small logical units of code.
CS1100 Computational Engineering
Selection CSCE 121 J. Michael Moore.
Functions, Part 1 of 3 Topics Using Predefined Functions
Conditions and Boolean Expressions
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Building Java Programs
Relational, Logical, and Equality Operators
CISC124 Labs start this week in JEFF 155. Fall 2018
C Programming Getting started Variables Basic C operators Conditionals
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Functions, Part 1 of 3 Topics Using Predefined Functions
Fundamental Programming
Life is Full of Alternatives
Programming Languages and Paradigms
CprE 185: Intro to Problem Solving (using C)
An Overview of C.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Functions.
Presentation transcript:

CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition

Slides are at http://www.comp.nus.edu.sg/~yanhwa/ HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/

Have you submitted your Lab #2? Do Read qn carefully, plan Give meaningful names for variables (no t1, t2, a, b, c…) Appropriate commenting Need to use functions Add spaces, empty lines appropriately Balance the number of vars created Must do Double/triple check your indentation (gg=G in vim will auto-indent your program nicely!) Add function description, preconditions and postconditions (if any) Do not Add function description elsewhere other than on the top of the function http://www.comp.nus.edu.sg/~cs1010/labs/2017s1/labguide.html Follow lab guidelines

Some tips If else Shorthand notation (Ternary Logic) return (answer1 > answer2 ? answer1 : answer2); Padding of zeros printf("%06d", zipCode);

.bashrc and .vimrc

Number, order, type of parameters Function description Lecture Summary Functions recap Function prototypes Parameter names may be omitted in the prototype, but not their type Number, order, type of parameters Function description Precondition, postcondition A function should perform either computation or I/O, not both.

Lecture Summary warning: implicit declaration of function 'f‘ warning: conflicting types for 'f' 1 #include <stdio.h> 2 3 int main(void) { 4 f(100, 7); 5 return 0; 6 } 7 8 void f(int a, int b) { 9 return a*b*b; 10 } Why will this happen? Without function prototype, compiler assumes function f to be an int function when it encounters f(100, 7); in main(). But it later conflicts with void f() A ‘type-less’ function has default return type of int

Lecture Summary What is pass-by-value?

Short-circuit evaluation (if A && B) (if A || B) Lecture Summary (5<2) + (6==5) A && B A || B !A == vs = Short-circuit evaluation (if A && B) (if A || B) Boolean flags “is…” “has…” Switch statement

Switch statement Lecture Summary switch ( <variable or expression> ) { case value1: Code to execute if <variable or expr> == value1 break; case value2: Code to execute if <variable or expr> == value2 case value3: Code to execute if <variable or expr> == value3 ... default: Code to execute if <variable or expr> does not equal to the value of any of the cases above }

Lab CodeCrunch Lab #2 Have you submitted?

while ( condition ) { // loop body } Repeat if a condition is true Lecture Summary Repeat if a condition is true while ( condition ) { // loop body }

Repeat for a certain number of times Lecture Summary Repeat for a certain number of times Steps: n=1; if (n<=10) { printf(…); n++; Go to step 2 } Exit the loop int n; for (n=1; n<=10; n++) { printf("%3d", n); } Same as “for n  1 to 10 STEP 1”

Square-free integer? What’s that? Lab CodeCrunch Lab #3 Square-free integer? What’s that?

Tutorial 3 Q1a The expression is evaluated from left to right: (0 < value), being a relational operation (also called comparison operation), evaluates to True when value = 0.5 The hint will print out 1 because the return value for true is 1 in C

Exploration What is a string? %s What is a char? %c max = ?

Tutorial 3 Q6 #define FEMALE 0 #define MALE 1 #define ACCEPTABLE 0 …. int body_type(int gender, double wt, double ht) {

Tutorial 2 Q7 Modularity: Reuse of codes, top-down design, ease of debugging, ease of editing function without affecting other parts of the program Do not mix input/output with computation

Tutorial 2 Q9 Confusing local scope:

Functions and call stack https://www.youtube.com/watch?v=jRcll9qY6b 0 Functions call stack Functions and call stack https://www.youtube.com/watch?v=jRcll9qY6b 0

What are functions and why they are useful Summary What are functions and why they are useful Local scope of variables in functions Function prototype, description, precond Selection statements (if-else, boolean true false, switch statements). Neat logic!! Repetition (will be covered more next week)