Week 2 Code Examples
#include<stdio.h>
// this is a constant that sets MIN_AGE to 18
#define MIN_AGE 18
int main(void) {
int age;
printf("Enter your age: ");
scanf("%d", &age);
// check is age is valid
if (age <= 0) {
printf("That is invalid!\n");
} else {
// we know for a fact everythign here is a positive age!
// age is 1 or higher
// are you greater than 0 but less than 18
if(age < MIN_AGE) {
printf("You are too young (but valid!)! Get out\n");
} else {
printf("You are legal! Enjoy\n");
}
}
return 0;
}
#include<stdio.h>
int main(void) {
int number_of_lines = 5;
int i = 0;
while (i < number_of_lines) {
printf("hey!\n");
i = i + 1;
}
}
#include<stdio.h>
int main(void) {
int finished = 0;
while (!finished) {
printf("hey! Keep printing?\n");
printf("(enter 1 if finished): ");
scanf("%d", &finished);
}
printf("Program finishing up\n");
}
ELF >