IPC144 Introduction to Programming Using C Week 3 – Lesson 1

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Decisions If statements in C.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Agenda Basic Logic Purpose if statement if / else statement
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
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.
COMP Loop Statements Yi Hong May 21, 2015.
Mindstorm NXT-G Introduction Towson University Robotics.
Agenda  Basic Logic - Continued...  if else statements  switch Statements  Compound Conditions.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Introduction to Programming Using C Basic Logic. 2 Contents Conditional programming If statement Relational operators Compound statements Loops While.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Repetition statements
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Programming Loops (continued).
Chapter 4 – C Program Control
CNG 140 C Programming (Lecture set 3)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
ECE Application Programming
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Repetition Structures Chapter 9
The switch Statement, and Introduction to Looping
CS161 Introduction to Computer Science
EGR 2261 Unit 4 Control Structures I: Selection
Iterations Programming Condition Controlled Loops (WHILE Loop)
While Loops (Iteration 2)
Control Structures – Selection
Control Structures - Repetition
Week 8 - Programming II Today – more features: Loop control
Chapter 4 Control structures and Loops
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Programming Fundamentals Lecture #6 Program Control
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Iteration: Beyond the Basic PERFORM
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Chapter 4 Selection.
REPETITION STATEMENTS
SELECTIONS STATEMENTS
CprE 185: Intro to Problem Solving (using C)
A LESSON IN LOOPING What is a loop?
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
CS2011 Introduction to Programming I Selections (I)
EPSII 59:006 Spring 2004.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
EECE.2160 ECE Application Programming
Repetition Statements (Loops) - 2
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
EECE.2160 ECE Application Programming
CprE 185: Intro to Problem Solving (using C)
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
Switch Case Structures
Presentation transcript:

IPC144 Introduction to Programming Using C Week 3 – Lesson 1 (Pages 26 to 37 in IPC144 Textbook)

Agenda Additional C programming Elements Take-up REALITY CHECK (Q3/4 – Week 2 Lesson1) Take up REALITY CHECK (Q3 – Week 2 Lesson 2) Logical operators (&& and ||) DeMorgan's law using script command (to generate typescript) switch/case statement Examples Introduction to looping

Take-up Homework REALITY CHECK (Week 2 – Lesson 1) Questions #3 & #4

Take-up Homework REALITY CHECK (Week 2 – Lesson 2) Question #3

Logic Relational Operators The simplest kinds of conditions to test in if statements involve the comparison of numeric amounts (using relational operators). Relational Operators > Greater than >= Greater than or equal to < Less than <= Less than or equal to == equal to != Not equal to

Logic Compound Relation Operators Sometimes a simple condition, such as a single comparison, is not sufficient to control an if statement or a while statement. In these situations, it is usually a combination of circumstances that determine what code should be executed. Relational Operators && true if, and only if, both of the sub-conditions are true. || true if either (or both) of the sub-conditions are true.

Logic Compound Relation Operators Example: if ( x < y && x < z ) printf (“x is less then y and z\n\n”); if ( x < y || x < z ) printf (“At least y or z is greater than x\n\n”); if ( x < y || x < z && y < z ) printf (“ x is less than y, or x and y is less than z\n\n”);

Logic Compound Relation Operators && ||

Practice From what we have learned as of now, let’s try the REALITY CHECK handout, Question #1 and #2 Using the handout, plan and then code your program

DeMorgan's Law To get the opposite of a compound condition, you must reverse all the sub- conditions and, at the same time, change all &&s to ||s, and all ||s to &&s.

Switch / Case Statements The switch is an alternative to a nested if statement in certain circumstances. The syntax for switch is: switch (variable) { case 1: statement(s); break; case 2: statement(s);break; ...and so on (as many cases as you like)... default: statement(s)‏ }

Switch / Case Statements Example: int number; printf ("Pick a number (1 or 2): "); scanf ("%d", &number); switch (number)‏ { case 1: printf ("You picked number 1.\n\n"); break; case 2: printf ("You picked number 2.\n\n"); default: printf("You must select either 1 or 2.\n\n"); } Notice a break statement is used to end the execution for a specific case!

Practice From what we have learned as of now, let’s try the REALITY CHECK handout, Question #3 and #4 Using the handout, plan and then code your program

Loops So far, our programs when involving logic, may do different things under different situations, but another foundation of programming is the ability to repeat. For Example: - Keep prompting user for data until correct data entered - Repeat calculations (eg. exponents: 2 x 2 x 2 x 2 x 2) - Repeat operation until user enters a zero to end entry of data.

Loops There are 2 category of loops: Determinant Loops (for() statement)‏ The number of repetitions are known. For example, repeating display of "Hello" 4 times.... Indeterminant Loops ( while() do-while() statements)‏ The number of repetitions is unknown. Repetitions depend on user input. For example, repeating display of "Hello" until user (when prompted) enters number 0 to exit...

Homework TASK #1 TASK #2 TASK #3 TASK #4 *** Highly Recommended *** Complete lab #2 since it is due at the beginning of this week’s lab! TASK #2 Study for a quiz to be held in this week’s lab based on material taught last week TASK #3 Complete and code the solutions for today’s REALITY CHECK QUESTIONS, as well as “hide” and repeat the walk-thru questions TASK #4 *** Highly Recommended *** Read ahead in IPC144 Programming Notes (especially looping).