0% found this document useful (0 votes)
4 views9 pages

Class Running Notes 14th Sept

The document explains iterative statements in Java, including while loops, do-while loops, and for loops, detailing their syntax and execution flow. It also provides example Java programs for string manipulation, including reversing a string and checking for palindromes, as well as retrieving characters based on index and identifying vowels or consonants. Additionally, it covers ASCII codes for characters and includes example code for displaying uppercase letters, lowercase letters, and numbers.

Uploaded by

kg666235
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)
4 views9 pages

Class Running Notes 14th Sept

The document explains iterative statements in Java, including while loops, do-while loops, and for loops, detailing their syntax and execution flow. It also provides example Java programs for string manipulation, including reversing a string and checking for palindromes, as well as retrieving characters based on index and identifying vowels or consonants. Additionally, it covers ASCII codes for characters and includes example code for displaying uppercase letters, lowercase letters, and numbers.

Uploaded by

kg666235
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/ 9

Dt : 14/9/2022

*imp

Iterative Statements:

=>The statements which are used to execute some selected

lines of program repeatedly are known as Iterative

Statements.

i
thi
=>The following are some important iterative statements

from Java:

ipa
(a)while loop

(b)do-while loop
Ma
(c)for loop

(a)while loop:
sh
=>In while looping structure the condition is checked

first and then the loop body is executed,this process is


ate

repeated until the condition is false.


nk

syntax:

while(condition)
Ve

//loop_body

Flowchart:
i
thi
ipa
-----------------------------------------------------

(b)do-while loop:
Ma
=>In do-while loop the loop body is executed first

and then the condition checked,this process is repeated


sh
until the condition is false.
ate

syntax:

do
nk

//loop_body
Ve

while(condition);

Flowchart:
i
thi
ipa
Note:
Ma
=>In realtime do-while loop is less used when compared

to while loop,because in do-while loop the execution

time is wasted in executing loop_body for false condition.


sh
-----------------------------------------------------

(c)for loop:
ate

=>for-loop is more simple in representation when

compared to while and do-while loops,because the


nk

initialization,condition and Incre/decre declared in the

same line
Ve

syntax:

for(Initialization;Condition;Incre/Decre)

//loop_body
}

Flowchart:

i
thi
ipa
Ma
=======================================================
sh
Ex-Program:

wap to read a String and display in reverse?


ate

DemoString2.java
nk

package maccess;
import java.util.*;
public class DemoString2 {
Ve

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println("Enter the String:");
String str = s.nextLine();
int len = str.length();
System.out.println("====display in forward based on
index===");
for(int i=0;i<=len-1;i++)
{
char ch = str.charAt(i);//get character by index
System.out.print(ch+" ");
}
System.out.println("\n====Display in reverse=====");
for(int i=len-1;i>=0;i--)
{
char ch = str.charAt(i);
System.out.print(ch+" ");
}
s.close();
}
}

i
thi
o/p:

Enter the String:

ipa
java language programming

====display in forward based on index===


Ma
java language programming

====Display in reverse=====

gnimmargorp egaugnal avaj


sh
==================================================
ate

Assignment:

wap to read a String and check the String is palindrome

String or not?
nk
Ve

Note:

=>The reverse of String is equal to the given String is

known as Palindrome String.

======================================================

faq:

define ASCII Codes?


=>ASCII stands for 'American Standard Code for Information

Interchange', and which is unique code generated for every

character entered from the keyboard.

ASCII codes for UpperCase letters : 65 to 90

i
thi
ASCII codes for LowerCase letters : 97 to 122

ipa
ASCII codes for Numbers( 0 to 9) : 48 to 57

Ma
Ex : DemoASCII.java

package maccess;
public class DemoASCII {
public static void main(String[] args) {
System.out.println("====UpperCase letters====");
sh
for(int i=65;i<=90;i++)
{
char ch = (char)i;//TypeCasting-ASCII(int) to char
ate

System.out.print(ch+" ");
}//end of loop
System.out.println("\n====LowerCase letters====");
for(int i=97;i<=122;i++)
{
nk

char ch = (char)i;//TypeCasting-ASCII(int) to char


System.out.print(ch+" ");
}//end of loop
Ve

System.out.println("\n====Numbers(0 - 9)====");
for(int i=48;i<=57;i++)
{
char ch = (char)i;//TypeCasting-ASCII(int) to char
System.out.print(ch+" ");
}//end of loop
}
}
o/p:

====UpperCase letters====

ABCDEFGHIJKLMNOPQRSTUVWXYZ

====LowerCase letters====

abcdefghijklmnopqrstuvwxyz

====Numbers(0 - 9)====

i
thi
0123456789

====================================================

ipa
Assignment:(Soulution)

wap to retrieve character based on index value and check


Ma
character is Vowel or Consonent or others?

DemoString3.java
sh
package maccess;
import java.util.Scanner;
public class DemoString3 {
ate

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println("Enter the String:");
String str = s.nextLine();
int len = str.length();
nk

System.out.println("Enter the index to retrieve char:");


int index = s.nextInt();
if(index>=0 && index<=len-1)
Ve

{
char ch = str.charAt(index);
int k = (int)ch;//TypeCasting-char to ASCII(int)
if((k>=65 && k<=90) || (k>=97 && k<=122))
{
switch(ch)
{
case 'a':
case 'A':
System.out.println("char : "+ch);
System.out.println("Vowel...");
break;
case 'e':
case 'E':
System.out.println("char : "+ch);
System.out.println("Vowel...");
break;
case 'i':
case 'I':
System.out.println("char : "+ch);
System.out.println("Vowel...");

i
break;

thi
case 'o':
case 'O':
System.out.println("char : "+ch);
System.out.println("Vowel...");

ipa
break;
case 'u':
case 'U':
Ma System.out.println("char : "+ch);
System.out.println("Vowel...");
break;
default:
System.out.println("char : "+ch);
System.out.println("Consonent...");
}//end of switch
sh
}//end of if
else
{
ate

System.out.println("char : "+ch);
System.out.println("Others...");
}
}//end of if
nk

else
{
System.out.println("Invalid index...");
}
Ve

s.close();
}

o/p:

Enter the String:


java

Enter the index to retrieve char:

char : j

Consonent...

===================================================

i
thi
ipa
Ma
sh
ate
nk
Ve

You might also like