INTRODUCTION TO COMPUTER SCIENCE - LAB

Similar documents
Introduction to C. Systems Programming Concepts

LAB 4.1 Relational Operators and the if Statement

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

C++/Java. C++: Hello World. Java : Hello World. Expression Statement. Compound Statement. Declaration Statement

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Following is the general form of a typical decision making structure found in most of the programming languages:

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Programming, numerics and optimization

ISA 563 : Fundamentals of Systems Programming

First of all, it is a variable, just like other variables you studied

C++ Programming: From Problem Analysis to Program Design, Third Edition

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

EEE145 Computer Programming

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Decision Making in C

Computer Programming : C++

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Add Subtract Multiply Divide

CSI33 Data Structures

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Lecture 5 Tao Wang 1

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Introduction to Programming

THE FUNDAMENTAL DATA TYPES

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

LECTURE 5 Control Structures Part 2

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. Fundamental Data Types

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

The Java language has a wide variety of modifiers, including the following:

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

DEPARTMENT OF COMPUTER ENGINEERING CMPE101: Foundation of Computer Engineering EXPERIMENT 3. Introduction to C Programming: Sequential code structure

Introduction to Computer Science Midterm 3 Fall, Points

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

OBJECT ORIENTED PROGRAMMING

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Programming Fundamentals and Methodology. COMP104: C++ Basics

Increment and the While. Class 15

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Pointers and Strings Prentice Hall, Inc. All rights reserved.

C++ Final Exam 2017/2018

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Variables and Constants

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Eng. Mohammed S. Abdualal

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Operators & Expressions

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

This tutorial adopts a simple and practical approach to describe the concepts of C++.

CS Spring 05 - MidTerm

SFU CMPT Topic: Control Statements

In this chapter you will learn:

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Tutorial-2a: First steps with C++ programming

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

INFO-I308 Laboratory Session #2

Programming Language. Functions. Eng. Anis Nazer First Semester

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Chapter 7. Additional Control Structures

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

FORM 2 (Please put your name and form # on the scantron!!!!)

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Introduction to C/C++ Lecture 3 - Program Flow Control

Data Structure Lab. Prepared By Dr. Hanan Ahmed Salman

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Boolean Algebra Boolean Algebra

Computer Programming. Decision Making (2) Loops

5. Selection: If and Switch Controls

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Flow Control. CSC215 Lecture

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Data Types and Variables in C language

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Control Structures. Repetition (Loop) Structure. Repetition (Loop) Structure. Repetition (Loop) Structure. CS225: Slide Set 8: C++ Loop Structure

Programming I Lecture 7

conditional statements

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

C/C++ Programming Lecture 7 Name:

Transcription:

LAB # O2: OPERATORS AND CONDITIONAL STATEMENT Assignment operator (=) The assignment operator assigns a value to a variable. X=5; Expression y = 2 + x; Increment and decrement (++, --) suffix X++ X-- prefix ++X --X Prepared by: Engr. M.Nadeem Page 1

In the case that the increase operator is used as a prefix (++x) the value, the expression evaluates to the final value of x, once it is already increased. On the other hand, in case that it is used as a suffix (x++) the value stored in x the expression evaluates to the value x had before being increased. Notice the difference: Example 1 Example 2 x = 3; x = 3; y = ++x; y = x++; // x contains 4, y contains 4 // x contains 4, y contains 3 In Example 1, the value assigned to y is the value of x after being increased. While in Example 2, it is the value x had before being increased. Prepared by: Engr. M.Nadeem Page 2

Prepared by: Engr. M.Nadeem Page 3

Sizeof Operator: The syntax of using sizeof is as follows: Conditional Operator: The Syntax of Conditional Operator as follows: Prepared by: Engr. M.Nadeem Page 4

Compare with If Statement Prepared by: Engr. M.Nadeem Page 5

Prepared by: Engr. M.Nadeem Page 6

Class Tasks to be performed 1- What will be the output of the given programs? Program 1: (Assignment Operator) int a, b; a = 10; b = 4; a = b; b = 7; cout << "a:"; cout << a; cout << " b:"; cout << b; Program 2: (Compound Assignment Operators) Prepared by: Engr. M.Nadeem Page 7

int a, b=3; a = b; a+=2; cout << a; // equivalent to a=a+2 Program 3: (Conditional Operator) int a,b,c; a=2; b=7; c = (a>b)? a : b; cout << c << '\n'; Program 4: (Arthmatic Operator) main() int a = 21; int b = 10; int c ; c = a + b; cout << "Line 1 - Value of c is :" << c << endl ; c = a - b; cout << "Line 2 - Value of c is :" << c << endl ; c = a * b; cout << "Line 3 - Value of c is :" << c << endl ; c = a / b; cout << "Line 4 - Value of c is :" << c << endl ; c = a % b; cout << "Line 5 - Value of c is :" << c << endl ; c = a++; cout << "Line 6 - Value of c is :" << c << endl ; c = a--; cout << "Line 7 - Value of c is :" << c << endl ; Program 5: (Relational Operators ) Prepared by: Engr. M.Nadeem Page 8

main() int a = 21; int b = 10; int c ; if( a == b ) cout << "Line 1 - a is equal to b" << endl ; else cout << "Line 1 - a is not equal to b" << endl ; if ( a < b ) cout << "Line 2 - a is less than b" << endl ; else cout << "Line 2 - a is not less than b" << endl ; if ( a > b ) cout << "Line 3 - a is greater than b" << endl ; else cout << "Line 3 - a is not greater than b" << endl ; /* Let's change the values of a and b */ a = 5; b = 20; if ( a <= b ) cout << "Line 4 - a is either less than \ or euqal to b" << endl ; if ( b >= a ) cout << "Line 5 - b is either greater than \ or equal to b" << endl ; Program 6: (Logical operators) main() int a = 5; int b = 20; int c ; if ( a && b ) Prepared by: Engr. M.Nadeem Page 9

cout << "Line 1 - Condition is true"<< endl ; if ( a b ) cout << "Line 2 - Condition is true"<< endl ; /* Let's change the values of a and b */ a = 0; b = 10; if ( a && b ) cout << "Line 3 - Condition is true"<< endl ; else cout << "Line 4 - Condition is not true"<< endl ; if (!(a && b) ) cout << "Line 5 - Condition is true"<< endl ; Program 7: (Size of) int main() cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; Program 8: (IF Statement) // local variable declaration: int a = 10; // check the boolean condition if( a < 20 ) // if condition is true then print the following Prepared by: Engr. M.Nadeem Page 10

cout << "a is less than 20;" << endl; cout << "value of a is : " << a << endl; Program 9: (if else Statement) // local variable declaration: int a = 100; // check the boolean condition if( a < 20 ) // if condition is true then print the following cout << "a is less than 20;" << endl; else // if condition is false then print the following cout << "a is not less than 20;" << endl; cout << "value of a is : " << a << endl; Program 10: (The if...else if...else Statement): // local variable declaration: int a = 100; // check the boolean condition if( a == 10 ) // if condition is true then print the following cout << "Value of a is 10" << endl; else if( a == 20 ) // if else if condition is true cout << "Value of a is 20" << endl; else if( a == 30 ) // if else if condition is true cout << "Value of a is 30" << endl; Prepared by: Engr. M.Nadeem Page 11

else // if none of the conditions is true cout << "Value of a is not matching" << endl; cout << "Exact value of a is : " << a << endl; Program 10: (Switch Statement) // local variable declaration: char grade = 'D'; switch(grade) case 'A' : cout << "Excellent!" << endl; break; case 'B' : case 'C' : cout << "Well done" << endl; break; case 'D' : cout << "You passed" << endl; break; case 'F' : cout << "Better try again" << endl; break; default : cout << "Invalid grade" << endl; cout << "Your grade is " << grade << endl; Program 11: (Nested if else Statement) // local variable declaration: int a = 100; int b = 200; // check the boolean condition if( a == 100 ) Prepared by: Engr. M.Nadeem Page 12

// if condition is true then check the following if( b == 200 ) // if condition is true then print the following cout << "Value of a is 100 and b is 200" << endl; cout << "Exact value of a is : " << a << endl; cout << "Exact value of b is : " << b << endl; Prepared by: Engr. M.Nadeem Page 13