if(<condition>) {
do_if_true();
} else if(<second_condition>) {
do_if_second_true();
} else {
do_if_both_false();
}
my_age > drinking_age
-> will evaluate to true/1 if age is greater than drinking_age
while(<condition>) {
do_something_over_and_over();
}
while
loop within a while
loopHow can we print something like this?
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
#include <stdio.h>
#define ROWS 5
#define COLUMNS 5
int main() {
int i = 0;
while (i < ROWS) {
int j = 1;
while (j <= COLUMNS) {
printf("%d ", j);
j++;
}
printf("\n");
i++;
}
return 0;
}
1
12
123
1234
12345
int main(void) {
int my_age = 20;
char initial = 'J';
int UNSW_year = 2;
return 0;
}
^ These three things are related...
struct
struct UNSW_student {
int age;
int year_number;
double WAM;
}
To use, we simply say:
struct UNSW_student Jake;
struct
(structures)struct <struct_name> {
data_type identifier;
data_type identifier;
}
struct UNSW_student {
int age;
int year_number;
double WAM;
}
struct <struct_name> {
data_type identifier;
data_type identifier;
}
struct UNSW_student {
int age;
int year_number;
double WAM;
}
^ Notice, no values... we are only defining.
#include <stdio.h>
struct UNSW_student {
int age;
int year_number;
double WAM;
}
int main(void) {
struct UNSW_student Jake;
return 0;
}
.
operatorstruct coordinate {
int x;
int y;
}
struct coordinate loc;
loc.x
loc.y
enum
1. int day_of_week = 1;
2. char day_of_week = 'm';
3. #define MONDAY 1
4. #define TUESDAY 2
day_of_week
enum enum_name { state_1, state_2, state_3... };
enum weekdays { Mon, Tue, Wed, Thu, Fri, Sat, Sun };
#include <stdio.h>
enum weekdays { Mon, Tue, Wed, Thu, Fri, Sat, Sun };
int main(void) {
enum weekdays day;
day = Sat; // <-- this is why enums are useful
return 0;
}
#include <stdio.h>
enum weekdays { Mon, Tue, Wed, Thu, Fri, Sat, Sun }
int main(void) {
enum weekdays day;
day = Sat;
printf("The actual value in day is: %d\n, day);
return 0;
}
#define
but these can clutter our code if we have manystruct
🤝 enum
enum student_status { Enrolled, Withdrawn, Leave }
struct student {
enum student_status status;
int age;
}