Control Flow - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Control Flow

Description:

Control Flow – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 10
Provided by: mah138
Category:

less

Transcript and Presenter's Notes

Title: Control Flow


1
Control Flow
2
if/else if construct
  • include ltstdio.hgt
  • int main(void)
  •     int number 75    int mark
  •     printf("Your examination mark\n")    printf
    ("Enter your score, please\n")    scanf("d",ma
    rk)    if (mark gt number)            printf
    ("Incredible, you have passed with a
    merit\n")        else if (mark gt
    65)            printf("You have
    passed\n")         else         printf("You
    have failed\n")        return 0

3
switch
  • The general form of a switch statement is
  • switch (variable) case expression1 do
    something 1 break
  • case expression2 do something
    2 break....default do default processing

4
  • int main(void)    float numb1 0, numb2 0
          / the two numbers to work on /    int
    menu 1             / add or substract or
    divide or multiply /    float total
    0           / the result of the calculation
    /    char calType             / what type of
    calculation /
  •     printf("Please enter in the first of the two
    numbers\n\t")    scanf("f", numb1)           
          / READ first number /
  •     printf("\n\nPlease enter the second of the
    two numbers\n\t")    scanf("f", numb2)
                    / READ second number /
  •     printf("\n\nWhat would you like to
    do?\n\n")    / WRITE instructions
    /    printf("\t1 add\n")    printf("\t2
    substract\n")    printf("\t3
    multiply\n")    printf("\t4 divide\n")
  •     printf("\n\nPleas make your selection
    now\n\t")    scanf("d",menu)
                      / READ calculation type /
  •     switch (menu)                  / select the
    type of calculation /        case 1 total
    numb1 numb2        calType ''
                / assign a char to symbolise
    calculation type /        break    case 2
    total numb1 - numb2        calType
    '-'        break    case 3 total numb1
    numb2        calType ''        break    c
    ase 4 total numb1 / numb2        calType
    '/'        break     default printf("Invalid
    option selected\n")        if (menu 3
    numb2 0)            / cannot divide by 0
    /        printf("\n\n\tYou cannot divide by
    0\n\n")
  •               / display result to 2 decimal
    places /    printf("\n\n
    ")    printf("\n\n\t.3f c .3f .2f",
    numb1, calType, numb2, total)    printf("\n\n
    \n\n")
  •     return 0

5
Loop constructs
  • While
  • For
  • Do-while

6
Break
  • breakis used to exit from a do, for, or while
    loop, It is also used to exit from a switch
    statement. An example of break in a loop is shown
    here
  • while (x lt 100)    x get_new_x()    if
    (kbhit())
  • break     / key hit on keyboard
    /    process(x)

7
continue
  • continue is used to bypass portions of code in a
    loop and forces the conditional expression to be
    evaluated. For example, the following while loop
    will simply read characters from the keyboard
    until an M or F gender is specified
  • include ltstdio.hgtinclude ltconio.hgtinclude
    ltstdio.hgtinclude ltctype.hgt
  • int main(void)    char gender
  •      while (gender getch())            i
    f (gender ! 'M' gender ! 'F'
    )                    printf("Incorrect gender,
    please type again\n")            continue     
       
  •      break        return 0

8
Bitwise operators
  • bitwise AND TRUE if both of its operands is
    TRUE
  • bitwise inclusive OR TRUE if either or
    both of its operands is TRUE.
  • bitwise exclusive OR TRUE only if just one
    of the operands is True
  • ones compliment turns all zeros into ones,
    and ones into zeros

9
Bit Shifts
  • Arithmetic shift
  • Logical shift

right shiftgtgt
Left shift ltlt
Left shiftltlt
right shiftgtgt
Write a Comment
User Comments (0)
About PowerShow.com