• Like
Dti2143 chap 4 control statement part 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Dti2143 chap 4 control statement part 2

  • 831 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
831
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
71
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Chapter 4
    Selection RECAP
  • 2. IF, ELSE
    if(Control Statement)
    {
    Selection operation
    }
    Else
    {
    Selection operation
    }
  • 3. IF ELSE SAMPLE
    int main()
    {
    int age;
    printf("please enter payment amount ");
    scanf("%d",&payment);
    if(payment>50)
    {
    payment=payment-10;
    }
    else
    {
    payment=payment;
    }
    printf("please pay %d",payment);
    }
  • 4. IF, ELSE IF, ELSE
    If (Control Statement)
    {
    Selection operation/action
    }
    else if (Control Statement)
    {
    Selection operation/action
    }
    else
    {
    Selection operation/action
    }
  • 5. IF, ELSE IF, ELSE SAMPLE
    int main(){
    int marks;
    printf("nEnter Marks :");
    scanf("%d",&marks);
    if ( marks<=100 && marks>=80){printf("nYour grade is A");}
    else if ( marks<=79 && marks>=60){printf("nYour grade is B");}
    else if ( marks<=59 && marks>=40){printf("nYour grade is C");}
    else if ( marks<=39 && marks>=20){printf("nYour grade is D");}
    else if ( marks<=19 && marks>=0){printf("nYour grade is E");}
    else{printf("nThe exam mark must be between 0 and 100 inclusive.");}
    return 0;
    }
  • 6. CHAP 4
    SWITCH, WHILE LOOP, DO WHILE LOOP,
  • 7. SWITCH CASE
    Format:
    Only Integer/
    character
    switch( conditional expression/variable)
    {
    case label1: C statement;
    break;
    case label2: C statement;
    break;

    default: pernyataanC;
    }
    value
    Exit from option
    Functioning like else
  • 8. How To Convert this If Else Snippet to Switch?
    if ( input == 1)
    printf(“nOne”);
    else if (input == 2)
    printf(“nTwo”);
    else if (input == 3)
    printf(“nThree”);
    else if (input == 4)
    printf(“nFour”);
    else
    printf(“nFive”);
  • 9. Answer:
    switch(input)
    {
    case 1:
    printf("nOne"); break;
    case 2:
    printf("nTwo");break;
    case 3:
    printf("nThree");break;
    case 4:
    printf("nFour");break;
    default:
    printf("nFive");break;
    }
  • 10. IF-ELSE / SWITCH CASE Side by Side
    switch(input)
    {
    case 1:
    printf("nOne"); break;
    case 2:
    printf("nTwo");break;
    case 3:
    printf("nThree");break;
    case 4:
    printf("nFour");break;
    default:
    printf("nFive");break;
    }
    if ( input == 1)
    printf(“nOne”);
    else if (input == 2)
    printf(“nTwo”);
    else if (input == 3)
    printf(“nThree”);
    else if (input == 4)
    printf(“nFour”);
    else
    printf(“nFive”);
  • 11. WHILE LOOP
    • Statement will be repeated if the condition is TRUE.
    • 12. Format :
    IMPORTANT!
    1. Counter
    2. Counter initialization
    3. Conditional expression testing
    4. Increment/decrement of counter
    counter = 0;
    while (conditional expression)
    {
    C statement;
    counter++;
    }
  • 13. WHILE LOOP IMPORTANT ASPECTS
    1. Counter Initialization
    • Counter is a variable
    • 14. Must be declared as an integer
    • 15. Used to count amount of loops
    • 16. Can be initialized starting from any value.
    • 17. Example : x = 0; a= 1; I = 3;proceed=‘y’;lagi!=‘n’;
    2. Conditional Expression
    • Logical (>,<,==,!=,<=,>=) symbol is used
    • 18. Used to ensure the loop/repetition is occurred.
    • 19. Example:
    while ( I < 4)
  • 20. WHILE LOOP IMPORTANT ASPECTS
    3.Increment/decrement value of counter
    • Used to increase/ decrease the value of counter
    for the statements to repeat.
    • If not, infinite loop will occured
    • 21. Example:
    I ++;
    ++a;
    y--;
    :: increment is used if the initial value of counter is
    less while decrement is used if the initial value of counter
    is high.
  • 22. Example 1:
    void main()
    { int nom1,nom2,nom3,nom4,nom5;
    printf(“nEnter one number :”);
    scanf(“%d”,&nom1);
    printf(“ nEnter one number:”);
    scanf(“%d”,&nom2);
    printf(“ nEnter one number:”);
    scanf(“%d”,&nom3);
    printf(“ nEnter one number:”);
    scanf(“%d”,&nom1);
    printf(“ nEnter one number:”);
    scanf(“%d”,&nom5);
    }
    void main()
    { int nom;
    inti;
    i =0;
    while(i <5)
    {
    printf(“ nEnter one number:”);
    scanf(“%d”,&nom);
    i++;
    }
    }
    counter
    condition
    increment
    WHILE LOOP CONVERT
  • 23. Determine the output of the following whileloop statement :
    a = 1;
    while ( a <= 5)
    { printf(“Number %d”,a);
    a++;
    }
  • 24. Use do..while statement
    Repetition will occurred if condition is TRUE
    Similar to while, must include 3 important things
    Format :
    counter = 0;
    do
    { C statement;
    counter++;
    } while (x < 5);
    Repeated statements
    DO-WHILE LOOP
  • 25. DO-WHILE/WHILE DIFFERENCES
  • 26. Example : Converting the following do..while loop to while loop
    i = 2;
    j = 1;
    do
    { i++;
    printf(“ i x j = %d”, i * j);
    } while (i < 8);
    i= 2;
    j = 1;
    while (i < 8)
    { i++;
    printf(“ i x j = %d”, i * j);
    }
    while
    do..while
    DO-WHILE to WHILE LOOP CONVERT
  • 27. FOR LOOP
    • Use for loop
    • 28. Repeat statement until condition is FALSE
    • 29. Similar to while loop, must includes 3 important things
    • 30. Format :
    for (initial_value; conditional expression ;increment)
    {
    C statement;
    }
    Repeated
    statements
  • 31. FOR LOOP SAMPLE
    intbil, nilai = 3;
    for (bil = 5; bil >0; bil--)
    {
    if( bil % 2 == 1)
    printf(“n %d”, bil + nilai);
    }
  • 32. WHILE,DO-WHILE and FOR LOOP side by side
  • 33. Selecting between while, do while and for loops
    • A ‘for’ loop is used when the number of times the loop is executed is well known
    • 34. A ‘while’ loop is used when the number of times the loop gets executed is not known and the loop should not be executed when the condition is initially false
    • 35. A ‘do while’ loop is used when the number of times the loop gets executed is not known and the loop should be executed even when the condition is initially false