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 *=

Slides:



Advertisements
Similar presentations
The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
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.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
1 Gentle Introduction to Programming Session 2: Functions.
Guidelines for working with Microsoft Visual Studio.Net.
Guidelines for working with Microsoft Visual Studio 6.
לולאות 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
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.
1 C Programming Week 2 Variables, flow control and the Debugger.
Chapter 5: Structured Programming
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.
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.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
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”
CISC105 – General Computer Science Class 4 – 06/14/2006.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
1 More on Arrays. 2 Review: Passing Arrays to Functions Passing an array: what goes on in the memory? Why isn’t it the same as when passing primitives?
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Chapter 5: Structured Programming
CSE 220 – C Programming Loops.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Looping.
- Additional C Statements
C Programming Variables.
Loops in C.
Program Control Topics While loop For loop Switch statement
Incremental operators
Week 2 Variables, flow control and the Debugger
UMBC CMSC 104 – Section 01, Fall 2016
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

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 *= a  ==  i = i * a i /= a  ==  i = i / a

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 some flexible ways of deciding how many times to loop, or when to exit a loop. for, while, do-while loops.

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

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

Example – fibonacci series fibonacci.c

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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) prints out the character inside the brackets. Requires including stdio.h Similar to printf. char c; putchar(c); char c; printf(“%c”, c);  ==== 

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

Low2up – step by step #include int main() { char c; char upper_c; printf(“Enter a string: "); c = getchar(); Buffer c upper_c Screen

Low2up – step by step #include int main() { char c; char upper_c; printf(“Enter a string: "); c = getchar(); yeS\n Buffer c upper_c Screen

Low2up – step by step #include int main() { char c; char upper_c; printf (“Enter a string: "); c = getchar(); eS\n Buffer c upper_c Screen

Low2up – step by step 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 */ c = getchar(); } putchar('\n'); eS\n Buffer c upper_c Screen

Low2up – step by step 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 */ c = getchar(); } putchar('\n'); eS\n Buffer c upper_c Screen

Low2up – step by step 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 */ c = getchar(); } putchar('\n'); eS\n Buffer ‘y’‘Y’ c upper_c Screen

Low2up – step by step eS\n Buffer ‘y’‘Y’ c upper_c Y Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step S\n Buffer ‘e’‘Y’ c upper_c Y Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step S\n Buffer ‘e’‘Y’ c upper_c Y Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step S\n Buffer ‘e’‘Y’ c upper_c Y Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step S\n Buffer ‘e’‘E’ c upper_c Y Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step S\n Buffer ‘e’‘E’ c upper_c YE Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step \n Buffer ‘S’‘E’ c upper_c YE Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step \n Buffer ‘S’‘E’ c upper_c YE Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step \n Buffer ‘S’‘E’ c upper_c YE Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step \n Buffer ‘S’ c upper_c YE Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step \n Buffer ‘S’ c upper_c YES Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step Buffer ‘\n’‘S’ c upper_c YES Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step Buffer ‘\n’‘S’ c upper_c YES Screen 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 */ c = getchar(); } putchar('\n');

Low2up – step by step Buffer ‘\n’‘S’ c upper_c YES Screen 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 */ c = getchar(); } putchar('\n');

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

Solution #include int main() { int a, b, res; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); res = 0; while ( (res+1) * b <= a) res = res + 1; 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 while loop. If called within a nested loop, break breaks out of the inner loop only.

Example – counting letters break.c

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.

for loops for loops are controlled by a counter variable. for (c = begin; c <= end; c += inc) { loop body } initializationconditionincrement

The factorial example again, this time using for #include 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 --- i 3 n 1 fact #include 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 1 i 3 n 1 fact #include 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 1 i 3 n 1 fact #include 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 2 i 3 n 1 fact #include 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 2 i 3 n 2 fact #include 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 3 i 3 n 2 fact #include 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 i 3 n 6 fact #include 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

4 i 3 n 6 fact #include 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

4 i 3 n 6 fact #include 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; }

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 /* Print a Fahrenheit-to-Celsius conversion table */ #include int main ( ) { int fahr; double celsius; 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; }

Nested for loop – rectangle example /* Print a rectangle of *. The height and width are defined by the user */ #include int main( ) { int i, j; int height, width; printf("Please enter the two box dimensions: \n"); scanf("%d%d", &height, &width); for (i = 1; i <= height; i++) { for (j = 1; j <= width; j++) printf("*"); printf("\n"); }

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

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

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

Solution #include int main() { int i, j, last; 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 1 #include int main() { int i, j, last; int found = 0; /* This indicates if we found the largest prime */ printf("enter a number\n"); scanf("%d", &last); i = last; while (!found) /* Loop until we find our guy */ { for (j = 2 ; j < i; j++) if (i % j == 0) break; if (j == i) /* If this is true then i is prime */ found = 1; else i--; } printf("The largest prime not larger than %d is %d.\n", last, i); return 0; }

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

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

Example – waiting for legal input #include int main() { int i; 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; }

Debugging It is virtually impossible to program without errors Syntax errors are detected by the compiler However, often a program has no syntax errors and compiles, but still doesn’t perform as desired

Debugging Debuggers are software tools designed to help find software bugs Both Visual C and the lcc compiler include a debugger

Debugging The debugger allows us to –  Execute the program one line at a time  At each step see the values of all variables and expressions  Run the program up to a pre-specified point  And more…

The debugger’s common features Setting breakpoints (a point where the execution stops): bring the cursor to desired line and press the palm icon or F9. A dark red dot appears near the line. Executing a debugged run: Build->start debug->go or F5. The program will run and stop at the first breakpoint.

The debugger’s common features (cont.) Stopping at a specific line: Bringing the cursor to the line and press ctrl+F10, or Build->start debug->go to cursor. The program will stop at that point. Stepping to the next line – F10. Entering a function – F11. Seeing variable values – quickwatch and/or debug window at the bottom. The yellow arrow indicates our whereabouts at any given moment.