You are here

Case Selection

26 July, 2019 - 09:47
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at

This program determines if a year is a leap year or not. A leap year is divisible by 4 but not by 100, except when it is divisible by 400. A number x is divisible by a number y if the remainder of x/y is identical to zero, i.e. Rem(x/y}=O is true therefore

Leap Year = {Rem (Year/4) = 0 And Not (Rem (Year/100) = 0)} Or Rem (Year/400) = 0        (3.1)

where And, Or and Not are Boolean operators.

For example:

1900 is not a leap year because it is divisible by 100

1970 is not a leap year because it is not divisible by 4

1980 is a leap year because it is divisible by 4 but not by 100

2000 is a leap year because it is divisible by 400

Start a new G program and right click on the Block Diagram window. Go to the Functions » Programming » Numeric menu in the Block Diagram window.

Figure 1.26 Quotient & Remainder Function 

Select three copies of the Quotient & Remainder function and three numeric constants. Type in 4, 100 and 400 for the numeric constants and wire these constants to the lower input terminal (corresponding to the dividend) of the Quotient & Remainder function.

Figure 1.27 Leap Year Numeric Constants 

From the Functions » Programming » Comparison menu, select 2 copies of the Equal to Zero function and one copy of the Not Equal to Zero function.

Figure 1.28 Comparison Functions 

Organize the comparison operations as show in the diagram.

Figure 1.29 Diagram 

From the Functions » Programming » Boolean menu select the AND and OR operators

Figure 1.30 Boolean Operators 

Place the Boolean operators as shown in Q&R, Comparison & Boolean Functions.

Figure 1.31 Q&R, Comparison & Boolean Functions 

From the Functions » Programming » Structures menu, click on the Case Structure.

Figure 1.32 Case Structure 

Click and drag on the Block Diagram window to create the Case Structure.

Figure 1.33 Creating a Case Structure 

The True diagra m option is indicated at the top of the case structure.

Figure 1.34 Created Case Structure 

Drop a string constant and type "Is a Leap Year".

Figure 1.35 True Case Editing 

Click on the down arrowhead next to the True label and select the False option .

Figure 1.36 Selecting the False Case  

Drop another string constant and type "Is not a Leap Year".

Figure 1.37 False Case Editing 

Go to the Front Panel window and place a numeric input and an output string. Re-label the numeric input to Year and the output string to Message.

Figure 1.38 Leap Year GUI 

Right click on Year and select Representation » I32 from the numeric pop-up menu.

Figure 1.39 32-Bit Integer Numeric 

Arrange the Year and Message terminals in the Block Diagram window as shown in the figure.

Figure 1.40 Unwired Leap Year Diagram  

Wire the OR operator is to the "7" in the case structure and the string constant "Is not a Leap Year" is wired to Message.

Figure 1.41 Leap Year False Case 

Select the True option and Wire the "Is a Leap Year" string constant to the output terminal of the Case Structure.

Figure 1.42 Leap Year True Case 

Save the program as Leap, enterYear values and run the program to determine whether the value of Year is that of a leap year or not.

Figure 1.43 Leap Year Program