0% found this document useful (0 votes)
6 views

PF Lab 6

Uploaded by

tayyabgujar41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PF Lab 6

Uploaded by

tayyabgujar41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

COMSATS UNIVERSITY

Instructor: Tooba Tehreem

LAB #05: INTRODUCTION TO FOR LOOPS, SWITCH CASE

Name:____________________ Reg #: ___________________

Lab Objective:

To practice for loops and switch cases and to get better understanding of
how to use them.

Lab Description:

A for loop is a repetition control structure that allows you to


efficiently write a loop that needs to execute a specific
number of times.

Syntax:

The syntax of a for loop in C++ is −


for ( init; condition; increment )
{
statement(s);
}

Figure 2: the loop’s logic.


Example
int main (){

Output:

Other Forms of the Update Expression :


You are not limited to using increment statements in the update expression. Here is a loop that
displays all the even numbers from 2 through 100 by adding 2 to its counter:
And here is a loop that counts backward from 10 down to 0:

Defining a Variable in the for Loop’s Initialization Expression:


Not only may the counter variable be initialized in the initialization expression, it may be defined
there as well. The following code shows an example.

Using Multiple Statements in the Initialization and Update Expressions:


It is possible to execute more than one statement in the initialization expression and the update
expression. When using multiple statements in either of these expressions, simply separate the
statements with commas. For example

Omitting the for Loop’s Expressions:


The initialization expression may be omitted from inside the for loop’s parentheses if it has already
been performed or no initialization is needed. Here is an example

Nested Loops:
A loop that is inside another loop is called a nested loop. A clock is a good example of something
that works like a nested loop. The second hand, minute hand, and hour hand all spin around the face
of the clock. Each time the hour hand increments, the minute hand increments 60 times. Each time
the minute hand increments, the second hand increments 60 times. Here is a program segment with
a for loop that partially simulates a digital clock. It displays the seconds from 0 to 59:

Example
Output:

Switch case

switch...case is a branching statement used to perform action based on available choices,


instead of making decisions based on conditions. Using switch...case you can write more
clean and optimal code than if...else statement. switch...case only works with integer,
character and enumeration constants.

Lab Tasks:
• Task1: C++ program to Print Table of any Number.
The concept of generating table of any number is multiply particular number from 1 to 10.

num * 1

num * 2

num * 3

num * 4

num * 5

num * 6

num * 7

num * 8

num * 9
num * 10
• Task2: Find power of any number using for loop.

• Task3: Sum of Natural Numbers using loop.

• Task4:C++ program to find sum of digits of a number.


Sum of digits means add all the digits of any number, for example we take any number like
358. Its sum of all digit is 3+5+8=16. Using given code we can easily write c++ program.

• Task 5: Program to Generate Factorial a Certain Number


Factorial of any number is the product of an integer and all the integers below it for example
factorial of 4 is 4! = 4 * 3 * 2 * 1 = 24

• Task7: Write a C++ program that will print the following pattern.
*
**
***
****
*****
******
*******
• Write a C program print total number of days in a month using switch case.
• Write a C program to check whether an alphabet is vowel or consonant using
switch case.

Think??
• What will be the output of the C program?
#include<stdio.h>
int main()
{
for(5;2;2)
{
cout<<"Hello";
}
return 0;
}
• What will be the output of the C program, if input is 6?
#include<stdio.h>
int main()
{
int i;
for(i = 0; i>9; i+=3)
{
cout<<"for ";
}
return 0;
}

• What will be the output of the C ++ program.

#include<stdio.h>
int main()
{
for(;;)
{
cout<<”10”;
}
return 0;
}

• What will be the output of the C program?


int main()
{
int fun=5;
cout<<"C++ for loop ";
int x = 5;
for(x=0;x<=fun;x++)
{
cout<<x;
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy