Incremental operators

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Advertisements

For loops For loops are controlled by a counter variable. for( c =init_value;c
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Computer Science 1620 Loops.
Introduction to Computers and Programming Class 15 Introduction to C Professor Avi Rosenfeld.
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Introduction to C Programming CE Lecture 4 Further Control Structures in C.
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.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Structured Programming
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Chapter 5: Structured Programming
CSE 220 – C Programming Loops.
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
EKT120 COMPUTER PROGRAMMING
Administrative things
CSE1320 Loop Dr. Sajib Datta
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Loops Chapter 6 Copyright © 2008 W. W. Norton & Company.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Looping.
Arrays, For loop While loop Do while loop
Chapter 13 Control Structures
C Programming Variables.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
The while Looping Structure
Week 2 Variables, flow control and the Debugger
UMBC CMSC 104 – Section 01, Fall 2016
More Loops Topics Counter-Controlled (Definite) Repetition
Exercise Arrays.
ECE 103 Engineering Programming Chapter 18 Iteration
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 13 Control Structures
Presentation transcript:

Incremental operators Used as a short-hand i += a == i = i + a i -= a == i = i - a i *= a == i = i * a i /= a == i = i / a i++ or ++i == i = i + 1 == i += 1 i-- or --i == i = i – 1 == i -= 1

getchar, putchar and Buffers

getchar getchar() gets a single character from the user. Requires including stdio.h Returns a non-positive number on failure. Similar to scanf. char c; c = getchar(); char c; scanf("%c", &c); ====

putchar putchar(char c) prints out the character inside the brackets. Requires including stdio.h Similar to printf. char c; putchar(c); char c; printf("%c", c); ====

Buffers Computers have Buffers – places for temporary storage When we enter input to the program, it is stored in a buffer When our program gets a scanf or putchar command, it doesn’t really wait for our input – it looks in the buffer. Only if the buffer is empty it waits for input!!!

Example printf(“Please enter your age: ”); scanf(“%d”,&age); Buffer printf(“Please enter your age: ”); scanf(“%d”,&age); printf(“Please enter you gender (M/F): ”); scanf(“%c”,&gender); If (gender==‘M’) printf(“You are a %d year old man!\n”); else if (gender==‘F’) printf(“You are a %d year old woman!\n”); else printf(“No such gender on earth!\n”); \n F 25\n \nF age 25 gender \n

Solution To tell scanf we expect an enter or space before the next character, we use: scanf(“ %c”,&gender); or: scanf(“\n%c”,&gender);

Loops – The Power of Repetition C Programming Loops – The Power of Repetition

Loops Used to repeat the same instruction(s) over and over again. Block of code Some changing state Loop while some condition holds

Loops C provides flexible ways of deciding how many times to loop, or when to exit a loop. for, while, do-while loops.

while loops while (expr) { statement(s); } true false while (expr) { statement(s); } The statements are executed as long as the condition is true. When the condition is no longer true, the loop is stopped.

Example - factorial Initialize Test Increment int main() { int i = 0, n = 0, fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; /* this is the counter */ while (i <= n) fact *= i; ++i; } printf("the factorial is %d\n", fact); return 0; } Initialize Test Increment

Example - Fibonacci 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … Notice that we only need two elements in order to calculate the next one. 1 1 2 3 5 8 1 1 2 3 5 8 1 1 2 3 5 1 1 2 3 5 8 13 1 1 2 3 5 8 13 21 1 1 2 3 5 8 13 1 1 2 3 5 1 1 2 3 1 1 1 1 2 1 1 2 1 1 2 3 + + + +

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 8 Screen

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 8 Screen 1

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 8 Screen 1

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 8 Screen 1 1

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 2 8 Screen 1 1

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 1 2 8 Screen 1 1

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 2 8 Screen 1 1

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 2 8 Screen 1 1

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 2 8 Screen 1 1 2

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 1 2 3 8 Screen 1 1 2

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 2 3 8 Screen 1 1 2

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 3 8 Screen 1 1 2

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 3 8 Screen 1 1 2

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 3 8 Screen 1 1 2 3

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 2 3 5 8 Screen 1 1 2 3

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 3 5 8 Screen 1 1 2 3

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 5 8 Screen 1 1 2 3

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 5 8 Screen 1 1 2 3

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 5 8 Screen 1 1 2 3 5

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 3 5 8 8 Screen 1 1 2 3 5

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 5 5 8 8 Screen 1 1 2 3 5

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 5 8 8 8 Screen 1 1 2 3 5

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 5 8 8 8 Screen 1 1 2 3 5

Fibonacci – step by step int fib1 = 1, fib2 = 1, fib_next = 0, lim=8; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); fib1 fib2 fib_next lim 5 8 8 8 Screen 1 1 2 3 5

Example – lower-case to upper case. low2up.c

Low2up – step by step Buffer c upper_c Screen yeS\n ‘#’ ‘@’ c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character. */ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer yeS\n c upper_c ‘#’ ‘@’ Screen

Low2up – step by step Buffer c upper_c Screen eS\n ‘y’ ‘@’ c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer eS\n c upper_c ‘y’ ‘@’ Screen

Low2up – step by step Buffer c upper_c Screen eS\n ‘y’ ‘@’ c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer eS\n c upper_c ‘y’ ‘@’ Screen

Low2up – step by step Buffer c upper_c Screen eS\n ‘y’ ‘Y’ c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer eS\n c upper_c ‘y’ ‘Y’ Screen

Low2up – step by step Buffer c upper_c Screen eS\n ‘y’ ‘Y’ Y c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer eS\n c upper_c ‘y’ ‘Y’ Screen Y

Low2up – step by step Buffer c upper_c Screen S\n ‘e’ ‘Y’ Y c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer S\n c upper_c ‘e’ ‘Y’ Screen Y

Low2up – step by step Buffer c upper_c Screen S\n ‘e’ ‘Y’ Y c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer S\n c upper_c ‘e’ ‘Y’ Screen Y

Low2up – step by step Buffer c upper_c Screen S\n ‘e’ ‘Y’ Y c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer S\n c upper_c ‘e’ ‘Y’ Screen Y

Low2up – step by step Buffer c upper_c Screen S\n ‘e’ ‘E’ Y c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer S\n c upper_c ‘e’ ‘E’ Screen Y

Low2up – step by step c upper_c Screen S\n ‘e’ ‘E’ YE c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); S\n c upper_c ‘e’ ‘E’ Screen YE

Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘E’ YE c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘E’ Screen YE

Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘E’ YE c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘E’ Screen YE

Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘E’ YE c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘E’ Screen YE

Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘S’ YE c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘S’ Screen YE

Low2up – step by step Buffer c upper_c Screen \n ‘S’ ‘S’ YES c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer \n c upper_c ‘S’ ‘S’ Screen YES

Low2up – step by step Buffer c upper_c Screen ‘\n’ ‘S’ YES c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer c upper_c ‘\n’ ‘S’ Screen YES

Low2up – step by step Buffer c upper_c Screen ‘\n’ ‘S’ YES c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer c upper_c ‘\n’ ‘S’ Screen YES

Low2up – step by step Buffer c upper_c Screen ‘\n’ ‘S’ YES c = getchar(); while (c != '\n' && c >= 0) { if (c >= 'a' && c <= 'z') upper_c = c - 'a' + 'A'; else upper_c = c; /* Print the converted character.*/ putchar(upper_c); /* Get the next character */ } putchar('\n'); Buffer c upper_c ‘\n’ ‘S’ Screen YES

Exercise Input: Output: Note: Two integers – A and B How many times A contains B This is the result of the integer division A/B Note: Do not use the division operator!

Solution int main() { int a = 0, b = 0, res = 0; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); res = 0; while ((res + 1) * b <= a) ++res; printf("%d / %d = %d", a, b, res); return 0; }

break in a loop When break is encountered, the loop is stopped regardless of whether the condition is still true. The program then continues to run from the first line after the loop. If called within a nested loop, break breaks out of the inner loop only.

Example – what does this do? int main() { int i = 1; char c; printf("Enter a line of text:\n"); c = getchar(); while (c != '\n' && c >= 0) { if (c == ' ') break; ++i; } …

Example – Counting Letters int main() { int i = 0; char c; printf("Enter a line of text:\n"); c = getchar(); while (c != '\n' && c >= 0) { if (c == ' ') /* found the first space - exit the loop */ break; ++i; } if (c== ' ') /* we found a space */ printf("There are %d letters before the first space.\n", i); else /* The loop ended without finding spaces */ printf("There are no spaces in the input line.\n"); return 0;

continue When continue is encountered, the rest of the loop body is ignored. The program then continues to run from the beginning of the loop.

What does it do? int main() { int i = 0; while (i < 10) { i++; if (i % 2 == 0) break; printf("%d\n", i); } printf("The End\n"); return 0; } 1 The End

and this? 1 3 5 7 9 The End int main() { int i = 0; while (i < 10) { i++; if (i % 2 == 0) continue; printf("%d\n", i); } printf("The End\n"); return 0; } 1 3 5 7 9 The End

for loops for (<initialization>; <condition>; <increment>) {     statement(s) } initialization condition statement(s) increment true false

The factorial example again, this time using for #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0;

Factorial with for – step by step n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; --- 3 1

Factorial with for – step by step n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 1 3 1

Factorial with for – step by step n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 1 3 1

Factorial with for – step by step n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 2 3 1

Factorial with for – step by step n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 2 3 2

Factorial with for – step by step n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 3 3 2

Factorial with for – step by step n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 3 3 6

Factorial with for – step by step n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 4 3 6

Factorial with for – step by step n fact #include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) fact *= i; } printf("the factorial is %d\n", fact); return 0; 4 3 6

for loops (cont.) Equivalent to while. Any for loop can be converted to while loop and vice versa Some applications are more natural to for, and others to while. If we want to perform something for a predefined number of times, better use for. If we just wait for something to happen (not after a certain number or iterations), better use while.

Example – Fahrenheit-Celsius conversion table int main ( ) { int fahr = 0; double celsius = 0.0; int lower = 0, upper = 300; int step = 20; for (fahr = lower; fahr <= upper; fahr += step) celsius = 5.0 * (fahr - 32.0) / 9.0; printf("%d\t%g\n", fahr, celsius); } return 0;

Another way to do it… int main ( ) { int fahr = 0; double celsius = 0.0; int lower = 0, upper = 300; int step = 20; for (fahr = lower; fahr <= upper; fahr ++) if (fahr%step != 0) continue; celsius = 5.0 * (fahr - 32.0) / 9.0; printf("%d\t%g\n", fahr, celsius); } return 0;

Nested Loops The loop body can contain other loops For each iteration of the outer loop we iterate the inner loop a number of times. while (...) { for(...) { . . . } . . . }

Nested Loops - example int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0;

Nested Loops - example row col --- --- int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; --- ---

Nested Loops - example row col 1 --- int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 1 ---

Nested Loops - example row col 1 1 int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 1 1

Nested Loops - example row col 1 1 2 3 4 int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 1 1 2 3 4

Nested Loops - example row col 1 5 output 1 2 3 4 5 int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 1 5 output 1 2 3 4 5

Nested Loops - example row col 2 5 output 1 2 3 4 5 int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 2 5 output 1 2 3 4 5

Nested Loops - example row col 2 1 4 2 3 output 1 2 3 4 5 int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 2 1 4 2 3 output 1 2 3 4 5

Nested Loops - example row col 2 5 output 1 2 3 4 5 2 4 6 8 10 int main() { int row, col; for (row = 1; row <= 5; ++row) for (col = 1; col <= 5; ++col) printf("%4d", row * col); } printf("\n"); return 0; 2 5 output 1 2 3 4 5 2 4 6 8 10

do-while loops do { statement(s) } while (condition); Similar to while loops Except the condition is evaluated after the loop The loop body is executed at least once, even if the expression is never true condition true false

Example – waiting for legal input int main() { int i = 0; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i <= 0) printf("Try again.\n"); } while (i <= 0); /* The program continues.... */ return 0; }

Exercise Write a program that prints an upside-down half triangle of *. The height of the pyramid is the input. ***** **** *** ** *

Solution int main() { int i = 0, j = 0, size = 0; printf("Please enter a size:\n"); scanf("%d",&size); for (i = 0; i < size; ++i) { for (j = 0; j < (size – i); ++j) printf("*"); printf("\n"); } return 0;

Exercise Write a program that accepts a number from the user, and prints out all of the prime numbers up to that number.

Solution int main() { int i = 0, j = 0, last = 0; printf("enter a number\n"); scanf("%d", &last); for (i = 2; i <= last; ++i) for (j = 2 ; j < i; ++j) if (i % j == 0) break; } if (j == i) printf("the number %d is prime\n", i); return 0;

Exercise Change the former program, so that is displays only the largest prime number which is smaller than or equal to the user’s input.

Solution int main() { int i, j, num; printf("enter a number\n"); scanf("%d", &num); for (i = num; i > 1; --i) for (j = 2 ; j < i; ++j) if (i % j == 0) break; if (j == i) /* i is prime. We found our number */ } printf("The largest prime not larger than %d is %d.\n", num, i); return 0;

C Programming Arrays

Arrays A block of many variables of the same type Array can be declared for any type E.g. int arr[10] is an array of 10 integers. Examples: list of students’ marks series of numbers entered by user vectors matrices

In Memory: The array variable holds the address in memory of the array beginning The n-th element of array arr is specified by arr[n-1] 1 2 3 4 5 6 7 8 9 int arr[10]; 5 21 arr arr[0]=5; arr[7]=21; arr[10]=3; Error!!!

Example - minimum #include <stdio.h> int main(void) { int i, min, array[10]; printf("please enter 10 numbers:\n"); for(i = 0; i < 10; ++i) scanf("%d", &array[i]); min = array[0]; for(i = 1; i < 10; ++i) { if (array[i] < min) min = array[i]; } printf("the minimum is: %d\n", min); return 0;

Define Magic Numbers (like 10 in the last example) in the program convey little information to the reader Hard to change in a systematic way #define defines a symbolic name During preprocessing phase, symbolic names are replaced by the replacement text

minimum with #define Use capital letters #include <stdio.h> #define ARRAY_SIZE 10 int main(void) { int i, min, array[ARRAY_SIZE]; printf("please enter %d numbers:\n", ARRAY_SIZE); for(i = 0; i < ARRAY_SIZE; ++i) scanf("%d", &array[i]); min = array[0]; for(i = 1; i < ARRAY_SIZE; ++i) { if (array[i] < min) min = array[i]; } printf("the minimum is: %d\n", min); return 0; Use capital letters

Initialization Like in the case of regular variables, we can initialize the array during declaration. the number of initializers cannot be more than the number of elements in the array

Initialization The array size can be inferred from the number of initializers by leaving the square brackets empty These are identical declarations : int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16}; int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};

Exercise Write a program that gets 10 numbers from the user. It then accepts another number and checks to see if that number was one of the previous ones. Example 1: Please enter 10 numbers: 1 2 3 4 5 6 7 8 9 10 Please enter a number to search for: 8 I found it! Example 2: Please enter a number to search for: 30 Sorry, it’s not there.

Solution (simple_search.c) #include <stdio.h> #define ARRAY_SIZE 10 int main(void) { int array[ARRAY_SIZE], i, num; printf("Please enter %d numbers:\n", ARRAY_SIZE); for (i = 0; i < ARRAY_SIZE; ++i) scanf("%d", &array[i]); printf("Please enter a number to search for:\n"); scanf("%d", &num); for (i = 0; i < ARRAY_SIZE; ++i) { if (array[i] == num) { printf("I found it!\n"); return 0; } printf("Sorry, it's not there\n");