Tutorial#3 if..../if.....else/if.....else if/ switch Solution.

Slides:



Advertisements
Similar presentations
Revision.
Advertisements

CMPUT 101 Lab # 5 October 22, :00 – 17:00.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
Principles of programming languages 2: Answers for exercises
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Switch Statement in C++. Syntax switch (selector) { case L1: statements1; break; case L2: statements2; break; … default: statements_n; } Semantics: This.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
Chapter 05 (Part III) Control Statements: Part II.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 CS161 Introduction to Computer Science Topic #8.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Char ch; ch ‘L’‘X’‘V’‘I’ As in Roman numerals Want to give each a value, n say switch (ch) { case ‘I’:n = 1; break; case ‘V’:n = 5; break; … default:cout.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
CS 240 Computer Programming 1
Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Chapter 3 Selection Statements
Control Statement tsenghy.
Compound Condition Break , Continue Switch Statements in Java
LESSON 4 Decision Control Structure
A mechanism for deciding whether an action should be taken
Engineering Problem Solving with C++, Etter/Ingber
CS149D Elements of Computer Science
Compound Assignment Operators in C++
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
The switch statement: an alternative to writing a lot of conditionals
CSE 1020:Control Structure
Decision Structures Case Structures.
Self study.
If Statements.
Control Structures Part 3
switch Selection Structure
Statements and flow control
do/while Selection Structure
Fundamental Programming
CS 101 First Exam Review.
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Programming Fundamental
Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3.
Presentation transcript:

Tutorial#3 if..../if.....else/if.....else if/ switch Solution

Q1: What is the output of these code: Code1: int n, k = 5; n = (100 % k ? k + 1 : k - 1); n=4

Code2: int a = 4, b = 2, c = 5; if(a > b) a= 5; if(c == a) { a = 6; c = 3; } else a = 7; cout << “a is : “<<a ; cout << “c is : “<<c ; a is: 6 c is:3

Code3 int x; int y; x=2; y=5; if(x = y) { cout <<"x is equal to y"; cout << "x+y=" << x+y<<","<<"y++="<<y++; } else cout << "x is not equal to y"; x is equal to y x+y= 10, y++=5

Code4 int x=5; switch (x) { cout << “$$$$”<< endl; case 1: x=x+2; break; case 3: x=x+1; case 5: if (x==4) x=x+6; case 6: x=x+3; break; default: x=x-1; } cout << x<< endl; 8

Q2: Convert the following switch statement to if statement int value = 0, input ; cin>>input; switch(input) { case 1: value+=4; break; case 2: value+=3; break; case 3: value+=2; break; default: value++; } if(input==1) value+=4; else if(input==2) value+=3; else if(input==3) value+=2; else value++;

Q3: Rewrite the following fragment using switch if (letter == ‘R’) cout <<“Red”; else if (letter ==‘Y’ ) cout<<“Yellow”; else if (letter == ‘W’) cout<<“White”; else cout<<“ No color “; switch(letter) { case ‘R’: cout<<“Red”; break; case ‘Y’: cout<<“Yellow”; break; case ‘W’: cout<<“White”; break; default: cout<<“No color”; }

Write a C++ program to find absolute value of negative number. Write a C++ program to test a number is negative number or positive number.