ECE160/CS160 - Lesson 3
Conditionals, Input/Output, Switch, Loops, Basic Arrays
Professor Hong
## Review & Questions
## Conditional Statements
## If Statements ``` if (a > b) { z = a; // do something } ``` * Example ``` #include
int main() { int a = 0; int b = 5; if (a < b) { printf("a is less than b\n"); } return 0; } ```
## If-Else Statements ``` if (a > b) { z = a; // do something } else { z = b; // do something else } ``` or ``` expr1 ? expr 2 : expr 3 ```
## If-Else Statement Example ``` #include
int main() { int a = 7; int b = 5; if (a < b) { printf("a is less than b\n"); } else { printf("a is greater than or equal to b\n"); } return 0; } ```
## If-Else If-Else Statements ``` if (a > b) { z = a; // do something } else if (a > c) { z = c; // do something } else { z = b; // do something else } ```
## If-Else If-Else Statement Example ``` #include
int main() { int a = 1; int b = 2; int c = 3; if (a < b) { printf("a is less than b\n"); } else if(a < c) { printf("a is less than c and greater than or equal to b\n"); } else { printf("a is greater than or equal to b and a is greater than or equal to c\n"); } return 0; } ```
## Basic Input/Output
## getchar * gets **one** character from the command line ``` #include
int main() { char c = getchar(); putchar(c); printf("%c", c); return 0; } ```