Presentation is loading. Please wait.

Presentation is loading. Please wait.

To remind us We finished the last day by introducing If statements Their structure was:::::::::

Similar presentations


Presentation on theme: "To remind us We finished the last day by introducing If statements Their structure was:::::::::"— Presentation transcript:

1 To remind us We finished the last day by introducing If statements Their structure was:::::::::

2 If statements The simplest way to modify the control flow of a program is with an if statement, which in its simplest form looks like this: if(x > max) max = x; The syntax of an if statement is: If ( expression ) statement where expression is any expression and statement is any statement

3 Basic example #Include main() { int a; printf(“enter number \n”); scanf(“%d”,&a); if(a > 10) printf(“the number you entered %d is more than ten”,c); }

4 A series of statements after an if statement What if you have a series of statements, all of which should be executed together or not at all depending on whether some condition is true? The answer is that you enclose them in braces: if( expression ) { statement 1 statement 2 statement 3 }

5 Basic example #Include main() { int a,b,c; printf(“enter first number \n”); scanf(“%d”,&a); printf(“enter second number \n”); scanf(“%d”,&b ); c= a*b; If (c > 100) {printf(“ok \n”); printf(“the product of the numbers you entered is %d”,c); }

6 Expressions in If statements The expression in an if statement is a Boolean expression typically made up of Relational and Boolean operators The statements in an if stamenst are any legal statements

7 Relational Operators The relational operators such as, and >= are in fact operators, just like +, -, *, and /. The relational operators take two values, test them, and ``return'' a value of 1 or 0 depending on whether the tested relation was true or false. The complete set of relational operators in C is: < less than <= less than or equal > greater than >= greater than or equal == equal != not equal For example, 1 4 is 0, 5 == 5 is 1, and 6 != 6 is 0.

8 Boolean operators The three Boolean operators are: && and || or ! not (takes one operand; ``unary'')

9 If else if( expression ) statement 1 else statement 2 if (a > b) printf(a); Else printf(b);

10 If else if We can also have multiple specific alternatives using if … else if….. Suppose we have a variable grade containing a student's numeric grade, and we want to print out the corresponding letter grade. Here is code that would do the job: if(grade >= 90) printf("A"); else if(grade >= 80) printf("B"); else if(grade >= 70) printf("C"); else if(grade >= 60) printf("D"); else printf("F");

11 Basic example #Include main() { int a,b,c; printf(“enter first number \n”); scanf(“%d”,&a); printf(“enter second number \n”); scanf(“%d”,&b ); If (a > b !! a > 5) printf(“%d is greater than %d or 5”,a,b); Else printf(“%d is not greater than %d and not greater than 5”,a,b); }

12 Exercises Write a program which accepts an integer as input and determines whether or not it is even or odd Write a program which accepts a year as input and determines whether or not it is a leap year

13 Iterations and Loops The purpose of a loop is to repeat the same action a number of times We refer to each time an action is repeated as an iteration. We continue repeating the action until a terminating condition is satisfied. This terminating condition is called a loop guard The loop guard is represented by a boolean expression in the same way as the condition of an IF statement. Before the loop starts we initialise variables involved in the loop In C there are a number of ways to represent loops

14 For loops For loops are a common form of loop particularly for repeating a task a fixed number of times( counting loops) The syntax of a for loop is For (loop initialization, loop guard, increment) statement

15 Example of for loop for (i = 0; i < 10; i = i + 1) printf("i is %d\n", i); i=0 is the initialization of the loop counter i < 10 is the loop guard i.e. repeat until i is greater or equal to 10 i = i + 1; this is the counter increment ; it is more commonly written i ++ printf("i is %d\n", i); is the loop statement

16 While loops The most basic loop in C is the while loop. The general syntax of a while loop is while( expression ) statement The expression represents the loop guard. While it is true repeat the statement. Terminate when the expression evaluates to false

17 Example of a while loop #include main() {int x x = 2; while(x < 1000) { printf("%d\n", x); x = x * 2; } } This program repeatedly prints out powers of two until we generate a number greater than or equal to 1000 The terminating condition is thus when the power of two equals or exceeds 1000

18 An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared Int a[10] which declares an array of 10 integers whose indices go from 0-9

19 Putting values in arrays We can use assignment A[5] = 27 A[3] = 8 A[9] = 888

20 We could use scanf and a for loop #include main() { int a[10] int i for ( i = 0;i < 10 ; i++) { printf(“enter a number”); scanf(“%d”,&a[i]); } for ( i = 0;i < 10 ; i++) printf(“the %d th number in the arrray is %d \n”,i,a[i]); }

21 Exercise Write a program to enter 10 numbers into an array and to print out the maximum element of the array.

22 Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters char s[10] char p[30] When declaring a string don’t forget to leave a space for the null character which is also known as the string terminator character

23 C offers four main operations on strings strcpy - copy one string into another strcat - append one string onto the right side of the other strcmp – compare alphabetic order of two strings strlen – return the length of a string

24 strcpy strcpy(destinationstring, sourcestring) Copies sourcestring into destinationstring For example strcpy(str, “hello world”); assigns “hello world” to the string str

25 strcat strcat(destinationstring, sourcestring) appends sourcestring to right hand side of destinationstring For example if str had value “a big ” strcat(str, “hello world”); appends “hello world” to the string “a big ” to get “ a big hello world”

26 strcpy strcmp(stringa, stringb) Compares stringa and stringb alphabetically Returns a negative value if stringa precedes stringb alphabetically Returns a positive value if stringb precedes stringa alphabetically Returns 0 if they are equal Note lowercase characters are greater than Uppercase

27 Input output functions of characters and strings getchar() reads a character from the screen in a non-interactive environment getche() like getchar() except interactive putchar(int ch) outputs a character to screen gets(str) gets a string from the keyboard puts(str) outputs string to screen

28 strlen strlen(str) returns length of string excluding null character strlen(“tttt”) = 4 not 5 since \0 not counted


Download ppt "To remind us We finished the last day by introducing If statements Their structure was:::::::::"

Similar presentations


Ads by Google