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

28
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:::::::::

Page 1: 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:::::::::

Page 2: 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

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

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

• }

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

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

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

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

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

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

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

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.

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

Boolean operators

• The three Boolean operators are:

• && and

• || or

• ! not (takes one operand; ``unary'')

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

If else

• if( expression ) statement<sub>1</sub> else statement<sub>2</sub>

• if (a > b) printf(a);

Else printf(b);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Putting values in arrays

• We can use assignment

• A[5] = 27

• A[3] = 8

• A[9] = 888

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

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]);• }

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

Exercise

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

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

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

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

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

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

strcpy

• strcpy(destinationstring, sourcestring)

• Copies sourcestring into destinationstring

• For example

• strcpy(str, “hello world”); assigns “hello world” to the string str

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

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”

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

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

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

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

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

strlen

• strlen(str) returns length of string excluding null character

• strlen(“tttt”) = 4 not 5 since \0 not counted