Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Basic Logic Purpose if statement if / else statement

Similar presentations


Presentation on theme: "Agenda Basic Logic Purpose if statement if / else statement"— 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 Print “you are a senior”
System Flowchart Start Scan and store user’s age into a variable called age. It is assumed that the variable has already been declared... Enter age Is age > 64? NO YES Print “you are a senior” Stop

5 Print “you are a senior”
System Flowchart Start Enter age Test for condition: is variable age greater than than 64? Is age > 64? NO YES Print “you are a senior” Stop

6 Print “you are a senior”
System Flowchart Start Enter age If the test is true (i.e. yes), then print you are a senior Is age > 64? NO If the test is false (i.e. no), then skip command (do nothing) YES Print “you are 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 test is contained in brackets (no semi-colon after if 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 used after if statement when more than one command requires to be executed

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 commands following if statement are performed only if test is TRUE Otherwise, if test is FALSE skip commands (do nothing)

10 Comparison Operators The following symbols are used in C to compare numeric amounts when performing tests: Symbol Meaning > 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 Stop Enter height Is height >= 200? NO YES
You are tall! You are short! Stop

14 if / else Statements if (height >= 200) printf (“you are tall!\n”);
else printf (“you are short!\n”); Perform action if variable height is greater than or equal to 200 Perform action if variable height is less than 200 Notice that since only 1 command 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


Download ppt "Agenda Basic Logic Purpose if statement if / else statement"

Similar presentations


Ads by Google