190 likes | 192 Views
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.
E N D
Agenda • Basic Logic • Purpose • if statement • if / else statement • Nested if statements
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.
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.
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
System Flowchart Start Enterage Test for condition:is variable agegreater thanthan 64? Isage > 64? NO YES Print “youare a senior” Stop
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
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!)
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
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)
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
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.
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.
System Flowchart Start Enter height Isheight >= 200? NO YES You are short! You are tall! Stop
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...
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)
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”
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.
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