From the course: C Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Exploring the possibilities - C Tutorial
From the course: C Essential Training
Exploring the possibilities
- The if keywords companion is the else keyword, combined you use if and else in your code to create blocks of either or decisions. For example, in this code input is obtained from the user twice. The if statement at line 11 tests to see whether the value of variable A is greater than the value of variable B. If true, the statement belonging to if is executed, but if not, the else statement is executed instead. Build and run the code. Let's try a few values. 10 and 20, 20 is greater than 10. Glad the computer knows this. It's run again, and we'll try eight and negative one and eight is greater than negative one. We're going to run it one more time and try the same value. Hmm, the program didn't air. It did just what you told it to, the else statement catches all conditions where A is greater than B is false, including when A is equal to B. This code addresses the issue with the proceeding example by adding an else if…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Making a decision3m 37s
-
Exploring the possibilities2m 34s
-
Using the ternary operator3m 1s
-
Working with the switch-case structure4m 9s
-
Challenge: Select an item29s
-
Solution: Select an item57s
-
Creating a for loop3m 58s
-
Setting up a while loop3m 6s
-
Challenge: Repeat some text48s
-
Solution: Repeat some text1m 40s
-
Nesting loops1m 55s
-
Breaking out of a loop3m 28s
-
Avoiding the goto keyword1m 56s
-
Challenge: Work the grid2m 21s
-
Solution: Work the grid2m 8s
-
-
-