What kind of statement is the if statement?
Alter flow of control from sequential execution of statements Show Allow us to skip parts of the program Whether/what to skip depends on some condition
2 kinds of if-statements: "if-then" or "if-then-else" Syntax:
Examples: if (x == y) printf ("%d is equal to %d", x, y); if (x == y) printf ("%d is equal to %d", x, y); else printf ("%d is not equal to %d", x, y); If-then statements If we need to do something under a given condition and nothing otherwise, set up the condition so that the action is performed in the "then-part", rather than the "else-part".
Notice that the condition for the first example is the negation of the condition for the second example: !(x != y) is the same as (x == y) If-then statements if (value == 10) printf ("value is okay\n"); printf ("Good work!\n"); "Good work!" is always printed. if (value == 10){ printf ("value is okay\n"); printf ("Good work!\n"); } "Good work is printed only if value is 10 Nested if-then-else statements Each if-statement is itself a statement, so it can be (part of) the statement in an if-statement. Nesting can go on indefinitely: if condition1
if condition2
statement;
else {
if condition3
statement;
else
statement;
}
else
statement; "Attachment Problem" If the nesting is deep, how do you know which ELSE goes with which IF? An ELSE always "attaches" to the nearest preceding IF that is not already attached to an ELSE: if condition1 /* if #1 */ if condition2 /* if #2 */ statement; else /* belongs to #2 */ statement; Nested if-then-else statements if condition1 /* if #1 */ if condition2 /* if #2 */ statement; else /* still belongs to #2 */ statement; To force the else to attach to the first if, you must use braces: if condition1 /* if #1 */ { if condition2 /* if #2 */ statement; } else statement /* belongs to #1 */ Sequential if-then statements vs. if-then-else If you have a series of if-then statements, each one will always be executed: if (command == ‘p’) /* code for command ‘p’ */; if (command == ‘i’) /* code for command ‘i’ */; In this case, since a command can only be one thing at a time, this is inefficient. Once we know it’s ‘p’, there’s no point in checking to see if it’s ‘i’! Sequential if-then statements vs. if-then-else If conditions are mutually exclusive, use if-then-else: if (command == ‘p’) /* code for command ‘p’ */; else if (command == ‘i’) /* code for command ‘i’ */; Sequential if-then statements If conditions are not mutually exclusive, you should use sequential if-statements. if (x == y) printf ("x = y"); if (x == z) printf ("x = z"); if (y == z) printf ("y = z"); Efficiency Simplify! Don't put the same code in both the then-part and the else part. scanf ("%c", &response); if (response == 'a') { printf ("You chose an apple\n"); printf ("What else do you want?\n"); } else { printf ("You chose an orange\n"); printf ("What else do you want?\n"); } Simplified: scanf ("%c", &response); printf ("You chose an "); if (response == 'a') printf ("apple\n"); else printf ("orange\n"); printf ("What else do you want?\n"); Efficiency
Compound Conditions Three boolean operators allow us to form compound conditions: OR: NOT: The condition must be false (it must evaluate to 0) for the expression to be true Precedence of operators (from highest to lowest): ! What type of statement is if?The IF statement is a decision-making statement that guides a program to make decisions based on specified criteria. The IF statement executes one set of code if a specified condition is met (TRUE) or another set of code evaluates to FALSE.
What is an if statement also known as?The if statement is also known as a decision making statement, as it makes a decision on the basis of a given condition or expression. The block of code inside the if statement is executed is the condition evaluates to true.
Are if statements conditional statements?If / Else / Else If conditional statements are conditional statements that have more than one condition. If the first condition is false, only then will the second condition will be checked.
What type of logic is an if statement?If statements are logical blocks used within programming. They're conditional statements that tell a computer what to do with certain information. In other words, they let a program make 'decisions' while it's running. They're comprised of a minimum of two parts, 'if' and 'then'.
|