1️⃣

4.1 If-Statements

 

If-Statements

An if statement is used to execute something only if a certain condition is met. An if statement checks to make sure the condition is true in its header, and if it is, it provides certain code to be run. Below is the syntax for an if statement:
 
int num1 = 5; int num2 = 3; if (num1 > num2) { printf(“num1 is greater than num2”); }
 
In the above example, we created two variables, num1 and num2, each storing the values 5 and 3, respectively. The if statement checks to see whether num1 is greater than num2, and if that is true, it prints out a statement that says num1 is greater than num2. Notice that the condition that is being checked is contained in parentheses, “( )”, right after the word if. Also, notice that the printf() statement is contained within a set of curly braces, “{ }”. This lets the computer know that whatever is contained within the curly braces should only be run if the condition is true.

Next Section

2️⃣
4.2 Else-Statements
 
⚖️
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.