Bifalgorithm post · Computer science one · If, If-Else, If-Else-If Statements

How are if, if-else, and if-else-if statements written in C?

How are if, if-else, and if-else-if statements written in C? (Computer science one · If, If-Else, If-Else-If Statements) — "16.2. If, If-Else, If-Else-If State…

Ideas in this flowchart

  • Conditional statements in C: if, if-else, and if-else-if
  • C uses the keywords if, else, and else if.
  • Syntax rules
  • Conditions are placed inside parentheses immediately after if and else if.
  • The statement if(x < 10) does not end with a semicolon.
  • It determines the flow of control rather than acting as an executable statement.
  • Code Sample 16.1 provides examples of the three conditional forms.
  • The if statement executes a block when x is less than 10.
  • The if-else statement selects between the x-less-than-10 block and the alternative block.
  • The if-else-if statement distinguishes whether x is less than, equal to, or greater than 10.
Feed/

How are if, if-else, and if-else-if statements written in C?

No cover

Channel

Computer science one

1 page

"16.2. If, If-Else, If-Else-If Statements\n\nConditional statements in C utilize the keywords if, else, and else if. Conditions are placed inside parentheses immediately after the if and else if keywords. Examples of all three can be found in Code Sample 16.1. Some observations about the syntax: the statement, if(x < 10) does not have a semicolon at the end. This is because it is a conditional statement that determines the flow of control and not an executable statement. Therefore, no semicolon is used.\n\n//example of an if statement:\n\nif(x < 10) {\n\nprintf(\"x is less than 10\n\n\");\n\n}\n\n//example of an if-else statement:\n\nif(x < 10) {\n\nprintf(\"x is less than 10\n\n\");\n\n} else {\n\nprintf(\"x is 10 or more\n\n\");\n\n}\n\n//example of an if-else-if statement:\n\nif(x < 10) {\n\nprintf(\"x is less than 10\n\n\");\n\n} else if(x == 10) {\n\nprintf(\"x is equal to ten\n\n\");\n\n} else {\n\nprintf(\"x is greater than 10\n\n\");\n\n}\n\nCode Sample 16.1.: Examples of Conditional Statements in C"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.