Control Flow, Arrays, Date Time and String C Programming Constructs: + Explain selection constructs + Describe loop constructs + Explain jump statements in C Arrays DateTime StringLuồng điều khiển, Mảng, Ngày giờ và Chuỗi Cấu trúc lập trình C : + Giải thích cấu trúc lựa chọn + Mô tả cấu trúc vòng lặp + Giải thích câu lệnh jump trong C Mảng Ngày giờ Chuỗi
Control Flow, Arrays, Date Time and String • C# Programming Constructs • Arrays • DateTime • String C# Programming Constructs • Explain selection constructs • Describe loop constructs • Explain jump statements in C# Selection Constructs • A selection construct is a programming construct supported by C# that controls the flow of a program • Executes a particular block of statements based on a boolean condition, which is an expression returning true or false • C# supports the following decision-making constructs: • if…else • if…else…if • Nested if • switch…case The if Statement • The if statement allows you to execute a block of statements after evaluating the specified logical condition Syntax: if (condition) { // one or more statements; } where, • condition: Is the boolean expression • statements: Are set of executable instructions executed when the boolean expression returns true The if…else Construct • In some situations, it is required to define an action for a false condition by using an if else construct • If the condition specified in the if statement evaluates to false, the statements in the else block are executed Syntax: if (condition) { // one or more statements; } else { //one or more statements; } The if…else…if Construct • The if…else…if construct allows you to check multiple conditions to execute a different block of code for each condition Syntax: if (condition) { // one or more statements; } else if (condition) { // one or more statements; } else { //one or more statements; } Nested if Construct • The following is the syntax for the nested if construct: if (condition) { // one or more statements; if (condition) { // one or more statements; } else { //one or more statements; } } switch…case Construct • The switch…case statement is used when a variable needs to be compared against different values switch (variable) { case s: statement; break; case t: statement; break; default: statement; break; } No-Fall-Through Rule switch (day) { case “Saturday": case “Sunday": Console.WriteLine(“Days off"); break; default: Console.WriteLine(“Working days"); break; } Loop Constructs • Loops allow you to execute a single statement or a block of statements repetitively • In software programming, a loop construct contains a condition that helps the compiler identify the number of times a specific block will be executed • If the condition is not specified, the loop continues infinitely and is termed as an infinite loop • The loop constructs are also referred to as iteration statements • C# supports four types of loop constructs such as: • • • • while loop while loop for loop foreach loop ... statement; break; } No-Fall-Through Rule switch (day) { case “Saturday": case “Sunday": Console.WriteLine(“Days off"); break; default: Console.WriteLine(“Working days"); break; } Loop Constructs • Loops... used to create and initialize an array without using the new keyword: type[] arrayName = {val1, val2, val3, , valN};