C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.

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

Introduction to C Programming
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Loops and Files.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 5: Control Structures II (Repetition)
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
Introduction to Computer Programming in c
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 5: Control Structures II (Repetition)
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
- Standard C Statements
Lecture 7: Repeating a Known Number of Times
for Repetition Structures
Week 4 – Repetition Structures / Loops
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Arrays, For loop While loop Do while loop
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
3 Control Statements:.
Chapter 6: Repetition Statements
Computer programming Lecture 3.
Chapter 5: Control Structures II (Repetition)
-2- Introduction to C Programming
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Dale Roberts, Lecturer IUPUI
DATA TYPES There are four basic data types associated with variables:
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

C Language 1 Program Looping

C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions Increment / decrement operators Increment / decrement operators break / continue statements break / continue statements scanf( ) formats scanf( ) formats

C Language3 Repetition Repetition - multiple execution of the same statement(s) (while, do while, for)

C Language4 Repetition // show repetition main() { // variable declarations // code statements while ( this == TRUE ) { statement1; // TRUE, keep doing it !!! } statement2; } FALSE (done)

C Language5 Equality Operators equal to == (a == b) not equal to != (a != b) Do not confuse = with = = A terrible language design construct

C Language6 Relational Operators less than < (a < b) less than or equal <= (a <= b) greater than > (a > b) greater than or equal >= (a >= b)

C Language7 Equality or Relational Expressions for example: a == b result of comparison is always an int result of comparison has value 0 or 1 0 is considered false (FALSE) 1 is considered true (TRUE)

C Language8 Increment / Decrement Operators increment ++ (a++ or ++a) decrement -- (a-- or --a) can be prefix or postfix add or subtract 1 from the operand

C Language9 Prefix vs. Postfix ++a ++a if a equals 0 if a equals 0 b = ++a b = ++a then a equals 1 and b equals 1 then a equals 1 and b equals 1 use new value use new value a++ a++ if a equals 0 if a equals 0 b = a++ then a equals 1 and b equals 0 use old value Prefix : Postfix :

C Language10 Repetition Statements Repeat statement(s) a controlled number of times can be infinite (then out of control!) Controlled by a test expression, typically a relational expression, which must be enclosed in parentheses ( ) Control a single statement or a compound statement (i.e. a block) for while do while

C Language11 for Good for counter-controlled or indexed loops Index variable or loop counter (typically an integer variable), controls the loop Condition is tested at the top of the loop (i.e. before executing the loop statements) Has initialization and update expressions for manipulating the loop counter

C Language12 for // show for statement main() { // variable declarations int i,// loop index sum = 0; // sum // for statement for ( i = 0 ; i < 10 ; i++ ) { sum = sum + i ; }

C Language13 while Good for flag or sentinel value conditions Uses while keyword Condition is tested at the top of the loop (i.e. before executing the loop statements)

C Language14 while // show while statement main() { // variable declarations int flag = 1;// flag variable // while statement while ( flag == 1 ) { statements; }

C Language15 do while Good for flag or sentinel value conditions, when you need to execute the loop statements at least once Uses do and while keywords (as a pair) Condition is tested at the bottom of the loop (i.e. after executing the loop statements) Requires a semicolon ; at the end

C Language16 do while // show do while statement main() { // variable declarations int flag = 0;// flag variable // do while statement do { statements; } while ( flag == 1 ) ; }

C Language17 while and for // show for as while statement main() { // variable declarations int I;// loop index int sum = 0; // sum // for loop implemented with while i = 0 ; while ( i < 10 ) { sum = sum + i ; i++ ; }

C Language18 break and continue break statement, used for early termination of a loop continue statement, used for early continuation of a loop

C Language19 break // show break statement while ( 1 ) { statements1; break ; statements2; } statements3;

C Language20 continue // show continue statement while ( 1 ) { statements1; continue ; statements2; } statements3;

C Language21 scanf() Formatted input from the console Scanf(control string, variable argument address list) The control string can be text embedded with conversion specifications (these begin with a % and end with a conversion character) The variable argument address list is a comma separated list of addresses of variables, and each argument must correspond to one conversion specification in the control string Uses the address operator (&) for simple variables

C Language22 scanf() Examples scanf(“%d”, &average) scanf(“%i %i %i”, &month, &day, &year) scanf(“%i:%i:%i”, &month, &day, &year) scanf(“%f”, &somefloatvalue)

C Language23 scanf( ) Conversion Characters d or i – integer in decimal o – integer in octal x – integer in hexadecimal e, f, g – floating point c – single character s – character string consult C Standard Library documentation for details

C Language24 WJK Programming Maxims Always indent to show the logic of your program. Always indent to show the logic of your program. Never use assignments in a conditional test (e.g. if ( total = sum ) or while( this = next ) Never use assignments in a conditional test (e.g. if ( total = sum ) or while( this = next )