The if keyword alters program flow based on evaluating an expression, true or false. Decision-making tools make it appear as though a program is smart by evaluating certain conditions and taking specific actions—or not.
- C programs run top down as written in the source code file. Each statement runs one after the other, until the final statement in the function. You can alter program flow by calling a function, in which case the functions statements execute. Here in the main function at line 32, the get_value function is called. At this point execution jumps to the function, which is located at line four in this source code file. Within a function, an if decision statement can alter program flow. When the if statement condition is true, the statements belonging to it are executed, otherwise they're skipped. So here at line 34, this statement is executed only when the value of variable rows is greater than 18. You can also alter program flow by using a loop. The for statement at line 37 repeats a group of statements based on a condition. This for statement is yet another way to alter program flow. The flow in this code is altered by the if statement at line nine. The if keyword is followed by a set of parentheses in which a condition appears, an expression. When this expression proves true or equals a non zero value, a block of statements are executed. When the expression is false, the block is skipped. Here when the value of variable a is less than 10, the statement puts at line 11 is executed, otherwise it's skipped. Build and run the code and try a few values. Four and let's run again. 100. You see the statement is skipped. Because only one statement is included in this If construction, you can remove the braces. In fact, both lines here, technically a single statement, though it's tradition and expected to put the if statement on another line. Oh, and don't put the semi colon right here, which happens often out of habit, but it's wrong. The if statement's semi-colon is actually right here. Because multiple statements belong to this code's if decision block, they must be enclosed embraces. Build and run this code on your own to see what it does. You can nest if statements as shown in this code, the two statements work like a filter to ensure in this case, if a is greater than six, but a second test to see if is also less than 15. When these conditions are met, the putchar Statement is executed. Of course, this code can be simplified. Here you see the simplified version. This exercise file places both conditions into a single if statement, the code runs the same, but it's easier to read if the value of a is greater than six. And this is the logical and operator, the value of variable a is less than 15. If these conditions are true, both of them, this statement, just one executes. Build and run. And you see in the output asterisks appear by values greater than six and less than 15. This shows not only how flow can be altered in a program, but how a decision can rely upon two expressions coupled with a logical operator.
- Writing your first C program
- Working the C development cycle
- Adding comments to code for clarity
- C language data types
- Declaring variables
- Making new data types
- Typecasting variables
- Using math, assignment, and logical operators
- Creating for and while loops
- Nesting loops
- Creating functions
- Returning values from functions
- Passing arguments to functions
- Creating recursive functions
Skill Level Intermediate
C: Data Structures, Pointers, and File Systemswith Dan Gookin2h 57m Intermediate
1. The Fundamentals of C
2. Variables and Values
3. Operators and Math
4. Decisions and Loops
Next steps1m 38s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.