icp/oer/courses/c-basics/sections/02-basic-language-features/03-conditionals/program.c

69 lines
1.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int value = 42;
if (value >= 27)
{
// This part of the program is only executed, if the expression is true
printf("value is indeed bigger or equal to 27 \n");
}
// the else-statement will be executed if the if-statement wasn't true
if (value == 43)
{
// This will not be executed
printf("value is equal 43 \n");
}
else
{
// This part will be executed
printf("value is not equal to 43 \n");
}
// Another useful syntax is else if statement.
// You want to use it if you want to test for multiple things.
// But be careful! The first condition, which is true, gets executed.
if(value != 42)
{
// This will not be executed
printf("value is not equal to 42 \n");
}
else if(value < 45)
{
// This will be executed
printf("value is less than 45 \n");
}
else if( value == 42)
{
// This will not be executed
printf("value is equal to 42\n");
}
// You can also chain expressions together with the logical operators like && for and || for or and ! for not
if(!(value >= 0 && value <42))
{
printf("Value is not bigger or equal to 0 and less than 42\n");
}
// in c any value other then 0 is considered to be true
// even negative values are true
if(value)
{
// this is legal in c and will be considered as true
}
// this is a great way to check for null-pointers
int Value1 = atoi(argv[1]);
int Value2 = atoi(argv[2]);
// We prepared the variables Value1 and Value2 to hold values,
// which are passed to the program
// TODO: compare these to values the following way
// if value1 is bigger write to the standart output "value 1 is bigger"
// if value2 is bigger write to the standart output "value 2 is bigger"
// otherwise print out "value 1 is equal to value 2"
}