Exam 2 – Nov 18th Room ACIV 008. Project 2 Update  Your code needs to use loops to create the multiplication table. Hint: use nested for loop (Lecture.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
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.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computer Programming
Chapter 4 – C Program Control
UMBC CMSC 104 – Section 01, Fall 2016
CprE 185: Intro to Problem Solving (using C)
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Relational & Logical Operators
INC 161 , CPE 100 Computer Programming
Looping.
- Additional C Statements
Relational and Logical Operators
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Relational and Logical Operators
Chapter 4 - Program Control
CSC215 Lecture Control Flow.
UMBC CMSC 104 – Section 01, Fall 2016
Relational, Logical, and Equality Operators
Relational & Logical Operators, if and switch Statements
Relational and Logical Operators
ECE 103 Engineering Programming Chapter 19 Nested Loops
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
Relational and Logical Operators
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
Relational and Logical Operators
More Loops Topics Counter-Controlled (Definite) Repetition
Relational and Logical Operators
CSC215 Lecture Control Flow.
Lecture 9: Implementing Complex Logic
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Switch Case Structures
Presentation transcript:

Exam 2 – Nov 18th Room ACIV 008

Project 2 Update  Your code needs to use loops to create the multiplication table. Hint: use nested for loop (Lecture L14 slide 18)  Use loop structure to compute the power.

A Sample Program to Illustrate Switch-Case switch (grade) { case ('a') : case ('A') : printf ("Good Job!\n") ; brake; case ('b') : case ('B') : printf ("Pretty good.\n") ; brake;

A Sample Program to Illustrate Switch-Case case ('c') : case ('C') : printf ("Better get to work.\n") ; brake; case ('d') : case ('D') : printf ("You are in trouble.\n") ; brake; default : printf ("You are failing!!\n") ; brake; } /* End of switch-case structure */ } /* End of main program */

Which Are Legal Identifiers? AREA area_under_the_curve 3D num45 Last-Chance #values x_yt3 pi num$ %done lucky*** Try them all in one of your the programs!!!

Logical Operators  So far we have seen only simple conditions. if ( count > 10 )...  Sometimes we need to test multiple conditions in order to make a decision.  Logical operators are used for combining simple conditions to make complex conditions. && is ANDif ( x > 5 && y < 6 ) || is ORif ( z == 0 || x > 10 ) ! is NOTif ( ! (bob > 42) )

Arithmetic Expressions: True or False  Arithmetic expressions evaluate to numeric values.  An arithmetic expression that has a value of zero is false.  An arithmetic expression that has a value other than zero is true.

Practice with Arithmetic Expressions int a = 1, b = 2, c = 3 ; float x = 3.33, y = 6.66 ; ExpressionNumeric Value True/False a + b3T b - 2 * a0F c - b – a0F c – a2T y – x3.33T y - 2 * x0F

Truth Table for && Exp 1 Exp 2 Exp 1 && Exp nonzero 0 nonzero0 0 nonzero nonzero 1 Exp1 && Exp2 && … && Expn will evaluate to 1 (true) only if ALL subconditions are true.

Truth Table for || Exp 1 Exp 2 Exp 1 || Exp nonzero 1 nonzero0 1 nonzero nonzero 1 Exp1 && Exp2 && … && Expn will evaluate to 1 (true) if only ONE subcondition is true.

Truth Table for ! Expression ! Expression 0 1 nonzero 0

Some Practice Expressions int a = 1, b = 0, c = 7; Expression True/False a1T b0F c7T a + b1F a && bT&&FF a || bT || FT !c7F !!c7T a && !bT && TT a < b && b < cF && TF a > b && b < cT && TT a >= b || b > cT || FT

Preprocessor Directives  Lines that begin with a # in column 1 are called preprocessor directives (commands).  Example: the #include directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code.  If we have #include the preprocessor will place the contents of the file foo.h into the code.  This header file was included because it contains information about the printf ( ) function that is used in this program.

Nested for Loops for ( i = 1; i < 5; i = i + 1 ) { for ( j = 1; j < 3; j = j + 1 ) { if ( j % 2 == 0 ) { printf (“O”) ; } else { printf (“X”) ; } printf (“\n”) ; } How many times is the “if” statement executed? What is the output ? XO

15 Nested for Loops int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=10; columns++) { printf("*"); } printf ("\n"); } Output: ********** Inner Loop Outer Loop

16 Nested for Loops, Example #2 int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=rows; columns++) { printf ("*"); } printf ("\n"); } Output: * ** *** **** ***** Outer LoopInner Loop

Nested for Loops int j, k; for(j = 0; j < 8; j++)// { for(k = 0; k < 8 - j; k++) //draw MAX - j blanks { printf(" "); } for(k = 0; k <= j; k++) //draw remaining j columns as x's { printf("x"); } printf("\n"); } printf("\n"); x xx xxx xxxx xxxxx xxxxxx xxxxxxx xxxxxxxx

The break Statement  The break statement can be used in while, do-while, and for loops to cause premature exit of the loop.  THIS IS NOT A RECOMMENDED CODING TECHNIQUE.

Example break in a for Loop #include int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) { if (i == 5) { break ; } printf (“%d “, i) ; } printf (“\nBroke out of loop at i = %d.\n”, i) ; return 0 ; } OUTPUT: Broke out of loop at i = 5. If brake was not there the output would have been

The continue Statement  The continue statement can be used in while, do-while, and for loops.  It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop.  THIS IS NOT A RECOMMENDED CODING TECHNIQUE.

Example continue in a for Loop #include int main ( ) { int i ; for ( i = 1; i < 10; i = i + 1 ) { if (i == 5) { continue ; } printf (“%d ”, i) ; } printf (“\nDone.\n”) ; return 0 ; } OUTPUT: Done. 5 Note we used continue ; to skip printing 5

Exam 2 – Nov 18th Room ACIV 008