CONDITIONAL STATEMENTS

Cards (40)

  • A program consists of a number of statements which are usually executed in _________
    sequence
  • Programs can be much more powerful if they can control the order in which statements are run.
    true
  • Three general types of statements:
    -Assignment
    -Input/Output
    -Control
  • It is where values, usually the results of calculations, are stored in variables.
    Assignment
  • Data is read in or printed out.

    Input/Output
  • The program makes a decision about what to do next.
    Control
  • The loop constructs

    while
    for
    do/while
  • Conditional statements
    if
    switch
  • Examples of control statements
    Loop constructs - while, for, do/while
    Conditional Statements - if, switch
    break, continue, goto statements
  • How can control statements be used to write powerful programs
    Repeating important sections of the program.
    Selecting between optional sections of a program.
  • is used when the program is made to decide whether to do something at a
    special point, or to decide between two courses of action.
    if-else statement
  • General form of if-else statement
    if (condition) statement;
    else statement;
  • Write a if-else statement. Program: If the condition (score >=70) is true then the next statement is executed. If it is false then the statement following the else is executed.
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
    int score;
    printf("Type the student's score (1-100): ");
    scanf ("%d", &score);

    If score >= 70
    printf("\nThe student passed the subjet");
    else
    printf("\Sorry, the student failed");
    return 0;
    }
  • General form of if statement
    if (condition) statement;
  • Write a if statement. Program: If the condition (score >=70) is true then the next statement is executed. If it is false then the statement following the else is executed.
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
    int score;
    printf("Type the student's score (1-100): ");
    scanf("%d", &score);

    if (score>=70)
    printf("\nThe student passed the subject.");
    if (score<70)
    printf("\nSorry, the student failed.");
    return 0;
    }
  • Why is if-else statement more efficient than if statement?
    The second if statement will be evaluated no matter how the first if statement will be resolved t. In large programs, these step could be source of bottleneck, which stops the program from progressing.
  • If the programmer wants to have more than one statement following the if or the else, they should be grouped together between ______
    curly braces
  • If the programmer wants to have more than one statement following the if or the else, they should be grouped together between curly braces. Such a grouping is called a___________
    compound statement or block.
  • Compound statement is also called a____
    block
  • The general form of the if-else with blocks of statement is
    if (condition) {
    statements;
    }
    else {
    statement;
    }
  • Use if-else statement with blocks. If the condition (score >=70) is true then the next statement is executed. If it is false then the statement following the else is executed.
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
    int score;
    printf("Type the student's score (1-100): ");
    scanf("%d", &score);
    if (score>=70)
    {printf("\nThe student passed the subject.");
    printf("\nCongratulations!");}
    else
    {printf("\nSorry, the student failed.");
    printf("\nBetter luck next time.");}
    return 0;
    }
  • In some instances, it would be necessary to do multiple checks before deciding on the course of action to take. Such cases could be solved with the use of _______________.
    nested if statements
  • They are are if statements within if statements.
    nested if statements
  • #include <stdio.h>
    #include <conio.h>
    int main()
    {
    int magic = 143;
    int guess;
    printf("Type your guess number");
    scanf("%d", &guess);
    if (guess==magic)
    {printf("\nYour guess is RIGHT.");
    printf("%d is the magic number.", guess);
    else
    printf("\nSorry your guess is WRONG!");
    if (guess>magic)
    printf("\nYour guess is too high.")
    else
    printf("\nYour guess is too low.");
    return 0;
    }
  • A common programming construct is the ______
    if-else-if ladder
  • if-else-if ladder has a general form of:
    if (condition) {
    statement (s);}
    else if (condition) {
    statement(s);}
    else if (condition) {
    statement(s);}
    .
    .
    else
    statement(s);
  • The conditions in if-else-if ladder is evaluated from the___
    top downward
  • The conditions are evaluated from the top downward. As soon-as a true condition is found, the statement associated with it is executed, and the rest of the ladder is bypassed. If none of the conditions are true, the final else is executed.

    True
  • The final else often acts as a default condition; that is, if all other conditional tests fail, the last else statement is performed.
    True
  • If the final else is not present, then no action takes place if all other conditions
    are false.
    True
  • #include <stdio.h>
    #include <conio.h>
    int main()
    {
    float score,grade;
    printf("Enter the student's grade:");
    scanf("%f",&score);
    if (score<50.0)
    {grade = 5.0;}
    else if(score>=50.0 &&score <60.0)
    {grade = 3.0;}
    else if(score>=60.0 &&score <70.0)
    {grade = 2.5;}
    else if(score>=70.0 &&score <80.0)
    {grade = 2.0;}
    else if(score>=80.0 &&score <90.0)
    {grade = 1.5;}
    else
    {grade = 1.0;}
    printf("The equivalent grade is %3.2f",grade);
    return 0;
    }
  • Although the if-else-if ladder can perform multi-way tests, the code could be very hard to follow if it becomes too long. It may even confuse the programmer at a later date. For these reasons, C has a built-in multiple-branch decision statement called _____
    switch
  • General form of switch
    switch (variable) {
    case constant 1 :
    statement (s ) ;
    break;
    case constant2 :
    statement ( s ) ;
    break;
    case constant3 :
    statement ( s ) ;
    break;
    .
    .
    .
    default:
    statement(s);
    }
  • In the switch statement, a variable is repeatedly tested against a list of integer or character constants.
  • The default keyword is also referred to as ______ on the switch, because the default acts as a safety net for all that does not fall into a case statement. Thus, the default keyword must always be placed as the last statement in a switch.
    forming closure
  • the default keyword must always be placed as the____ statement in a switch.
    last
  • The ____ statement is used to cause the program to proceed to the statement immediately following the closing curly brace of the switch statement.
    break
  • If the break is omitted, the program will continue to execute the rest of the case statements. This is because the case keyword is simply a label, and there is nothing in it that can cause program flow to be altered.

    True
  • No break statement, however, is needed for default, if default is the last statement of the switch. If default is used somewhere else, it must have a break statement.
    True
  • #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    char optr;
    float op1,op2, secondnum,result;
    printf("Please type the expression to be calculated:");
    scanf("%f %c %f", &op1,&optr,&op2);
    switch (optr)
    {
    case'+':
    result = op1 + op2; break;
    case'-':
    result = op1 - op2; break;
    case'*':
    result = op1 * op2; break;
    case'/':
    result = op1 / op2; break;
    default:
    printf("\Sorry, %c is not a valid arithmetic operator",optr);
    }
    printf("\nThe result is %3.2f",result);
    return 0;
    }