# Practice4 - Reminder – if statement int main void …...

This preview shows pages 1–15. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Reminder – if statement int main( void ) { … if (input<‘a’) number=number+5; … } Reminder – switch statement switch (num_of_cars/10) { case 10: case 9: printf ("A\n"); break ; /* What happens here? case 10 || 9: printf ("A\n"); break; */ } Loops Used to repeat the same instruction(s) over and over again. 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 exited. Example - factorial #include <stdio.h> int main( void ) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; while (i<=n) { fact = fact*i; i++; } printf("the factorial is %d\n", fact); return 0; } This is a counter Every iteration i is incremented by 1. Equivalent to i=i+1. 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"); Screen 5 lim 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"); Screen 5 lim 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 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 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"); 0 1 1 Screen 5 lim 1 fib1 1 fib2 1 fib_next...
View Full Document

## This note was uploaded on 01/10/2010 for the course CS 463 taught by Professor Can'tsay during the Spring '09 term at Haaga - Helia University of Applied Sciences.

### Page1 / 80

Practice4 - Reminder – if statement int main void …...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online