Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

Selection

What is selection?

  • Selection is when the flow of a program is changed, depending on a set of conditions

  • The outcome of this condition will then determine which lines or block of code is run next

  • Selection is used for validation, calculation and making sense of a user’s choices

  • There are two ways to write selection statements:

    • if… then… else

    • case

If statements

What is an if statement?

  • An If statements allow you to execute a set of instructions if a condition is true

  • They have the following syntax:

Pseudocode

IF <condition> THEN <statement(s)>
ENDIF
IF <condition> THEN <statement(s)>
ELSE <statement(s)>
ENDIF

Without an ELSE clause

With an ELSE clause

Nested if statements

  • Nested if statements are an if statement within an if statement

  • Nested means to be ‘stored inside the other

Example code

IF Player2Score > Player1Score THEN IF Player2Score > HighScore THEN OUTPUT Player2, " is champion and highest scorer" ELSE OUTPUT Player2, " is the new champion" ENDIF
ELSE OUTPUT Player1, " is still the champion" IF Player1Score > HighScore THEN OUTPUT Player1, " is also the highest scorer" ENDIF
ENDIF

If statements in different languages

Python

VB.net

Java

weather = input("What is the weather like today? (sunny, rainy, snowy): ") if weather == "sunny": print("Don't forget your sunglasses!")
elif weather == "rainy": print("Take an umbrella with you.")
elif weather == "snowy": print("Wear warm clothes!")
else: print("Not sure what to suggest for that kind of weather.")
Module WeatherCheck Sub Main() Dim weather As String Console.Write("What is the weather like today? (sunny, rainy, snowy): ") weather = Console.ReadLine() If weather = "sunny" Then Console.WriteLine("Don't forget your sunglasses!") ElseIf weather = "rainy" Then Console.WriteLine("Take an umbrella with you.") ElseIf weather = "snowy" Then Console.WriteLine("Wear warm clothes!") Else Console.WriteLine("Not sure what to suggest for that kind of weather.") End If Console.ReadLine() ' Keeps the console open End Sub
End Module
import java.util.Scanner; public class WeatherCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String weather; System.out.print("What is the weather like today? (sunny, rainy, snowy): "); weather = scanner.nextLine(); if (weather.equals("sunny")) { System.out.println("Don't forget your sunglasses!"); } else if (weather.equals("rainy")) { System.out.println("Take an umbrella with you."); } else if (weather.equals("snowy")) { System.out.println("Wear warm clothes!"); } else { System.out.println("Not sure what to suggest for that kind of weather."); } scanner.close(); }
}

Case statements

What is a case statement?

  • A case statement can mean less code but it only useful when comparing multiple values of the same variable

  • If statements are more flexible and are generally used more in languages such as Python

  • The format of a CASE statement is:

Pseudocode

CASE of <identifier> <value 1> : <statement1> <statement2> ... <value 2> : <statement1> <statement2> ... ...
ENDCASE 
CASE of <identifier> <value 1> : <statement1> <statement2> ... <value 2> : <statement1> <statement2> ... OTHERWISE : <statement1> <statement2> ...
ENDCASE 

An OTHERWISE clause can be the last clause

Example code

DECLARE Direction : STRING OUTPUT "Enter a direction (N, S, E, W):"
INPUT Direction CASE OF Direction "N" : OUTPUT "You are heading North" "S" : OUTPUT "You are heading South" "E" : OUTPUT "You are heading East" "W" : OUTPUT "You are heading West" OTHERWISE : OUTPUT "Invalid direction entered"
ENDCASE

Case statements in different languages

Python

VB.net

Java

weather = input("What is the weather like today? (sunny, rainy, snowy): ") match weather: case "sunny": print("Don't forget your sunglasses!") case "rainy": print("Take an umbrella with you.") case "snowy": print("Wear warm clothes!") case _: print("Not sure what to suggest for that kind of weather.")
Module WeatherCheck Sub Main() Dim weather As String Console.Write("What is the weather like today? (sunny, rainy, snowy): ") weather = Console.ReadLine() Select Case weather Case "sunny" Console.WriteLine("Don't forget your sunglasses!") Case "rainy" Console.WriteLine("Take an umbrella with you.") Case "snowy" Console.WriteLine("Wear warm clothes!") Case Else Console.WriteLine("Not sure what to suggest for that kind of weather.") End Select Console.ReadLine() ' Keeps the console open End Sub
End Module
import java.util.Scanner; public class WeatherCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String weather; System.out.print("What is the weather like today? (sunny, rainy, snowy): "); weather = scanner.nextLine(); switch (weather) { case "sunny": System.out.println("Don't forget your sunglasses!"); break; case "rainy": System.out.println("Take an umbrella with you."); break; case "snowy": System.out.println("Wear warm clothes!"); break; default: System.out.println("Not sure what to suggest for that kind of weather."); break; } scanner.close(); }
}

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注