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

Exploring the possibilities - C Tutorial

From the course: C Essential Training

Start my 1-month free trial

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…

Contents