Switches and Loops (1) PDF
Document Details
Tags
Related
- Unit 2 Repetition in Programs - C Programming PDF
- C Programming Exam Questions PDF 2024-25
- PPS Unit Wise Important Questions PDF
- 22316 Object-Oriented Programming using C++ Sample Question Paper PDF
- Algorithmique et Programmation C - INPT Smart ICT PDF
- The C Programming Language (Second Edition) PDF
Summary
This document explains different types of loops (while, do-while, for loops) and switches in programming. The document includes code examples, and syntax.
Full Transcript
Switches CONTENT 01 02 03 Switches Introduction While loop on Loops 04 05 06 Do While loop for loop Control your loop 07 Nest...
Switches CONTENT 01 02 03 Switches Introduction While loop on Loops 04 05 06 Do While loop for loop Control your loop 07 Nested loop Switches Most `if` statements are straightforward: a basic `if`, an `if/else`, an `if/else if`, or a combination of these. However, there are times when they grow into long sequences with multiple possible outcomes. In such cases, the structure can resemble a railroad switchyard—where one track branches into many different routes to sort or direct railcars along various paths, much like the image shown below. Traffic Light Example with if statement Switches Syntax switch (variableName) { case value1: // Statement for case value1 statement1; break; case value2: // Statement for case value2 statement2; break; default: // Default statement if no case matches defaultStatement; break; } Traffic Light Example with Switch Loops Introduction on Loops we learned that listing statements one after the next causes them to run in that order. In last lecture, we learned that we could use if statements and switches to skip over statements and pick which of many instructions to run. In this lecture, we’ll discuss the third and final essential element of procedural programming: the ability to go back and repeat code—a loop. C# has four types of loops. THE WHILE LOOP A while loop repeats code over and over for as long as some given condition evaluates to true. Its structure closely resembles an if statement. While loop syntax: THE WHILE LOOP The following code illustrates a while loop that displays the numbers 1 through 5 THE WHILE LOOP Here are a few crucial subtleties of while loops to keep in mind: 1. If the loop’s condition is false initially, the loop’s body will not run at all. 2. The loop’s condition is only evaluated when we check it at the start of each cycle. If the condition changes in the middle of executing the loop’s body, it does not immediately leave the loop. 3. It is entirely possible to build a loop whose condition never becomes false. This is called an infinite loop. It is occasionally done on purpose but usually represents a bug. If your program seems like it has gotten stuck, check to see if you created an infinite loop. THE WHILE LOOP This code asks the user to enter a number between 0 and 10. It keeps asking (with a loop) until they enter a number in that range: THE DO/WHILE LOOP The second loop type is a slight variation on a while loop. A do/while loop evaluates its condition at the end of the loop instead of the beginning. This ensures the loop runs at least once. Don’t forget the semicolon at the end of the line; it is necessary. THE DO/WHILE LOOP Variables Declared in Block Statements and Loops Blocks used in a loop are still just blocks. Like any block, variables declared within the loop’s block are inaccessible once you leave the block. You can declare a variable inside the body of a loop, but these variables will not be accessible outside of the loop or even in the loop’s condition. THE FOR LOOP A for loop lets you pack loop management code into a single line. Syntax: The second part is the condition THE FOR LOOP to evaluate every time through the loop. A for loop is more like a while loop than a do-while loop— one-time setup needed to get defines how to change the if its condition is false initially, the loop started. This nearly variable used in the loop’s the for loop’s body will not run at always involves declaring a condition. all variable and initializing it to its starting value. for (int x = 1; x