1 / 19

Agenda

Learn about basic logic and control-flow statements in C programming, including if and if/else statements. Understand how to alter the flow of a program based on different conditions.

jpearl
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Agenda • Basic Logic • Purpose • if statement • if / else statement • Nested if statements

  2. Basic Logic • Logic allows for the ability to tell the computer to do “different things” under “different conditions”. • You will learn a set of C statements in this course that uses basic logic. • Statements which alter the flow or direction of a program depending on different situations are referred to as “control-flow” statements.

  3. Example of Basic Logic • As you may recall, after you have determined the inputs, outputs and required processing of you program, the next step is to write out the structure (including logic) of your program. • Let’s plan a program that asks for the user’s age, and display whether or not they are a senior citizen.We will display the structure of this program using a system flowchart. A system flowchart displays inputs, processes, storage and output, but it also display logical sequences.

  4. System Flowchart Start Scan and storeuser’s age intoa variable calledage. It is assumedthat the variablehas already beendeclared... Enterage Isage > 64? NO YES Print “youare a senior” Stop

  5. System Flowchart Start Enterage Test for condition:is variable agegreater thanthan 64? Isage > 64? NO YES Print “youare a senior” Stop

  6. System Flowchart Start Enterage If the test is true(i.e. yes), thenprint you are a senior Isage > 64? NO If the test is false(i.e. no), thenskip command(do nothing) YES Print “youare a senior” Stop

  7. if statement • The if statement is used to test a condition,and only perform action if test is TRUE • if (age > 64) • { • printf (“Congratulations,\n”); • printf (“you are a senior!\n”); • } Notice condition to testis contained in brackets(no semi-colon afterif statement!)

  8. if statement • The if statement is used to test a condition,and only perform action if test is TRUE • if (age > 64) • { • printf (“Congratulations,\n”); • printf (“you are a senior!\n”); • } Braces usedafter if statementwhen more thanone commandrequires to beexecuted

  9. if statement • The if statement is used to test a condition,and only perform action if test is TRUE • if (age > 64) • { • printf (“Congratulations,\n”); • printf (“you are a senior!\n”); • } Set of commandsfollowing ifstatement are performed only if test is TRUE Otherwise, if test is FALSEskip commands (do nothing)

  10. Comparison Operators • The following symbols are used in C to compare numeric amounts when performing tests:SymbolMeaning • > greater than >= greater than or equal to < less than <= less than or equal to != not equal to == equal to

  11. In Class Exercise • Plan and create a C program to ask the user for their height (in centimeters). If the height is 200 centimeters or over, display “you are tall!”; otherwise, do not display any message.

  12. if / else Statements • What if you needed to perform another action if the test condition is false? • The else statement is used with the if statement to provide an alternative action.

  13. System Flowchart Start Enter height Isheight >= 200? NO YES You are short! You are tall! Stop

  14. if / else Statements • if (height >= 200) • printf (“you are tall!\n”); • else printf (“you are short!\n”); Perform action if variableheight is greater than or equalto 200 Perform action if variableheight is less than 200 Notice that since only 1command is executed for each situation, no braces are required...

  15. In Class Exercise • Plan and write a C program to ask the user to choose a product: 1 or 2. If the user selects product 1, assign the variable sales with a value of $56.50; otherwise, if the user selects product 2, assign the variable sales with a value of $75.99 • Display “The value of the product is $xx.xx.”(where xx.xx represents value of variable sales)

  16. Nested if statements • Try to draw a system flowchart for the following situation: • Ask user for age, if age is less than or equal to 5 display the message “Sorry you can’t go on ride”. • If age is greater than 5, then prompt user for height (in centimeters). If height of user is greater than 150 cm, then display “You can go on ride”; otherwise, display “Sorry you are short”

  17. System Flowchart(Nested If Statement)

  18. Nested if statement • if (age > 5) • { • printf (“Please enter your height: ”); • scanf (“%d”, &height); • if (height >= 150) • printf (“OK, you can go\n”); else • printf (“Sorry, you are short\n”); • } • else printf (“Sorry, you can’t go\n”); Nested if statements are simply if statements that are contained within if statements.

  19. Nested if statement • if (age > 5) • { • printf (“Please enter your height: ”); • scanf (“%d”, &height); • if (height >= 150) • printf (“OK, you can go\n”); else • printf (“Sorry, you are short\n”); • } • else printf (“Sorry, you can’t go\n”); General rule with nested if statements indicate that you should match if’s with corresponding else’s

More Related