Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Fundamentals of Computer Programming Lab 7: Using Switch Statements for Conditional Logic, Lab Reports of Acting

An introduction to switch statements, an alternative to if else statements for evaluating expressions and branching off to multiple sections of a program. The syntax of a switch statement, its advantages over if else statements, and provides an example of its usage. Additionally, it includes an exercise for creating a gpa calculator using both if else and switch statements.

Typology: Lab Reports

Pre 2010

Uploaded on 11/08/2009

koofers-user-qtw
koofers-user-qtw 🇺🇸

10 documents

1 / 2

Toggle sidebar

Related documents


Partial preview of the text

Download Fundamentals of Computer Programming Lab 7: Using Switch Statements for Conditional Logic and more Lab Reports Acting in PDF only on Docsity! CSCI 101: Fundamentals of Computer Programming Lab 7: More Conditional & Relational Operators Fall 2005 Introduction Another alternative to using if else statements are switch statements. Similar to if else statements, the switch statement also evaluate an expression, and branches off to multiple sections of the program depending on that expression. However, the advantage of the switch statement becomes apparent when there are multiple alternatives to the condition, instead of the default true/false conditions that we are used to in if statements. The syntax of a switch statement is below: switch (expression) { case (constant 1): task 1; … break; case (constant 2): task 2; … break; default: task 3; … break; } When the compiler reaches the start of the switch statement, it will evaluate the value of expression. It will then look for the its corresponding case, and evaluates the task that follows it until it encounters the break statement. It will run the default case if no matching cases are found. Both if else and switch statements have their advantages and disadvantages for different cases. They somewhat complement each other relatively well and it is important for programmers to learn when to use which structure for more elegant code. However, it is important to note that both structures are capable of doing what the other does.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved