Tag c programming language

C If … Else Examples

Real-Life Examples This example shows how you can use if..else to “open a door” if the user enters the correct code: Example int doorCode = 1337; if (doorCode == 1337) {   printf(“Correct code.\nThe door is now open.”); } else…

C Short Hand If Else

Short Hand If…Else (Ternary Operator) There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It…

C Else If

The else if Statement Use the else if statement to specify a new condition if the first condition is false. Syntax if (condition1) {   // block of code to be executed if condition1 is true } else if (condition2)…

C Else

The else Statement Use the else statement to specify a block of code to be executed if the condition is false. Syntax if (condition) {   // block of code to be executed if the condition is true } else…

C If … Else

Conditions and If Statements You have already learned that C supports the usual logical conditions from mathematics: Less than: a < b Less than or equal to: a <= b Greater than: a > b Greater than or equal to:…

C Boolean Examples

Real Life Example Let’s think of a “real life example” where we need to find out if a person is old enough to vote. In the example below, we use the >= comparison operator to find out if the age…

C Booleans

Booleans Very often, in programming, you will need a data type that can only have one of two values, like: YES / NO ON / OFF TRUE / FALSE For this, C has a bool data type, which is known…

C Operators

Operators Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values: Example int myNum = 100 + 50; Although the + operator is often used to…

C Constants

Constants If you don’t want others (or yourself) to change existing variable values, you can use the const keyword. This will declare the variable as “constant”, which means unchangeable and read-only: