C Programming Tutorial - Making Decisions Using if Statements


Hey guys in this tutorial we are going to see some example of if statement code

General pattern
#include<stdio.h>

int main() {
  if(test) {
      code; 
  }
}

If the statement is true, it will run the code otherwise it won't.

#1

Input
#include<stdio.h>

int main() {
  if(4<10) {
      printf("Easy hoss"); 
  }
}

Output

Easy hoss

#2
Input

#include<stdio.h>

int main() {
  if(4<10) {
      printf("Easy hoss"); 
  }
  if(4>10){ 
      printf("meatball");
  }
}

Output

Easy hoss

#3

#include<stdio.h>

int main() {
  if(5==5) {
      printf("Easy hoss"); 
  }
  
}

Output

Easy hoss

#4

Input
#include<stdio.h>

int main() {
  if(5!=5) {
      printf("Easy hoss"); 
  }
  
}

Output

none

#5

#include<stdio.h>

int main() {
int age;
printf("How old are you?\n");
scanf("%d", &age);

if(age >=18) {
    printf ("You may enter this website!"); 
}
if (age<18){
    printf("Nohing to see here!");
}
  
}

Comments