Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick reference or revision guide. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

Conditional logic

Logic is the ability to make decisions. Conditional logic means making decisions based on clearly defined conditions, e.g. If the starting gun for a race has been fired then start running, else (otherwise) keep waiting.

Top tips:

  • Think carefully about the decisions your program will have to make
    You can compare data using the greater than > operator or the less than < operator. 
  • Be careful when checking if two values are equal
    In VB.NET you use one equals sign (=) to both set a value and test if two values are the same.
    In python you use one equals sign (=) to set a value and two equals signs to test if two values are the same.
Skill
Description
VB.NET
Python
C#
IF statement
   

If you want your code to make a decision you'll need an IF statement.

This example asks the user how old they are and checks that they've entered a number bigger than 0

age = int(input("How old are you?")) if age < 0: print("You can't be less than 0 years old!")
Console.WriteLine("How old are you?"); int age = int.Parse(Console.ReadLine()); if(age < 0) { Console.WriteLine("You can't be less than 0 years old!"); }
IF ... ELSE statement
   

If you want your code to choose between two possibilities you'll need an IF ... ELSE statement.

This example asks the user what their name is. If they answer the question it says hello. If they don't answer the question it displays an error message.

name = input("What is your name?") if name == "": print("You have to enter your name") else: print("Hello " + name)
Console.WriteLine("What is your name?"); string name = Console.ReadLine(); if(name == "") { Console.WriteLine("You have to enter your name"); } else { Console.WriteLine("Hello " + name); }
Not equal to
   

You can test if two expressions are the same using the equal to or not equal to operator.

An expression can be a variable or combination of variables & data (e.g. 1 + 1)

An operator is a symbol that means add, subtract or something similar.

This example asks the user to enter a temperature  of a bath (in degrees Celsius) and then follows the following rules:

Equal to 37 degrees: says "Perfect - that's body temperature"

Not equal to 37 degrees: "Not body temperature"

temperature = int(input("What's the temperature of the bath?") if temperature == 37: print("Perfect - that's body temperature") if temperature != 37: print("Not body temperature")
Console.WriteLine("What's the temperature of the bath?"); int temperature = int.Parse(Console.ReadLine()); if(temperature == 37) { Console.WriteLine("Perfect - that's body temperature"); } if(temperature != 37) { Console.WriteLine("Not body temperature"); }
Greater than or less than
   

If you need to compare two expressions you will need the greater than or less than operators.

An expression can be a variable or combination of variables & data (e.g. 1 + 1)

An operator is a symbol that means add, subtract or something similar.

This example asks the user to enter a password and displays how strong that password is based purely on the length:

Less than 5 characters: weak

More than or equal to 8 characters: strong

Otherwise: medium

password = input("Please enter a password") if len(password) >= 8: print("strong") else: if len(password) < 5: print("weak") else: print("medium")
Console.WriteLine("Please enter a password"); string password = Console.ReadLine(); if (password.Length >= 8) { Console.WriteLine("strong"); } else { if(password.Length < 5) { Console.WriteLine("weak"); } else { Console.WriteLine("medium"); } }

Loading...