Else-Statements
An
else
statement is used to execute something if the condition checked in the if
statement is not true. Below is a continuation of the code from the previous section, this time including an else
statement:int num1 = 5; int num2 = 3; if (num1 > num2) { printf(“num1 is greater than num2”); } else { printf(“num1 is not greater than num2”); }
In the above example, the else statement comes after the
if
statement, and it prints out a message (contained within curly braces) that num1
is not greater than num2
.Practice
Baked
I’m making my famous peanut butter cookies today! Make a program to check whether or not my cookies are baked, then print out a message for me! My cookies are baked if and only if the oven is at least 350 degrees.
Solution
#include <stdio.h> void int(main) { int ovenTemp = 300; if (oventemp >= 350) { printf("Baked!"); } else { print("Raw..."); } }
Copyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.