Top Banner
For loops For loops are controlled by a counter variable. for( c =init_value;c<=fin_value;c+=increment_value) { loop body; } c is a counter. c is a incremented after every iteration (can also be decreased!)
37

For loops For loops are controlled by a counter variable. for( c =init_value;c

Dec 16, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: For loops For loops are controlled by a counter variable. for( c =init_value;c

For loops

For loops are controlled by a counter variable.

for( c =init_value;c<=fin_value;c+=increment_value)

{loop body;

}

c is a counter.

c is a incremented after every iteration (can also be decreased!)

Page 2: For loops For loops are controlled by a counter variable. for( c =init_value;c

The factorial example again, this time using for

#include <stdio.h>int main(void){ 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;}

Page 3: For loops For loops are controlled by a counter variable. for( c =init_value;c

Factorial with for – step by step

#include <stdio.h>int main(void){ 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;}

---

i

3

n

1

fact

Page 4: For loops For loops are controlled by a counter variable. for( c =init_value;c

Factorial with for – step by step

#include <stdio.h>int main(void){ 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

i

3

n

1

fact

Page 5: For loops For loops are controlled by a counter variable. for( c =init_value;c

Factorial with for – step by step

#include <stdio.h>int main(void){ 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

i

3

n

1

fact

Page 6: For loops For loops are controlled by a counter variable. for( c =init_value;c

Factorial with for – step by step

#include <stdio.h>int main(void){ 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

i

3

n

1

fact

Page 7: For loops For loops are controlled by a counter variable. for( c =init_value;c

Factorial with for – step by step

#include <stdio.h>int main(void){ 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

i

3

n

2

fact

Page 8: For loops For loops are controlled by a counter variable. for( c =init_value;c

Factorial with for – step by step

#include <stdio.h>int main(void){ 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

2

fact

Page 9: For loops For loops are controlled by a counter variable. for( c =init_value;c

Factorial with for – step by step

#include <stdio.h>int main(void){ 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

Page 10: For loops For loops are controlled by a counter variable. for( c =init_value;c

Factorial with for – step by step

#include <stdio.h>int main(void){ 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

i

3

n

6

fact

Page 11: For loops For loops are controlled by a counter variable. for( c =init_value;c

Factorial with for – step by step

#include <stdio.h>int main(void){ 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

i

3

n

6

fact

Page 12: For loops For loops are controlled by a counter variable. for( c =init_value;c

For loops (cont.) Equivalent to while… Any for loop can be

converted to while loop and vice versa But 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.

Page 13: For loops For loops are controlled by a counter variable. for( c =init_value;c

Incremental operators

Used as a short-hand for incrementing (or decrementing) variables.i++ or ++i == i = i + 1i-- or --i == i = i – 1i += a == i = i + ai -= a == i = i - ai *= a == i = i * ai /= a == i = i / a

Page 14: For loops For loops are controlled by a counter variable. for( c =init_value;c

Example – fahrenheit-celsius conversion table/* Print a Fahrenheit-to-Celsius conversion table */

#include <stdio.h> int main (void) {

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;

}

Page 15: For loops For loops are controlled by a counter variable. for( c =init_value;c

Nested for loop – rectangle example/* Print a rectangle of *. The height and width are defined by the user */#include <stdio.h>

int main(void){

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");}

}

Page 16: For loops For loops are controlled by a counter variable. for( c =init_value;c

Exercise

Write a program that prints an upside-down half triangle of *.

The height of the pyramid is the input.

*****

*****

****

*

Page 17: For loops For loops are controlled by a counter variable. for( c =init_value;c

Solution #include<stdio.h>

int main(void){

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;}

Page 18: For loops For loops are controlled by a counter variable. for( c =init_value;c

Exercise

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

(Hint – first write a program that checks whether a given number is prime)

Page 19: For loops For loops are controlled by a counter variable. for( c =init_value;c

Solution#include <stdio.h>int main(void){

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;

}

Page 20: For loops For loops are controlled by a counter variable. for( c =init_value;c

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.

Page 21: For loops For loops are controlled by a counter variable. for( c =init_value;c

Solution 1#include <stdio.h>int main(void){

int i, j, last;int found = 0; /* This indicates whether 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;

}

Page 22: For loops For loops are controlled by a counter variable. for( c =init_value;c

Solution 2 (with break)#include <stdio.h>int main(void){

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;

}

Page 23: For loops For loops are controlled by a counter variable. for( c =init_value;c

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 (equals zero)

Page 24: For loops For loops are controlled by a counter variable. for( c =init_value;c

Example – waiting for legal input#include <stdio.h>int main(void){

int i;

printf("Please enter a positive number.\n");do { scanf("%d", &i);

if (i<=0) printf("That's not a positive number! Try again.\n");} while (i<=0);/* The program continues.... */return 0;

}

Page 25: For loops For loops are controlled by a counter variable. for( c =init_value;c

The ?: operator

expr1 ? expr2 : expr3

If expr1 is true (non-zero), expr2 is evaluated. Otherwise, expr3 is evaluated

Page 26: For loops For loops are controlled by a counter variable. for( c =init_value;c

The ?: operator#include <stdio.h>

int main(void){

int i, j, min;

printf("Please enter two numbers: ");scanf("%d%d", &i, &j);

min = i<j ? i : j;printf("The minimum between %d and %d is %d\n", i, j, min);

return 0;}

Page 27: For loops For loops are controlled by a counter variable. for( c =init_value;c

The switch statement a multiway conditional statement

similar to the if-else if-else "statement" allows the selection of an arbitrary number of

choices based on an integer value switch (expression) {

  case const-expr: statements

  case const-expr: statements

  …  default:

statements}

Page 28: For loops For loops are controlled by a counter variable. for( c =init_value;c

The switch statement

expression must have an integral value when the switch statement is

executed: the expression is evaluated if a case matches the value of the

expression, the program jumps to the first statement after that case label

otherwise, the default case is selected the default is optional

Page 29: For loops For loops are controlled by a counter variable. for( c =init_value;c

That grade example againswitch (grade/10) {

case 10: case 9:

printf ("A\n"); break;

case 8: printf ("B\n"); break;

case 7: printf ("C\n"); break;

case 6: printf ("D\n"); break;

default: printf ("F\n");

}

Page 30: For loops For loops are controlled by a counter variable. for( c =init_value;c

Give me a break when the switch transfers to the

chosen case, it starts executing statements at that point

it will “fall through” to the next case unless you “break” out

it causes the program to immediately jump to the next statement after the switch statement

Page 31: For loops For loops are controlled by a counter variable. for( c =init_value;c

Riddle me this Suppose a program’s execution reaches

the following line –scanf(“%d%c”, &i, &ch);

And suppose the user input is –100 b

What will be the contents of i and ch? Answer

i == 100 ch == ‘ ‘ (there’s a space in there)

Page 32: For loops For loops are controlled by a counter variable. for( c =init_value;c

Spaces in scanf

One way to overcome this problem is to introduce a space before the %c –

scanf(“%d %c”, &i, &ch); The space tells scanf to ignore all

whitespace characters that come after the %d is read

Page 33: For loops For loops are controlled by a counter variable. for( c =init_value;c

Example – mass conversion Write a program such that –

Input – A positive number indicating mass One of the following characters – o, c, k,

p, indicating measurement unit (ounce, carat, kilogram, or pound

Output – The same mass expressed in grams

convert_gr.c

Page 34: For loops For loops are controlled by a counter variable. for( c =init_value;c

Exercise Write a program that accepts a number

between 1 and 100 from the user. If there is a coin of that value in cents, it should display its name. Otherwise, it should report that there is no such coin

1 = cent, 5 = nickel, 10 = dime, 25 = quarter, 100 = dollar

Remember to check for the validity of the input!

Page 35: For loops For loops are controlled by a counter variable. for( c =init_value;c

Solution

Coins.c

Page 36: For loops For loops are controlled by a counter variable. for( c =init_value;c

A cooler exercise Write a program that accepts an real

number, followed by an arithmetical operator (+, -, *, /) and then a second real number

The program will calculate the result of the expression and present it on the screen

Example – for the input 10-8, the output will be – 10-8 = 2

Page 37: For loops For loops are controlled by a counter variable. for( c =init_value;c

Solution

Operation.c