CSE1311 Introduction to Programming for Science & Engineering Students Mostafa Parchami, Ph.D. Dept. of Comp. Science and Eng., Univ. of Texas at Arlington, USA Slides adopted from T. Ferguson Spring 2016
Topics Boolean Expressions If Statement Switch Statement 2
Why do we need conditions? scanf("%d", &pressurevalue); while (pressurevalue <= MAX_PRESSURE) if (pressurevalue == NOMINAL_VALUE) printf("pressure is nominal\n"); else printf("pressure is %d pounds\n", pressurevalue); for (i = 5; i <= pressurevalue; i = i + 10) printf("+"); printf("\n"); scanf("%d", &pressurevalue); // End while 3
What is a conditional Expression? A conditional expression evaluates to a value of true or false These are the two values for the Boolean type named after the mathematician George Boole When a conditional expression is evaluated, it results in a value of type Boolean In the C programming language there is no explicit Boolean type. Instead, false is 0 and true is any non-zero value. Many C programs use a #define macro to set a Boolean variable to true or false #define FALSE 0 #define TRUE 1 Conditional expressions are also called Boolean expressions A conditional expression is used whenever a true or false decision needs to be made about the value of certain variables or constants in a program 4
Expression Contents A conditional expression in C may contain any valid mathematical expression It may also contain relational operators and logical operators In addition, it may contain one or more function calls that return a value 5
Relational Operators ==,!= Equivalent, Not equivalent <, <= Less than, Less than or equals >, >= Greater than, Greater than or equals if (A == B) C = 5; if (A!= B) C = 10; for (i = 0; i < MAX_INDEX; i++) printf("*"); if (A <= B) D = 20; while (A > B) B++; if (A >= B) D = 40; A = B <= C; D = A == B; 6
Logical Operators && Logical AND Logical OR! Logical NOT if ( (A == B) && (C <= D) ) E = 5; if ( (A!= B) (C >= D) ) F = 10; while (!(A == B) ) B++; if (!((A >= B) && (C <= D) (E!= F))) G = 100; A = B C; D = A && B; 7
Boolean Algebra if (0 0) printf("does false OR false = true?"); if (0 && 0) printf("does false AND false = true?"); 8
if, if-else, if-else-if // Variable can be char or int switch(<variable>) case <value1>: // variable==value1 case <value2>: // variable==value2 default: // variable!=value1 and variable!=value2 9
// Variable can be char or int switch(<variable>) case <value1>: // variable==value1 case <value2>: default: if(<condition1>) //Condition1 is true else if(<condition2>) // Condition1=false, Condition 2=true switch if(<condition>) //Condition is true else // Condition is false if(<condition1>) //Condition1 is true else if(<condition2>) // Condition1=false, Condition 2=true 10
Programming Examples 1) Read an integer and print Odd if the number is odd, print Even otherwise. 2) Read an integer and if it is positive print Positive, otherwise print Negative. 3) Read three numbers and print them in decreasing order. 4) Read a character and if the character is equal to + read two integers and print their sum. a, read two numbers and print the average. If the value is n, read one number negative of that number. If the value is e, do nothing. Otherwise, print Entered wrong character! ; 5) Read a character, if it was 1 or 2 or 3 or a or b, print Class A!. If the character was 4 or 5 or 6 or c or d, print Class B!. 11
Problem 1 /*A program that reads an integer is even!*/ #include <stdio.h> int main() int num; printf("please enter an integer: "); scanf("%d", &num); if (num % 2 == 0) // Is it even? printf("even"); else // Is not even? printf("odd"); return 0; 12
Problem 2 /*A program that reads an integer and checks if it is positive or negative!*/ #include <stdio.h> int main() int num; printf("please enter an integer: "); scanf("%d", &num); if (num > 0) // Is it positive? printf("positive"); else if (num < 0) // Is it negative? printf("negative"); else // Not positive or negative?! printf("zero!"); return 0; 13
Problem 3 14
// A simple calculator #include <stdio.h> Problem 4 int main() char ch; float num1, num2, average, sum, negative; printf("please enter the control character: "); scanf("%c", &ch); switch (ch) case '+': printf("please enter the first number: "); scanf("%f", &num1); printf("please enter the second number: "); scanf("%f", &num2); sum = num1 + num2; printf("the sum of %f and %f is: %f", num1, num2, sum); 15
Problem 4 return 0; case 'a': case 'n': case 'e': default: printf("please enter the first number: "); scanf("%f", &num1); printf("please enter the second number: "); scanf("%f", &num2); average = (num1 + num2)/2; printf("the average of %f and %f is: %f", num1, num2, average); printf("please enter the first number: "); scanf("%f", &num1); negative = -num1; printf("the negative of %f is: %f", num1, negative); printf("entered wrong character!"); 16
#include <stdio.h> Problem 5 int main() char ch; printf("please enter the control character: "); scanf("%c", &ch); switch (ch) case '1': case '2': case '3': case 'a': case 'b': printf("class A!"); case '4': case '5': case '6': case 'c': case 'd': printf("class B!"); return 0; 17