To remind us We finished the last day by introducing If statements Their structure was:::::::::
-
date post
21-Dec-2015 -
Category
Documents
-
view
213 -
download
0
Transcript of To remind us We finished the last day by introducing If statements Their structure was:::::::::
To remind us
• We finished the last day by introducing If statements
• Their structure was:::::::::
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
Basic example
• #Include <stdio.h>• main()• { int a;• printf(“enter number \n”);• scanf(“%d”,&a);
• if(a > 10) printf(“the number you entered %d is more than ten”,c);
• }
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<sub>1</sub> statement<sub>2</sub>
statement<sub>3</sub> }
Basic example• #Include <stdio.h>• 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);• }• }
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
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 < 2 is 1, 3 > 4 is 0, 5 == 5 is 1, and 6 != 6 is 0.
Boolean operators
• The three Boolean operators are:
• && and
• || or
• ! not (takes one operand; ``unary'')
If else
• if( expression ) statement<sub>1</sub> else statement<sub>2</sub>
• if (a > b) printf(a);
Else printf(b);
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");
Basic example• #Include <stdio.h>• 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);• }
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
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
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
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
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
Example of a while loop
• #include <stdio.h>• 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
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
Putting values in arrays
• We can use assignment
• A[5] = 27
• A[3] = 8
• A[9] = 888
We could use scanf and a for loop
• #include <stdio.h>• 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]);• }
Exercise
• Write a program to enter 10 numbers into an array and to print out the maximum element of the array.
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
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
strcpy
• strcpy(destinationstring, sourcestring)
• Copies sourcestring into destinationstring
• For example
• strcpy(str, “hello world”); assigns “hello world” to the string str
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”
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
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
strlen
• strlen(str) returns length of string excluding null character
• strlen(“tttt”) = 4 not 5 since \0 not counted