Lesson 12 of 12
Learn datatypes with flowcharts, as college courses teach them: Integer, Real, String and Boolean, what each holds, and the int, double and String in Java.
About 8 minutes · 5 parts, each with a program to try
Part 1 of 5
A value has a name, what it holds now, and a datatype: what kind of value it can hold. age holds 18, so it's an Integer.
B4Coder works datatypes out by itself. College courses (and Flowgorithm) write them down: click the Start block and turn on Datatype Mode, then give each Create Value and Ask its datatype.
There are four: Integer (whole numbers, like 17), Real (numbers with a decimal point, like 8.5), String (text, like "Asha") and Boolean (true or false).
name = "Asha"age = 18cgpa = 8.5hosteller = Trueprint("Name: " + name)print("Age: " + str(age))print("CGPA: " + str(cgpa))print("Hosteller: " + str(hosteller).lower())Part 2 of 5
An Integer holds whole numbers only. Put 3.5 into one and the program stops: "3.5 has a decimal point".
A Real holds any number. An Integer can always go into a Real, so the average of Integer marks goes into a Real average.
Dividing gives a Real (7 / 2 is 3.5). To keep a whole number, use floor, or % for what's left over.
import math
def round_half_up(x, digits=0): # Like round() in B4Coder: a half goes up, so 2.5 is 3 (Python's round() gives 2). scale = 10 ** digits whole = math.floor(x * scale) if x * scale - whole >= 0.5: whole += 1 return whole if digits == 0 else whole / scale
m1 = int(input("Marks in subject 1 "))m2 = int(input("Marks in subject 2 "))m3 = int(input("Marks in subject 3 "))total = m1 + m2 + m3average = round_half_up(total / 3, 2)print("Total: " + str(total))print("Average: " + str(average))Part 3 of 5
A String holds text: a name, a department, a roll number. In a program it's written in "quotes".
A roll number like 0042 is a String, not an Integer: as an Integer it becomes 42 and loses its zeros. Phone numbers and PIN codes are Strings too.
roll = input("Enter your roll number ")rollNumber = int(input("Type it again "))print("As a String: " + roll)print("As an Integer: " + str(rollNumber))Part 4 of 5
A Boolean holds just true or false: the answer to a yes-or-no question. passed = marks >= 40 is true when the mark is 40 or more.
A Decision can use a Boolean on its own: passed is already true or false. When an Ask's answer is a Boolean, they type yes or no.
marks = int(input("Your marks "))passed = marks >= 40print("passed = " + str(passed).lower())if passed: print("Well done, you passed!")else: print("Not this time: you need 40.")Part 5 of 5
In Datatype Mode, an Ask whose answer is an Integer asks again if they type 7.5. A Change Value that would put text into an Integer stops the program with a message saying why.
View Code writes the same datatypes: int, double, String and boolean in Java, the way you'll write them in C++, C# and Java at college.
Already have a program? Turn on Datatype Mode and press Fill in datatypes: B4Coder fills in the type each value already holds.
number = int(input("Enter a whole number "))if number % 2 == 0: print(str(number) + " is Even")else: print(str(number) + " is Odd")Quick check