Using Blocks of Code in if Statements - C Statement

C examples for Statement:if

Introduction

The general form for an if statement that involves statement blocks would look like this:

if(expression){
  StatementA1;
  StatementA2;
  . . .
}else{
  StatementB1;
  StatementB2;
  . . .
}
Next_statement;

Demo Code

#include <stdio.h>

int main(void){
  
  const double unit_price = 3.50;                     // Unit price in dollars
  int quantity = 0;
  
  printf("Enter the number that you want to buy:");   // Prompt message
  scanf(" %d", &quantity);                            // Read the input
  
  double total = 0.0;                                 // Total price
  
  if(quantity > 10){
    total = quantity*unit_price*0.95;//from  w w  w .j  ava 2s  .c o  m
    printf("discount");
  }else{
    total = quantity*unit_price;
    printf("no discount");
  }  
  
  printf("The price for %d is $%.2f\n", quantity, total);
  return 0;
}

Result


Related Tutorials