Modulus Operator #include main() { int a, b, q, r; printf(" a b a/b a%b\n"); while (scanf("%d%d", &a, &b) != EOF) { q = a / b; /* quotient */ r = a % b;

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

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
For loops For loops are controlled by a counter variable. for( c =init_value;c
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
CS201 - Repetition loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 Structured Programming in C Welcome to CPSC 206.
CS150 Introduction to Computer Science 1
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
1 Repetition structures Overview while statement for statement do while statement.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive.
Basic Concepts – Conditionals Conditionals are used for making decisions. The conditionals available in C are if and if-else statements the switch statement.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 5.
Expressions So far, we have seen fairly simple expressions. For example, x = 4; z = z - y; if(x%4 == 0) However, expressions can be much more complex.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
IIT Kanpur C Course Lecture 3 Aug 31, Rishi Kumar, Final year BT-MT, CSE.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Overview Go over parts of quiz? Another iteration structure for loop.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
Chapter 9 Control Structures.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
February 25,  The BDE(Begin-During-End) event.  Worksheet – Exercise # 9 Instructions  2nd Period Test.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
Operators And Expressions
Lecture 7: Repeating a Known Number of Times
Chapter 5: Loops and Files.
Arrays, For loop While loop Do while loop
Structure of a C Program
Debugging October 3, 2007 ComS 207: Programming I (in Java)
- Additional C Statements
Chapter 9 Control Structures.
Chapter 6 Decision Making and Looping
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Modulus Operator #include main() { int a, b, q, r; printf(" a b a/b a%b\n"); while (scanf("%d%d", &a, &b) != EOF) { q = a / b; /* quotient */ r = a % b; /* remainder */ printf("%4d %5d %5d %5d\n", a, b, q, r); } a.out < in.dat a b a/b a%b (in.dat contains a list of the first two columns of numbers a and b) It is always true that a = q*b + r with |r|<|b|, b  0.

Relational/Logical Expressions Any values can be used in logical expressions. Zero (0) is interpreted as false; any nonzero values (such as 1, -1, 5) are interpreted as true. The results of relational or logical operations are always 0 or 1. Examples: x = 5; y = !x; /* y gets 0 */ z = x || y; /* z gets 1 */

Exercise, Do it now What are printed? #include main() { int x, y, z, u, v; int j1, j2, j3, j4, j5; x = -1; y = 3; z = 0; u = 100; v = 7; j1 = v/u - x % y; j2 = y % v % y; j3 = x && y || u < v; j4 = x <= !y || z; j5 = !!x == x; printf("%d %d %d %d %d\n", j1, j2, j3, j4, j5); } 10110

The For Loop For( ; ; ) is a statement for looping. The general form is for(expr1; expr2; expr3) action where expr1 is evaluated first and once only. expr2 is checked as a condition [like in while(expr)]. If expr2 is true, action is performed. Then expr3 is evaluated. Repeat expr2/action/expr3 until expr2 becomes false.

For Loop for( ; ; ) { } expr2expr3 expr1 action Initialization test update

For Loop Example sum = 0; for(i = 1; i <= 4; i += 1){ sum += i; printf("i=%d, sum=%d\n", i, sum); } prints i=1, sum=1 i=2, sum=3 i=3, sum=6 i=4, sum=10

For Loop Example sum = 0; for(i = 1; i <= 4; ++i) sum += i; printf("i=%d, sum=%d\n", i, sum); prints i=5, sum=10 ++ is called increment operator. ++i is the same as i = i + 1, or i += applies only to integral type variables.

Equivalence of while and for For(expr1; expr2; expr3) action Is equivalent to expr1; while (expr2) { action expr3; }

Equivalence of for and while while(expr) action Is equivalent to for( ; expr; ) { action }

Infinite Loop (looping forever) With for loop we can write for( ; ; ) action If expr2 (test) is missing, the condition is regarded as true. With while, we can say while ( 1 ) action

Comma Operator "," The expression expr1, expr2,..., exprn can be used in places where only one expression is logically allowed. The expr1, expr2, and so on are evaluated in turn, the value of the whole expression is the value of exprn. Example for(i=0,j=5; i<j; ++i,--j) printf("%d %d\n", i, j) prints

Increment ( ++ ) Operator When we execute ++x (prefix) two things happen: –The value of the variable x is increased by 1. –The value of the expression ++x is equal to the new value (original value plus 1). When we execute x++ (postfix) two things happen: –The value of the variable x is increased by 1. Same as ++x. –The value of the expression x++ is equal to the old value (original value of x).

Decrement ( -- ) Operator When we execute --x two things happen: ‚The value of the variable x is decreased by 1.  The value of the expression --x is equal to the new value (original value minus 1). When we execute x-- two things happen: ‚The value of the variable x is decreased by 1. Same as --x.  The value of the expression x-- is equal to the old value (original value of x).

++/--, Examples int x, y; x = 4; y = --x; /* y gets 3 */ x = 1; y = 4; z = --y == 3 && !(x++ <=1); /* -- y is 3, -- y==3 is true, x++ increases x by 1, the value of the expression x++ is 1 (not 2), (x++ <= 1) is true. !(x++ <= 1) is false. True && false is false. So z gets 0. */ These are examples and 3.7.5, on page 105.

Reading/Home Working Read Chapter 3, page 99 to 110. Work on Problems –Section 3.6, page , exercise 1, 3, 5. –Section 3.7, page 106, exercise 1, 3, 5. Check your answers in the back of the textbook. Do not hand in.

Lab 1, Due Monday, 17 Aug 98 Work on Problems –Page 67-68, Programming exercise 2.3, 2.5, 2.9. –Page 111, Programming exercise 3.1 Follow the instructions given by our Teaching Assistant Alice Heng.